gjson_api.go 14 KB

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