jsonpointer.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package jsonutils
  15. import (
  16. "fmt"
  17. "reflect"
  18. "strings"
  19. "time"
  20. )
  21. const (
  22. jsonPointerKey = "___jnid_"
  23. )
  24. type sJSONPointer struct {
  25. node JSONObject
  26. nodeId int
  27. }
  28. var _ JSONObject = (*sJSONPointer)(nil)
  29. func (s *sJsonMarshalSession) newJsonPointer(inf interface{}) *sJSONPointer {
  30. s.nodeIndex++
  31. jsonPtr := &sJSONPointer{
  32. nodeId: s.nodeIndex,
  33. }
  34. s.nodeMap[s.nodeIndex] = s.objectTrace.push(inf, jsonPtr)
  35. return jsonPtr
  36. }
  37. func (s *sJsonMarshalSession) setJsonObject(ptr *sJSONPointer, obj JSONObject) {
  38. if _, ok := obj.(*JSONDict); ok {
  39. ptr.node = obj
  40. }
  41. s.objectTrace.pop()
  42. }
  43. func (s *sJsonMarshalSession) setAllNodeId() {
  44. for nodeId, jsonPtrNode := range s.nodeMap {
  45. if jsonPtrNode.refcnt > 1 && jsonPtrNode.pointer.node != nil {
  46. jsonPtrNode.pointer.node.(*JSONDict).setNodeId(nodeId)
  47. }
  48. }
  49. }
  50. func (ptr *sJSONPointer) String() string {
  51. return fmt.Sprintf("<%d>", ptr.nodeId)
  52. }
  53. func (ptr *sJSONPointer) PrettyString() string {
  54. return ptr.String()
  55. }
  56. func (ptr *sJSONPointer) buildString(sb *strings.Builder) {
  57. sb.WriteString(ptr.String())
  58. }
  59. func (dict *JSONDict) setNodeId(nodeId int) {
  60. dict.nodeId = nodeId
  61. }
  62. func (ptr *sJSONPointer) unmarshalValue(s *sJsonUnmarshalSession, val reflect.Value) error {
  63. return s.setPointerValue(ptr.nodeId, val)
  64. }
  65. func (ptr *sJSONPointer) IsZero() bool {
  66. if ptr.node != nil {
  67. return ptr.node.IsZero()
  68. }
  69. return true
  70. }
  71. func (ptr *sJSONPointer) parse(s *sJsonParseSession, str []byte, offset int) (int, error) {
  72. // null ops
  73. return -1, nil
  74. }
  75. func (ptr *sJSONPointer) prettyString(level int) string {
  76. return jsonPrettyString(ptr, level)
  77. }
  78. func (ptr *sJSONPointer) YAMLString() string {
  79. return ptr.String()
  80. }
  81. func (ptr *sJSONPointer) QueryString() string {
  82. return ""
  83. }
  84. func (ptr *sJSONPointer) _queryString(key string) string {
  85. return ""
  86. }
  87. func (ptr *sJSONPointer) getNode() JSONObject {
  88. if ptr.node != nil {
  89. return ptr.node
  90. }
  91. return JSONNull
  92. }
  93. func (ptr *sJSONPointer) Contains(keys ...string) bool {
  94. return ptr.getNode().Contains(keys...)
  95. }
  96. func (ptr *sJSONPointer) ContainsIgnoreCases(keys ...string) bool {
  97. return ptr.getNode().ContainsIgnoreCases(keys...)
  98. }
  99. func (ptr *sJSONPointer) Get(keys ...string) (JSONObject, error) {
  100. return ptr.getNode().Get(keys...)
  101. }
  102. func (ptr *sJSONPointer) GetIgnoreCases(keys ...string) (JSONObject, error) {
  103. return ptr.getNode().GetIgnoreCases(keys...)
  104. }
  105. func (ptr *sJSONPointer) GetAt(i int, keys ...string) (JSONObject, error) {
  106. return ptr.getNode().GetAt(i, keys...)
  107. }
  108. func (ptr *sJSONPointer) Int(keys ...string) (int64, error) {
  109. return ptr.getNode().Int(keys...)
  110. }
  111. func (ptr *sJSONPointer) Float(keys ...string) (float64, error) {
  112. return ptr.getNode().Float(keys...)
  113. }
  114. func (ptr *sJSONPointer) Bool(keys ...string) (bool, error) {
  115. return ptr.getNode().Bool(keys...)
  116. }
  117. func (ptr *sJSONPointer) GetMap(keys ...string) (map[string]JSONObject, error) {
  118. return ptr.getNode().GetMap(keys...)
  119. }
  120. func (ptr *sJSONPointer) GetArray(keys ...string) ([]JSONObject, error) {
  121. return ptr.getNode().GetArray(keys...)
  122. }
  123. func (ptr *sJSONPointer) GetTime(keys ...string) (time.Time, error) {
  124. return ptr.getNode().GetTime(keys...)
  125. }
  126. func (ptr *sJSONPointer) GetString(keys ...string) (string, error) {
  127. return ptr.getNode().GetString(keys...)
  128. }
  129. func (ptr *sJSONPointer) Unmarshal(obj interface{}, keys ...string) error {
  130. return ptr.getNode().Unmarshal(obj, keys...)
  131. }
  132. func (ptr *sJSONPointer) Equals(obj JSONObject) bool {
  133. return ptr.getNode().Equals(obj)
  134. }
  135. func (ptr *sJSONPointer) Interface() interface{} {
  136. return ptr.getNode().Interface()
  137. }
  138. func (ptr *sJSONPointer) isCompond() bool {
  139. return ptr.getNode().isCompond()
  140. }