std_json.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. "encoding/json"
  17. "fmt"
  18. "reflect"
  19. "yunion.io/x/pkg/gotypes"
  20. )
  21. func tryStdUnmarshal(s *sJsonUnmarshalSession, jo JSONObject, v reflect.Value, unmarshalFunc func(s *sJsonUnmarshalSession, value reflect.Value) error) error {
  22. u := IsImplementStdUnmarshaler(v)
  23. if u != nil {
  24. return u.UnmarshalJSON([]byte(jo.String()))
  25. }
  26. return unmarshalFunc(s, v)
  27. }
  28. func IsImplementStdUnmarshaler(v reflect.Value) json.Unmarshaler {
  29. return indirectStdUnmarshaler(v)
  30. }
  31. func indirectStdUnmarshaler(v reflect.Value) json.Unmarshaler {
  32. v0 := v
  33. haveAddr := false
  34. // If v is a named type and is addressable,
  35. // start with its address, so that if the type has pointer methods,
  36. // we find them
  37. if v.Kind() != reflect.Ptr && v.Type().Name() != "" && v.CanAddr() {
  38. haveAddr = true
  39. v = v.Addr()
  40. }
  41. for {
  42. // Load value from interface, but only if the result will be
  43. // usefully addressable.
  44. if v.Kind() == reflect.Interface && !v.IsNil() {
  45. e := v.Elem()
  46. if e.Kind() == reflect.Ptr && !e.IsNil() && e.Elem().Kind() == reflect.Ptr {
  47. haveAddr = false
  48. v = e
  49. continue
  50. }
  51. }
  52. if v.Kind() != reflect.Ptr {
  53. break
  54. }
  55. // Prevent infinite loop if v is an interface pointing to its own address:
  56. // var v interface{}
  57. // v = &v
  58. if v.Elem().Kind() == reflect.Interface && v.Elem().Elem() == v {
  59. v = v.Elem()
  60. break
  61. }
  62. if v.IsNil() {
  63. v.Set(reflect.New(v.Type().Elem()))
  64. }
  65. if v.Type().NumMethod() > 0 && v.CanInterface() {
  66. if u, ok := v.Interface().(json.Unmarshaler); ok {
  67. return u
  68. }
  69. }
  70. if haveAddr {
  71. v = v0 // restore original value after round-trip Value.Addr().Elem()
  72. haveAddr = false
  73. } else {
  74. v = v.Elem()
  75. }
  76. }
  77. return nil
  78. }
  79. func IsImplementStdMarshaler(v reflect.Value) json.Marshaler {
  80. return indirectStdMarshaler(v)
  81. }
  82. func indirectStdMarshaler(v reflect.Value) json.Marshaler {
  83. if v.Kind() == reflect.Ptr && v.IsNil() {
  84. return nil
  85. }
  86. if v.Type().NumMethod() > 0 && v.CanInterface() {
  87. if u, ok := v.Interface().(json.Marshaler); ok {
  88. return u
  89. }
  90. }
  91. return nil
  92. }
  93. func tryStdMarshal(v reflect.Value, marshalFunc func(v reflect.Value) JSONObject) JSONObject {
  94. if v.Type() != gotypes.TimeType {
  95. m := IsImplementStdMarshaler(v)
  96. if m != nil {
  97. data, err := m.MarshalJSON()
  98. if err != nil {
  99. panic(fmt.Sprintf("MarshalJSON of %q error: %v", v.String(), err))
  100. }
  101. jo, err := Parse(data)
  102. if err != nil {
  103. panic(fmt.Sprintf("Parse data %q to json of %q error: %v", data, v.String(), err))
  104. }
  105. return jo
  106. }
  107. }
  108. return marshalFunc(v)
  109. }