gitee.com/quant1x/gox@v1.7.6/fastjson/handy.go (about) 1 package fastjson 2 3 var handyPool ParserPool 4 5 // GetString returns string value for the field identified by keys path 6 // in JSON data. 7 // 8 // Array indexes may be represented as decimal numbers in keys. 9 // 10 // An empty string is returned on error. Use Parser for proper error handling. 11 // 12 // Parser is faster for obtaining multiple fields from JSON. 13 func GetString(data []byte, keys ...string) string { 14 p := handyPool.Get() 15 v, err := p.ParseBytes(data) 16 if err != nil { 17 handyPool.Put(p) 18 return "" 19 } 20 sb := v.GetStringBytes(keys...) 21 str := string(sb) 22 handyPool.Put(p) 23 return str 24 } 25 26 // GetBytes returns string value for the field identified by keys path 27 // in JSON data. 28 // 29 // Array indexes may be represented as decimal numbers in keys. 30 // 31 // nil is returned on error. Use Parser for proper error handling. 32 // 33 // Parser is faster for obtaining multiple fields from JSON. 34 func GetBytes(data []byte, keys ...string) []byte { 35 p := handyPool.Get() 36 v, err := p.ParseBytes(data) 37 if err != nil { 38 handyPool.Put(p) 39 return nil 40 } 41 sb := v.GetStringBytes(keys...) 42 43 // Make a copy of sb, since sb belongs to p. 44 var b []byte 45 if sb != nil { 46 b = append(b, sb...) 47 } 48 49 handyPool.Put(p) 50 return b 51 } 52 53 // GetInt returns int value for the field identified by keys path 54 // in JSON data. 55 // 56 // Array indexes may be represented as decimal numbers in keys. 57 // 58 // 0 is returned on error. Use Parser for proper error handling. 59 // 60 // Parser is faster for obtaining multiple fields from JSON. 61 func GetInt(data []byte, keys ...string) int { 62 p := handyPool.Get() 63 v, err := p.ParseBytes(data) 64 if err != nil { 65 handyPool.Put(p) 66 return 0 67 } 68 n := v.GetInt(keys...) 69 handyPool.Put(p) 70 return n 71 } 72 73 // GetFloat64 returns float64 value for the field identified by keys path 74 // in JSON data. 75 // 76 // Array indexes may be represented as decimal numbers in keys. 77 // 78 // 0 is returned on error. Use Parser for proper error handling. 79 // 80 // Parser is faster for obtaining multiple fields from JSON. 81 func GetFloat64(data []byte, keys ...string) float64 { 82 p := handyPool.Get() 83 v, err := p.ParseBytes(data) 84 if err != nil { 85 handyPool.Put(p) 86 return 0 87 } 88 f := v.GetFloat64(keys...) 89 handyPool.Put(p) 90 return f 91 } 92 93 // GetBool returns boolean value for the field identified by keys path 94 // in JSON data. 95 // 96 // Array indexes may be represented as decimal numbers in keys. 97 // 98 // False is returned on error. Use Parser for proper error handling. 99 // 100 // Parser is faster for obtaining multiple fields from JSON. 101 func GetBool(data []byte, keys ...string) bool { 102 p := handyPool.Get() 103 v, err := p.ParseBytes(data) 104 if err != nil { 105 handyPool.Put(p) 106 return false 107 } 108 b := v.GetBool(keys...) 109 handyPool.Put(p) 110 return b 111 } 112 113 // Exists returns true if the field identified by keys path exists in JSON data. 114 // 115 // Array indexes may be represented as decimal numbers in keys. 116 // 117 // False is returned on error. Use Parser for proper error handling. 118 // 119 // Parser is faster when multiple fields must be checked in the JSON. 120 func Exists(data []byte, keys ...string) bool { 121 p := handyPool.Get() 122 v, err := p.ParseBytes(data) 123 if err != nil { 124 handyPool.Put(p) 125 return false 126 } 127 ok := v.Exists(keys...) 128 handyPool.Put(p) 129 return ok 130 } 131 132 // Parse parses fastjson string s. 133 // 134 // The function is slower than the Parser.Parse for re-used Parser. 135 func Parse(s string) (*Value, error) { 136 var p Parser 137 return p.Parse(s) 138 } 139 140 // MustParse parses fastjson string s. 141 // 142 // The function panics if s cannot be parsed. 143 // The function is slower than the Parser.Parse for re-used Parser. 144 func MustParse(s string) *Value { 145 v, err := Parse(s) 146 if err != nil { 147 panic(err) 148 } 149 return v 150 } 151 152 // ParseBytes parses b containing fastjson. 153 // 154 // The function is slower than the Parser.ParseBytes for re-used Parser. 155 func ParseBytes(b []byte) (*Value, error) { 156 var p Parser 157 return p.ParseBytes(b) 158 } 159 160 // MustParseBytes parses b containing fastjson. 161 // 162 // The function banics if b cannot be parsed. 163 // The function is slower than the Parser.ParseBytes for re-used Parser. 164 func MustParseBytes(b []byte) *Value { 165 v, err := ParseBytes(b) 166 if err != nil { 167 panic(err) 168 } 169 return v 170 }