gjson_api.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. // Copyright GoFrame Author(https://github.com/gogf/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://github.com/gogf/gf.
  6. package gjson
  7. import (
  8. "fmt"
  9. "time"
  10. "github.com/gogf/gf/util/gutil"
  11. "github.com/gogf/gf/container/gvar"
  12. "github.com/gogf/gf/os/gtime"
  13. "github.com/gogf/gf/util/gconv"
  14. )
  15. // Value returns the json value.
  16. func (j *Json) Value() interface{} {
  17. j.mu.RLock()
  18. defer j.mu.RUnlock()
  19. return *(j.p)
  20. }
  21. // IsNil checks whether the value pointed by <j> is nil.
  22. func (j *Json) IsNil() bool {
  23. j.mu.RLock()
  24. defer j.mu.RUnlock()
  25. return j.p == nil || *(j.p) == nil
  26. }
  27. // Get retrieves and returns value by specified <pattern>.
  28. // It returns all values of current Json object if <pattern> is given empty or string ".".
  29. // It returns nil if no value found by <pattern>.
  30. //
  31. // We can also access slice item by its index number in <pattern> like:
  32. // "list.10", "array.0.name", "array.0.1.id".
  33. //
  34. // It returns a default value specified by <def> if value for <pattern> is not found.
  35. func (j *Json) Get(pattern string, def ...interface{}) interface{} {
  36. j.mu.RLock()
  37. defer j.mu.RUnlock()
  38. // It returns nil if pattern is empty.
  39. if pattern == "" {
  40. return nil
  41. }
  42. // It returns all if pattern is ".".
  43. if pattern == "." {
  44. return *j.p
  45. }
  46. var result *interface{}
  47. if j.vc {
  48. result = j.getPointerByPattern(pattern)
  49. } else {
  50. result = j.getPointerByPatternWithoutViolenceCheck(pattern)
  51. }
  52. if result != nil {
  53. return *result
  54. }
  55. if len(def) > 0 {
  56. return def[0]
  57. }
  58. return nil
  59. }
  60. // GetVar returns a gvar.Var with value by given <pattern>.
  61. func (j *Json) GetVar(pattern string, def ...interface{}) *gvar.Var {
  62. return gvar.New(j.Get(pattern, def...))
  63. }
  64. // GetVars returns []*gvar.Var with value by given <pattern>.
  65. func (j *Json) GetVars(pattern string, def ...interface{}) []*gvar.Var {
  66. return gvar.New(j.Get(pattern, def...)).Vars()
  67. }
  68. // GetMap retrieves and returns the value by specified <pattern> as map[string]interface{}.
  69. func (j *Json) GetMap(pattern string, def ...interface{}) map[string]interface{} {
  70. result := j.Get(pattern, def...)
  71. if result != nil {
  72. return gconv.Map(result)
  73. }
  74. return nil
  75. }
  76. // GetMapStrStr retrieves and returns the value by specified <pattern> as map[string]string.
  77. func (j *Json) GetMapStrStr(pattern string, def ...interface{}) map[string]string {
  78. result := j.Get(pattern, def...)
  79. if result != nil {
  80. return gconv.MapStrStr(result)
  81. }
  82. return nil
  83. }
  84. // GetMaps retrieves and returns the value by specified <pattern> as []map[string]interface{}.
  85. func (j *Json) GetMaps(pattern string, def ...interface{}) []map[string]interface{} {
  86. result := j.Get(pattern, def...)
  87. if result != nil {
  88. return gconv.Maps(result)
  89. }
  90. return nil
  91. }
  92. // GetJson gets the value by specified <pattern>,
  93. // and converts it to a un-concurrent-safe Json object.
  94. func (j *Json) GetJson(pattern string, def ...interface{}) *Json {
  95. return New(j.Get(pattern, def...))
  96. }
  97. // GetJsons gets the value by specified <pattern>,
  98. // and converts it to a slice of un-concurrent-safe Json object.
  99. func (j *Json) GetJsons(pattern string, def ...interface{}) []*Json {
  100. array := j.GetArray(pattern, def...)
  101. if len(array) > 0 {
  102. jsonSlice := make([]*Json, len(array))
  103. for i := 0; i < len(array); i++ {
  104. jsonSlice[i] = New(array[i])
  105. }
  106. return jsonSlice
  107. }
  108. return nil
  109. }
  110. // GetJsonMap gets the value by specified <pattern>,
  111. // and converts it to a map of un-concurrent-safe Json object.
  112. func (j *Json) GetJsonMap(pattern string, def ...interface{}) map[string]*Json {
  113. m := j.GetMap(pattern, def...)
  114. if len(m) > 0 {
  115. jsonMap := make(map[string]*Json, len(m))
  116. for k, v := range m {
  117. jsonMap[k] = New(v)
  118. }
  119. return jsonMap
  120. }
  121. return nil
  122. }
  123. // GetArray retrieves the value by specified <pattern>,
  124. // and converts it to a slice of []interface{}.
  125. func (j *Json) GetArray(pattern string, def ...interface{}) []interface{} {
  126. return gconv.Interfaces(j.Get(pattern, def...))
  127. }
  128. // GetString retrieves the value by specified <pattern> and converts it to string.
  129. func (j *Json) GetString(pattern string, def ...interface{}) string {
  130. return gconv.String(j.Get(pattern, def...))
  131. }
  132. // GetBytes retrieves the value by specified <pattern> and converts it to []byte.
  133. func (j *Json) GetBytes(pattern string, def ...interface{}) []byte {
  134. return gconv.Bytes(j.Get(pattern, def...))
  135. }
  136. // GetBool retrieves the value by specified <pattern>,
  137. // converts and returns it as bool.
  138. // It returns false when value is: "", 0, false, off, nil;
  139. // or returns true instead.
  140. func (j *Json) GetBool(pattern string, def ...interface{}) bool {
  141. return gconv.Bool(j.Get(pattern, def...))
  142. }
  143. // GetInt retrieves the value by specified <pattern> and converts it to int.
  144. func (j *Json) GetInt(pattern string, def ...interface{}) int {
  145. return gconv.Int(j.Get(pattern, def...))
  146. }
  147. // GetInt8 retrieves the value by specified <pattern> and converts it to int8.
  148. func (j *Json) GetInt8(pattern string, def ...interface{}) int8 {
  149. return gconv.Int8(j.Get(pattern, def...))
  150. }
  151. // GetInt16 retrieves the value by specified <pattern> and converts it to int16.
  152. func (j *Json) GetInt16(pattern string, def ...interface{}) int16 {
  153. return gconv.Int16(j.Get(pattern, def...))
  154. }
  155. // GetInt32 retrieves the value by specified <pattern> and converts it to int32.
  156. func (j *Json) GetInt32(pattern string, def ...interface{}) int32 {
  157. return gconv.Int32(j.Get(pattern, def...))
  158. }
  159. // GetInt64 retrieves the value by specified <pattern> and converts it to int64.
  160. func (j *Json) GetInt64(pattern string, def ...interface{}) int64 {
  161. return gconv.Int64(j.Get(pattern, def...))
  162. }
  163. // GetUint retrieves the value by specified <pattern> and converts it to uint.
  164. func (j *Json) GetUint(pattern string, def ...interface{}) uint {
  165. return gconv.Uint(j.Get(pattern, def...))
  166. }
  167. // GetUint8 retrieves the value by specified <pattern> and converts it to uint8.
  168. func (j *Json) GetUint8(pattern string, def ...interface{}) uint8 {
  169. return gconv.Uint8(j.Get(pattern, def...))
  170. }
  171. // GetUint16 retrieves the value by specified <pattern> and converts it to uint16.
  172. func (j *Json) GetUint16(pattern string, def ...interface{}) uint16 {
  173. return gconv.Uint16(j.Get(pattern, def...))
  174. }
  175. // GetUint32 retrieves the value by specified <pattern> and converts it to uint32.
  176. func (j *Json) GetUint32(pattern string, def ...interface{}) uint32 {
  177. return gconv.Uint32(j.Get(pattern, def...))
  178. }
  179. // GetUint64 retrieves the value by specified <pattern> and converts it to uint64.
  180. func (j *Json) GetUint64(pattern string, def ...interface{}) uint64 {
  181. return gconv.Uint64(j.Get(pattern, def...))
  182. }
  183. // GetFloat32 retrieves the value by specified <pattern> and converts it to float32.
  184. func (j *Json) GetFloat32(pattern string, def ...interface{}) float32 {
  185. return gconv.Float32(j.Get(pattern, def...))
  186. }
  187. // GetFloat64 retrieves the value by specified <pattern> and converts it to float64.
  188. func (j *Json) GetFloat64(pattern string, def ...interface{}) float64 {
  189. return gconv.Float64(j.Get(pattern, def...))
  190. }
  191. // GetFloats retrieves the value by specified <pattern> and converts it to []float64.
  192. func (j *Json) GetFloats(pattern string, def ...interface{}) []float64 {
  193. return gconv.Floats(j.Get(pattern, def...))
  194. }
  195. // GetInts retrieves the value by specified <pattern> and converts it to []int.
  196. func (j *Json) GetInts(pattern string, def ...interface{}) []int {
  197. return gconv.Ints(j.Get(pattern, def...))
  198. }
  199. // GetStrings retrieves the value by specified <pattern> and converts it to []string.
  200. func (j *Json) GetStrings(pattern string, def ...interface{}) []string {
  201. return gconv.Strings(j.Get(pattern, def...))
  202. }
  203. // GetInterfaces is alias of GetArray.
  204. // See GetArray.
  205. func (j *Json) GetInterfaces(pattern string, def ...interface{}) []interface{} {
  206. return gconv.Interfaces(j.Get(pattern, def...))
  207. }
  208. // GetTime retrieves the value by specified <pattern> and converts it to time.Time.
  209. func (j *Json) GetTime(pattern string, format ...string) time.Time {
  210. return gconv.Time(j.Get(pattern), format...)
  211. }
  212. // GetDuration retrieves the value by specified <pattern> and converts it to time.Duration.
  213. func (j *Json) GetDuration(pattern string, def ...interface{}) time.Duration {
  214. return gconv.Duration(j.Get(pattern, def...))
  215. }
  216. // GetGTime retrieves the value by specified <pattern> and converts it to *gtime.Time.
  217. func (j *Json) GetGTime(pattern string, format ...string) *gtime.Time {
  218. return gconv.GTime(j.Get(pattern), format...)
  219. }
  220. // Set sets value with specified <pattern>.
  221. // It supports hierarchical data access by char separator, which is '.' in default.
  222. func (j *Json) Set(pattern string, value interface{}) error {
  223. return j.setValue(pattern, value, false)
  224. }
  225. // Remove deletes value with specified <pattern>.
  226. // It supports hierarchical data access by char separator, which is '.' in default.
  227. func (j *Json) Remove(pattern string) error {
  228. return j.setValue(pattern, nil, true)
  229. }
  230. // Contains checks whether the value by specified <pattern> exist.
  231. func (j *Json) Contains(pattern string) bool {
  232. return j.Get(pattern) != nil
  233. }
  234. // Len returns the length/size of the value by specified <pattern>.
  235. // The target value by <pattern> should be type of slice or map.
  236. // It returns -1 if the target value is not found, or its type is invalid.
  237. func (j *Json) Len(pattern string) int {
  238. p := j.getPointerByPattern(pattern)
  239. if p != nil {
  240. switch (*p).(type) {
  241. case map[string]interface{}:
  242. return len((*p).(map[string]interface{}))
  243. case []interface{}:
  244. return len((*p).([]interface{}))
  245. default:
  246. return -1
  247. }
  248. }
  249. return -1
  250. }
  251. // Append appends value to the value by specified <pattern>.
  252. // The target value by <pattern> should be type of slice.
  253. func (j *Json) Append(pattern string, value interface{}) error {
  254. p := j.getPointerByPattern(pattern)
  255. if p == nil {
  256. return j.Set(fmt.Sprintf("%s.0", pattern), value)
  257. }
  258. switch (*p).(type) {
  259. case []interface{}:
  260. return j.Set(fmt.Sprintf("%s.%d", pattern, len((*p).([]interface{}))), value)
  261. }
  262. return fmt.Errorf("invalid variable type of %s", pattern)
  263. }
  264. // GetStruct retrieves the value by specified <pattern> and converts it to specified object
  265. // <pointer>. The <pointer> should be the pointer to an object.
  266. func (j *Json) GetStruct(pattern string, pointer interface{}, mapping ...map[string]string) error {
  267. return gconv.Struct(j.Get(pattern), pointer, mapping...)
  268. }
  269. // GetStructDeep does GetStruct recursively.
  270. // Deprecated, use GetStruct instead.
  271. func (j *Json) GetStructDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
  272. return gconv.StructDeep(j.Get(pattern), pointer, mapping...)
  273. }
  274. // GetStructs converts any slice to given struct slice.
  275. func (j *Json) GetStructs(pattern string, pointer interface{}, mapping ...map[string]string) error {
  276. return gconv.Structs(j.Get(pattern), pointer, mapping...)
  277. }
  278. // GetStructsDeep converts any slice to given struct slice recursively.
  279. // Deprecated, use GetStructs instead.
  280. func (j *Json) GetStructsDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
  281. return gconv.StructsDeep(j.Get(pattern), pointer, mapping...)
  282. }
  283. // GetScan automatically calls Struct or Structs function according to the type of parameter
  284. // <pointer> to implement the converting..
  285. func (j *Json) GetScan(pattern string, pointer interface{}, mapping ...map[string]string) error {
  286. return gconv.Scan(j.Get(pattern), pointer, mapping...)
  287. }
  288. // GetScanDeep automatically calls StructDeep or StructsDeep function according to the type of
  289. // parameter <pointer> to implement the converting..
  290. func (j *Json) GetScanDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
  291. return gconv.ScanDeep(j.Get(pattern), pointer, mapping...)
  292. }
  293. // GetMapToMap retrieves the value by specified <pattern> and converts it to specified map variable.
  294. // See gconv.MapToMap.
  295. func (j *Json) GetMapToMap(pattern string, pointer interface{}, mapping ...map[string]string) error {
  296. return gconv.MapToMap(j.Get(pattern), pointer, mapping...)
  297. }
  298. // GetMapToMapDeep retrieves the value by specified <pattern> and converts it to specified map
  299. // variable recursively.
  300. // See gconv.MapToMapDeep.
  301. func (j *Json) GetMapToMapDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
  302. return gconv.MapToMapDeep(j.Get(pattern), pointer, mapping...)
  303. }
  304. // GetMapToMaps retrieves the value by specified <pattern> and converts it to specified map slice
  305. // variable.
  306. // See gconv.MapToMaps.
  307. func (j *Json) GetMapToMaps(pattern string, pointer interface{}, mapping ...map[string]string) error {
  308. return gconv.MapToMaps(j.Get(pattern), pointer, mapping...)
  309. }
  310. // GetMapToMapsDeep retrieves the value by specified <pattern> and converts it to specified map slice
  311. // variable recursively.
  312. // See gconv.MapToMapsDeep.
  313. func (j *Json) GetMapToMapsDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
  314. return gconv.MapToMapsDeep(j.Get(pattern), pointer, mapping...)
  315. }
  316. // Map converts current Json object to map[string]interface{}.
  317. // It returns nil if fails.
  318. func (j *Json) Map() map[string]interface{} {
  319. j.mu.RLock()
  320. defer j.mu.RUnlock()
  321. return gconv.Map(*(j.p))
  322. }
  323. // Array converts current Json object to []interface{}.
  324. // It returns nil if fails.
  325. func (j *Json) Array() []interface{} {
  326. j.mu.RLock()
  327. defer j.mu.RUnlock()
  328. return gconv.Interfaces(*(j.p))
  329. }
  330. // Struct converts current Json object to specified object.
  331. // The <pointer> should be a pointer type of *struct.
  332. func (j *Json) Struct(pointer interface{}, mapping ...map[string]string) error {
  333. j.mu.RLock()
  334. defer j.mu.RUnlock()
  335. return gconv.Struct(*(j.p), pointer, mapping...)
  336. }
  337. // Structs converts current Json object to specified object slice.
  338. // The <pointer> should be a pointer type of []struct/*struct.
  339. func (j *Json) Structs(pointer interface{}, mapping ...map[string]string) error {
  340. j.mu.RLock()
  341. defer j.mu.RUnlock()
  342. return gconv.Structs(*(j.p), pointer, mapping...)
  343. }
  344. // Scan automatically calls Struct or Structs function according to the type of parameter
  345. // <pointer> to implement the converting..
  346. func (j *Json) Scan(pointer interface{}, mapping ...map[string]string) error {
  347. return gconv.Scan(*(j.p), pointer, mapping...)
  348. }
  349. // MapToMap converts current Json object to specified map variable.
  350. // The parameter of <pointer> should be type of *map.
  351. func (j *Json) MapToMap(pointer interface{}, mapping ...map[string]string) error {
  352. j.mu.RLock()
  353. defer j.mu.RUnlock()
  354. return gconv.MapToMap(*(j.p), pointer, mapping...)
  355. }
  356. // MapToMaps converts current Json object to specified map variable slice.
  357. // The parameter of <pointer> should be type of []map/*map.
  358. func (j *Json) MapToMaps(pointer interface{}, mapping ...map[string]string) error {
  359. j.mu.RLock()
  360. defer j.mu.RUnlock()
  361. return gconv.MapToMaps(*(j.p), pointer, mapping...)
  362. }
  363. // Dump prints current Json object with more manually readable.
  364. func (j *Json) Dump() {
  365. j.mu.RLock()
  366. defer j.mu.RUnlock()
  367. gutil.Dump(*j.p)
  368. }
  369. // Export returns <j> as a string with more manually readable.
  370. func (j *Json) Export() string {
  371. j.mu.RLock()
  372. defer j.mu.RUnlock()
  373. return gutil.Export(*j.p)
  374. }