reflect.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. "reflect"
  17. "yunion.io/x/pkg/errors"
  18. "yunion.io/x/pkg/gotypes"
  19. )
  20. var (
  21. JSONDictType reflect.Type
  22. JSONArrayType reflect.Type
  23. JSONStringType reflect.Type
  24. JSONIntType reflect.Type
  25. JSONFloatType reflect.Type
  26. JSONBoolType reflect.Type
  27. JSONDictPtrType reflect.Type
  28. JSONArrayPtrType reflect.Type
  29. JSONStringPtrType reflect.Type
  30. JSONIntPtrType reflect.Type
  31. JSONFloatPtrType reflect.Type
  32. JSONBoolPtrType reflect.Type
  33. JSONObjectType reflect.Type
  34. )
  35. func init() {
  36. JSONDictType = reflect.TypeOf(JSONDict{})
  37. JSONArrayType = reflect.TypeOf(JSONArray{})
  38. JSONStringType = reflect.TypeOf(JSONString{})
  39. JSONIntType = reflect.TypeOf(JSONInt{})
  40. JSONFloatType = reflect.TypeOf(JSONFloat{})
  41. JSONBoolType = reflect.TypeOf(JSONBool{})
  42. JSONDictPtrType = reflect.TypeOf(&JSONDict{})
  43. JSONArrayPtrType = reflect.TypeOf(&JSONArray{})
  44. JSONStringPtrType = reflect.TypeOf(&JSONString{})
  45. JSONIntPtrType = reflect.TypeOf(&JSONInt{})
  46. JSONFloatPtrType = reflect.TypeOf(&JSONFloat{})
  47. JSONBoolPtrType = reflect.TypeOf(&JSONBool{})
  48. JSONObjectType = reflect.TypeOf((*JSONObject)(nil)).Elem()
  49. gotypes.RegisterSerializable(JSONObjectType, func() gotypes.ISerializable {
  50. return nil
  51. })
  52. gotypes.RegisterSerializable(JSONDictPtrType, func() gotypes.ISerializable {
  53. return NewDict()
  54. })
  55. gotypes.RegisterSerializable(JSONArrayPtrType, func() gotypes.ISerializable {
  56. return NewArray()
  57. })
  58. }
  59. func JSONDeserialize(objType reflect.Type, strVal string) (gotypes.ISerializable, error) {
  60. objPtr, err := gotypes.NewSerializable(objType)
  61. if err != nil {
  62. return nil, errors.Wrap(err, "gotypes.NewSerializable")
  63. }
  64. json, err := ParseString(strVal)
  65. if err != nil {
  66. return nil, errors.Wrap(err, "ParseString")
  67. }
  68. if objPtr == nil {
  69. return json, nil
  70. }
  71. err = json.Unmarshal(objPtr)
  72. if err != nil {
  73. return nil, errors.Wrap(err, "json.Unmarshal")
  74. }
  75. objPtr = gotypes.Transform(objType, objPtr)
  76. return objPtr, nil
  77. }