OSDN Git Service

try to fix ban peer bug (#273)
[bytom/vapor.git] / encoding / json / json.go
1 package json
2
3 import (
4         "encoding/hex"
5         "encoding/json"
6 )
7
8 func IsValidJSON(b []byte) bool {
9         var v interface{}
10         err := json.Unmarshal(b, &v)
11         return err == nil
12 }
13
14 type HexBytes []byte
15
16 func (h HexBytes) MarshalText() ([]byte, error) {
17         return []byte(hex.EncodeToString(h)), nil
18 }
19
20 func (h *HexBytes) UnmarshalText(text []byte) error {
21         n := hex.DecodedLen(len(text))
22         *h = make([]byte, n)
23         _, err := hex.Decode(*h, text)
24         return err
25 }
26
27 type Map []byte
28
29 func (m Map) MarshalJSON() ([]byte, error) {
30         return m, nil
31 }
32
33 func (m *Map) UnmarshalJSON(text []byte) error {
34         var check map[string]*json.RawMessage
35         err := json.Unmarshal(text, &check)
36         if err != nil {
37                 return err
38         }
39         *m = text
40         return nil
41 }