github.com/zhongdalu/gf@v1.0.0/g/encoding/gparser/gparser_api.go (about) 1 // Copyright 2017 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://gitee.com/johng/gp. 6 7 package gparser 8 9 import ( 10 "time" 11 12 "github.com/zhongdalu/gf/g/container/gvar" 13 "github.com/zhongdalu/gf/g/os/gtime" 14 ) 15 16 // Val returns the value. 17 func (p *Parser) Value() interface{} { 18 return p.json.Value() 19 } 20 21 // Get returns value by specified <pattern>. 22 // It returns all values of current Json object, if <pattern> is empty or not specified. 23 // It returns nil if no value found by <pattern>. 24 // 25 // We can also access slice item by its index number in <pattern>, 26 // eg: "items.name.first", "list.10". 27 // 28 // It returns a default value specified by <def> if value for <pattern> is not found. 29 func (p *Parser) Get(pattern string, def ...interface{}) interface{} { 30 return p.json.Get(pattern, def...) 31 } 32 33 // GetVar returns a *gvar.Var with value by given <pattern>. 34 func (p *Parser) GetVar(pattern string, def ...interface{}) *gvar.Var { 35 return p.json.GetVar(pattern, def...) 36 } 37 38 // GetMap gets the value by specified <pattern>, 39 // and converts it to map[string]interface{}. 40 func (p *Parser) GetMap(pattern string, def ...interface{}) map[string]interface{} { 41 return p.json.GetMap(pattern, def...) 42 } 43 44 // GetArray gets the value by specified <pattern>, 45 // and converts it to a slice of []interface{}. 46 func (p *Parser) GetArray(pattern string, def ...interface{}) []interface{} { 47 return p.json.GetArray(pattern, def...) 48 } 49 50 // GetString gets the value by specified <pattern>, 51 // and converts it to string. 52 func (p *Parser) GetString(pattern string, def ...interface{}) string { 53 return p.json.GetString(pattern, def...) 54 } 55 56 // GetBool gets the value by specified <pattern>, 57 // and converts it to bool. 58 // It returns false when value is: "", 0, false, off, nil; 59 // or returns true instead. 60 func (p *Parser) GetBool(pattern string, def ...interface{}) bool { 61 return p.json.GetBool(pattern, def...) 62 } 63 64 func (p *Parser) GetInt(pattern string, def ...interface{}) int { 65 return p.json.GetInt(pattern, def...) 66 } 67 68 func (p *Parser) GetInt8(pattern string, def ...interface{}) int8 { 69 return p.json.GetInt8(pattern, def...) 70 } 71 72 func (p *Parser) GetInt16(pattern string, def ...interface{}) int16 { 73 return p.json.GetInt16(pattern, def...) 74 } 75 76 func (p *Parser) GetInt32(pattern string, def ...interface{}) int32 { 77 return p.json.GetInt32(pattern, def...) 78 } 79 80 func (p *Parser) GetInt64(pattern string, def ...interface{}) int64 { 81 return p.json.GetInt64(pattern, def...) 82 } 83 84 func (p *Parser) GetInts(pattern string, def ...interface{}) []int { 85 return p.json.GetInts(pattern, def...) 86 } 87 88 func (p *Parser) GetUint(pattern string, def ...interface{}) uint { 89 return p.json.GetUint(pattern, def...) 90 } 91 92 func (p *Parser) GetUint8(pattern string, def ...interface{}) uint8 { 93 return p.json.GetUint8(pattern, def...) 94 } 95 96 func (p *Parser) GetUint16(pattern string, def ...interface{}) uint16 { 97 return p.json.GetUint16(pattern, def...) 98 } 99 100 func (p *Parser) GetUint32(pattern string, def ...interface{}) uint32 { 101 return p.json.GetUint32(pattern, def...) 102 } 103 104 func (p *Parser) GetUint64(pattern string, def ...interface{}) uint64 { 105 return p.json.GetUint64(pattern, def...) 106 } 107 108 func (p *Parser) GetFloat32(pattern string, def ...interface{}) float32 { 109 return p.json.GetFloat32(pattern, def...) 110 } 111 112 func (p *Parser) GetFloat64(pattern string, def ...interface{}) float64 { 113 return p.json.GetFloat64(pattern, def...) 114 } 115 116 func (p *Parser) GetFloats(pattern string, def ...interface{}) []float64 { 117 return p.json.GetFloats(pattern, def...) 118 } 119 120 // GetStrings gets the value by specified <pattern>, 121 // and converts it to a slice of []string. 122 func (p *Parser) GetStrings(pattern string, def ...interface{}) []string { 123 return p.json.GetStrings(pattern, def...) 124 } 125 126 func (p *Parser) GetInterfaces(pattern string, def ...interface{}) []interface{} { 127 return p.json.GetInterfaces(pattern, def...) 128 } 129 130 func (p *Parser) GetTime(pattern string, format ...string) time.Time { 131 return p.json.GetTime(pattern, format...) 132 } 133 134 func (p *Parser) GetDuration(pattern string, def ...interface{}) time.Duration { 135 return p.json.GetDuration(pattern, def...) 136 } 137 138 func (p *Parser) GetGTime(pattern string, format ...string) *gtime.Time { 139 return p.json.GetGTime(pattern, format...) 140 } 141 142 // GetToVar gets the value by specified <pattern>, 143 // and converts it to specified golang variable <v>. 144 // The <v> should be a pointer type. 145 // Deprecated. 146 func (p *Parser) GetToVar(pattern string, pointer interface{}) error { 147 return p.json.GetToVar(pattern, pointer) 148 } 149 150 // GetStruct gets the value by specified <pattern>, 151 // and converts it to specified object <pointer>. 152 // The <pointer> should be the pointer to an object. 153 func (p *Parser) GetStruct(pattern string, pointer interface{}, mapping ...map[string]string) error { 154 return p.json.GetStruct(pattern, pointer, mapping...) 155 } 156 157 // GetStructDeep does GetStruct recursively. 158 func (p *Parser) GetStructDeep(pattern string, pointer interface{}, mapping ...map[string]string) error { 159 return p.json.GetStructDeep(pattern, pointer, mapping...) 160 } 161 162 // GetStructs converts any slice to given struct slice. 163 func (p *Parser) GetStructs(pattern string, pointer interface{}, mapping ...map[string]string) error { 164 return p.json.GetStructs(pattern, pointer, mapping...) 165 } 166 167 // GetStructsDeep converts any slice to given struct slice recursively. 168 func (p *Parser) GetStructsDeep(pattern string, pointer interface{}, mapping ...map[string]string) error { 169 return p.json.GetStructsDeep(pattern, pointer, mapping...) 170 } 171 172 // GetToStruct is alias of GetStruct. 173 // Deprecated. 174 func (p *Parser) GetToStruct(pattern string, pointer interface{}, mapping ...map[string]string) error { 175 return p.json.GetStruct(pattern, pointer, mapping...) 176 } 177 178 // Set sets value with specified <pattern>. 179 // It supports hierarchical data access by char separator, which is '.' in default. 180 func (p *Parser) Set(pattern string, value interface{}) error { 181 return p.json.Set(pattern, value) 182 } 183 184 // Len returns the length/size of the value by specified <pattern>. 185 // The target value by <pattern> should be type of slice or map. 186 // It returns -1 if the target value is not found, or its type is invalid. 187 func (p *Parser) Len(pattern string) int { 188 return p.json.Len(pattern) 189 } 190 191 // Append appends value to the value by specified <pattern>. 192 // The target value by <pattern> should be type of slice. 193 func (p *Parser) Append(pattern string, value interface{}) error { 194 return p.json.Append(pattern, value) 195 } 196 197 // Remove deletes value with specified <pattern>. 198 // It supports hierarchical data access by char separator, which is '.' in default. 199 func (p *Parser) Remove(pattern string) error { 200 return p.json.Remove(pattern) 201 } 202 203 // ToMap converts current object values to map[string]interface{}. 204 // It returns nil if fails. 205 func (p *Parser) ToMap() map[string]interface{} { 206 return p.json.ToMap() 207 } 208 209 // ToArray converts current object values to []interface{}. 210 // It returns nil if fails. 211 func (p *Parser) ToArray() []interface{} { 212 return p.json.ToArray() 213 } 214 215 // ToStruct converts current Json object to specified object. 216 // The <objPointer> should be a pointer type. 217 func (p *Parser) ToStruct(pointer interface{}) error { 218 return p.json.ToStruct(pointer) 219 } 220 221 func (p *Parser) ToStructDeep(pointer interface{}) error { 222 return p.json.ToStructDeep(pointer) 223 } 224 225 func (p *Parser) ToStructs(pointer interface{}) error { 226 return p.json.ToStructs(pointer) 227 } 228 229 func (p *Parser) ToStructsDeep(pointer interface{}) error { 230 return p.json.ToStructsDeep(pointer) 231 } 232 233 // Dump prints current Json object with more manually readable. 234 func (p *Parser) Dump() { 235 p.json.Dump() 236 } 237 238 func (p *Parser) Export() string { 239 return p.json.Export() 240 }