使用Go反射进行复杂对象的反序列化方法

huangapple go评论120阅读模式
英文:

Unmarshal method for complex object with go reflections

问题

我正在开始编写更复杂的Go代码,我的目标是将JSON对象的列表转换为具有特定键的映射。这个操作可以帮助我加快算法的速度。但是我现在遇到了一个问题,我的容器结构体中有几个复杂的JSON字段,我无法编写一个通用的解决方案来实现这个操作。我目前想到的唯一方法是使用一个大的switch case语句,但我认为这不是正确的解决方案。

以下是我目前的代码,其中statusChannel在代码中是一个映射,但在JSON字符串中是一个列表:

  1. type MetricOne struct {
  2. // Internal id to identify the metric
  3. id int `json:"-"`
  4. // Version of metrics format, it is used to migrate the
  5. // JSON payload from previous version of plugin.
  6. Version int `json:"version"`
  7. // Name of the metrics
  8. Name string `json:"metric_name"`
  9. NodeId string `json:"node_id"`
  10. Color string `json:"color"`
  11. OSInfo *osInfo `json:"os_info"`
  12. // timezone where the node is located
  13. Timezone string `json:"timezone"`
  14. // array of the up_time
  15. UpTime []*status `json:"up_time"`
  16. // map of informatonof channel information
  17. ChannelsInfo map[string]*statusChannel `json:"channels_info"`
  18. }
  19. func (instance *MetricOne) MarshalJSON() ([]byte, error) {
  20. jsonMap := make(map[string]interface{})
  21. reflectType := reflect.TypeOf(*instance)
  22. reflectValue := reflect.ValueOf(*instance)
  23. nFiled := reflectValue.Type().NumField()
  24. for i := 0; i < nFiled; i++ {
  25. key := reflectType.Field(i)
  26. valueFiled := reflectValue.Field(i)
  27. jsonName := key.Tag.Get("json")
  28. switch jsonName {
  29. case "-":
  30. // skip
  31. continue
  32. case "channels_info":
  33. // TODO convert the map[string]*statusChannel in a list of statusChannel
  34. statusChannels := make([]*statusChannel, 0)
  35. for _, value := range valueFiled.Interface().(map[string]*statusChannel) {
  36. statusChannels = append(statusChannels, value)
  37. }
  38. jsonMap[jsonName] = statusChannels
  39. default:
  40. jsonMap[jsonName] = valueFiled.Interface()
  41. }
  42. }
  43. return json.Marshal(jsonMap)
  44. }
  45. func (instance *MetricOne) UnmarshalJSON(data []byte) error {
  46. var jsonMap map[string]interface{}
  47. err := json.Unmarshal(data, &jsonMap)
  48. if err != nil {
  49. log.GetInstance().Error(fmt.Sprintf("Error: %s", err))
  50. return err
  51. }
  52. instance.Migrate(jsonMap)
  53. reflectValue := reflect.ValueOf(instance)
  54. reflectStruct := reflectValue.Elem()
  55. // reflectType := reflectValue.Type()
  56. for key, value := range jsonMap {
  57. fieldName, err := utils.GetFieldName(key, "json", *instance)
  58. if err != nil {
  59. log.GetInstance().Info(fmt.Sprintf("Error: %s", err))
  60. if strings.Contains(key, "dev_") {
  61. log.GetInstance().Info("dev propriety skipped if missed")
  62. continue
  63. }
  64. return err
  65. }
  66. field := reflectStruct.FieldByName(*fieldName)
  67. fieldType := field.Type()
  68. filedValue := field.Interface()
  69. val := reflect.ValueOf(filedValue)
  70. switch key {
  71. case "channels_info":
  72. statusChannelsMap := make(map[string]*statusChannel)
  73. toArray := value.([]interface{})
  74. for _, status := range toArray {
  75. var statusType statusChannel
  76. jsonVal, err := json.Marshal(status)
  77. if err != nil {
  78. return err
  79. }
  80. err = json.Unmarshal(jsonVal, &statusType)
  81. if err != nil {
  82. return err
  83. }
  84. statusChannelsMap[statusType.ChannelId] = &statusType
  85. }
  86. field.Set(reflect.ValueOf(statusChannelsMap))
  87. default:
  88. field.Set(val.Convert(fieldType))
  89. }
  90. }
  91. return nil
  92. }

当我解码对象时,我收到以下错误:

  1. panic: reflect.Value.Convert: value of type map[string]interface {} cannot be converted to type *plugin.osInfo [recovered]
  2. panic: reflect.Value.Convert: value of type map[string]interface {} cannot be converted to type *plugin.osInfo

有人能解释一下如何以通用的方式执行此操作吗?

英文:

I'm starting to writing more complex go code, and my object node it to convert a list from a JSON object to a map with a particular key. This operation helps me to speed up my algorithm. But I have a problem now, my container struct has several complex JSON and I'm not able to write a generic solution to achieve a generic solution. The only way that I have in mind is to use a big switch case, but I think this is not the right solution.

This is my code at the moment, where the statusChannel is a map in the code but it is a list in the JSON string

  1. type MetricOne struct {
  2. // Internal id to identify the metric
  3. id int `json:&quot;-&quot;`
  4. // Version of metrics format, it is used to migrate the
  5. // JSON payload from previous version of plugin.
  6. Version int `json:&quot;version&quot;`
  7. // Name of the metrics
  8. Name string `json:&quot;metric_name&quot;`
  9. NodeId string `json:&quot;node_id&quot;`
  10. Color string `json:&quot;color&quot;`
  11. OSInfo *osInfo `json:&quot;os_info&quot;`
  12. // timezone where the node is located
  13. Timezone string `json:&quot;timezone&quot;`
  14. // array of the up_time
  15. UpTime []*status `json:&quot;up_time&quot;`
  16. // map of informatonof channel information
  17. ChannelsInfo map[string]*statusChannel `json:&quot;channels_info&quot;`
  18. }
  19. func (instance *MetricOne) MarshalJSON() ([]byte, error) {
  20. jsonMap := make(map[string]interface{})
  21. reflectType := reflect.TypeOf(*instance)
  22. reflectValue := reflect.ValueOf(*instance)
  23. nFiled := reflectValue.Type().NumField()
  24. for i := 0; i &lt; nFiled; i++ {
  25. key := reflectType.Field(i)
  26. valueFiled := reflectValue.Field(i)
  27. jsonName := key.Tag.Get(&quot;json&quot;)
  28. switch jsonName {
  29. case &quot;-&quot;:
  30. // skip
  31. continue
  32. case &quot;channels_info&quot;:
  33. // TODO convert the map[string]*statusChannel in a list of statusChannel
  34. statusChannels := make([]*statusChannel, 0)
  35. for _, value := range valueFiled.Interface().(map[string]*statusChannel) {
  36. statusChannels = append(statusChannels, value)
  37. }
  38. jsonMap[jsonName] = statusChannels
  39. default:
  40. jsonMap[jsonName] = valueFiled.Interface()
  41. }
  42. }
  43. return json.Marshal(jsonMap)
  44. }
  45. func (instance *MetricOne) UnmarshalJSON(data []byte) error {
  46. var jsonMap map[string]interface{}
  47. err := json.Unmarshal(data, &amp;jsonMap)
  48. if err != nil {
  49. log.GetInstance().Error(fmt.Sprintf(&quot;Error: %s&quot;, err))
  50. return err
  51. }
  52. instance.Migrate(jsonMap)
  53. reflectValue := reflect.ValueOf(instance)
  54. reflectStruct := reflectValue.Elem()
  55. // reflectType := reflectValue.Type()
  56. for key, value := range jsonMap {
  57. fieldName, err := utils.GetFieldName(key, &quot;json&quot;, *instance)
  58. if err != nil {
  59. log.GetInstance().Info(fmt.Sprintf(&quot;Error: %s&quot;, err))
  60. if strings.Contains(key, &quot;dev_&quot;) {
  61. log.GetInstance().Info(&quot;dev propriety skipped if missed&quot;)
  62. continue
  63. }
  64. return err
  65. }
  66. field := reflectStruct.FieldByName(*fieldName)
  67. fieldType := field.Type()
  68. filedValue := field.Interface()
  69. val := reflect.ValueOf(filedValue)
  70. switch key {
  71. case &quot;channels_info&quot;:
  72. statusChannelsMap := make(map[string]*statusChannel)
  73. toArray := value.([]interface{})
  74. for _, status := range toArray {
  75. var statusType statusChannel
  76. jsonVal, err := json.Marshal(status)
  77. if err != nil {
  78. return err
  79. }
  80. err = json.Unmarshal(jsonVal, &amp;statusType)
  81. if err != nil {
  82. return err
  83. }
  84. statusChannelsMap[statusType.ChannelId] = &amp;statusType
  85. }
  86. field.Set(reflect.ValueOf(statusChannelsMap))
  87. default:
  88. field.Set(val.Convert(fieldType))
  89. }
  90. }
  91. return nil
  92. }

And when I will decode the object I receive the following error:

  1. go-metrics-reported git:(dev) make check
  2. go test -v ./...
  3. ? github.com/OpenLNMetrics/go-metrics-reported/cmd/go-metrics-reported [no test files]
  4. ? github.com/OpenLNMetrics/go-metrics-reported/init/persistence [no test files]
  5. === RUN TestJSONSerializzation
  6. --- PASS: TestJSONSerializzation (0.00s)
  7. === RUN TestJSONDeserializzation
  8. --- FAIL: TestJSONDeserializzation (0.00s)
  9. panic: reflect.Value.Convert: value of type map[string]interface {} cannot be converted to type *plugin.osInfo [recovered]
  10. panic: reflect.Value.Convert: value of type map[string]interface {} cannot be converted to type *plugin.osInfo
  11. goroutine 7 [running]:
  12. testing.tRunner.func1.1(0x61b440, 0xc0001d69a0)
  13. /home/vincent/.gosdk/go/src/testing/testing.go:1072 +0x30d
  14. testing.tRunner.func1(0xc000001e00)
  15. /home/vincent/.gosdk/go/src/testing/testing.go:1075 +0x41a
  16. panic(0x61b440, 0xc0001d69a0)
  17. /home/vincent/.gosdk/go/src/runtime/panic.go:969 +0x1b9
  18. reflect.Value.Convert(0x6283e0, 0xc0001bb1a0, 0x15, 0x6b93a0, 0x610dc0, 0x610dc0, 0xc00014cb40, 0x196)
  19. /home/vincent/.gosdk/go/src/reflect/value.go:2447 +0x229
  20. github.com/OpenLNMetrics/go-metrics-reported/internal/plugin.(*MetricOne).UnmarshalJSON(0xc00014cb00, 0xc0001d8000, 0x493, 0x500, 0x7f04d01453d8, 0xc00014cb00)
  21. /home/vincent/Github/OpenLNMetrics/go-metrics-reported/internal/plugin/metrics_one.go:204 +0x5b3
  22. encoding/json.(*decodeState).object(0xc00010be40, 0x657160, 0xc00014cb00, 0x16, 0xc00010be68, 0x7b)
  23. /home/vincent/.gosdk/go/src/encoding/json/decode.go:609 +0x207c
  24. encoding/json.(*decodeState).value(0xc00010be40, 0x657160, 0xc00014cb00, 0x16, 0xc000034698, 0x54ec19)
  25. /home/vincent/.gosdk/go/src/encoding/json/decode.go:370 +0x6d
  26. encoding/json.(*decodeState).unmarshal(0xc00010be40, 0x657160, 0xc00014cb00, 0xc00010be68, 0x0)
  27. /home/vincent/.gosdk/go/src/encoding/json/decode.go:180 +0x1ea
  28. encoding/json.Unmarshal(0xc0001d8000, 0x493, 0x500, 0x657160, 0xc00014cb00, 0x500, 0x48cba6)
  29. /home/vincent/.gosdk/go/src/encoding/json/decode.go:107 +0x112
  30. github.com/OpenLNMetrics/go-metrics-reported/internal/plugin.TestJSONDeserializzation(0xc000001e00)
  31. /home/vincent/Github/OpenLNMetrics/go-metrics-reported/internal/plugin/metric_one_test.go:87 +0x95
  32. testing.tRunner(0xc000001e00, 0x681000)
  33. /home/vincent/.gosdk/go/src/testing/testing.go:1123 +0xef
  34. created by testing.(*T).Run
  35. /home/vincent/.gosdk/go/src/testing/testing.go:1168 +0x2b3
  36. FAIL github.com/OpenLNMetrics/go-metrics-reported/internal/plugin 0.008s
  37. ? github.com/OpenLNMetrics/go-metrics-reported/pkg/db [no test files]
  38. ? github.com/OpenLNMetrics/go-metrics-reported/pkg/graphql [no test files]
  39. ? github.com/OpenLNMetrics/go-metrics-reported/pkg/log [no test files]
  40. ? github.com/OpenLNMetrics/go-metrics-reported/pkg/utils [no test files]
  41. FAIL
  42. make: *** [Makefile:15: check] Error 1

can someone explain how I can do this operation in a generic way?

答案1

得分: 3

  1. type MetricOne struct {
  2. // ...
  3. // 忽略该字段。
  4. ChannelsInfo map[string]*statusChannel `json:"-"`
  5. }
  1. func (m MetricOne) MarshalJSON() ([]byte, error) {
  2. // 声明一个新类型,使用MetricOne的定义,
  3. // 这样M将具有与MetricOne相同的结构,
  4. // 但没有它的方法(这避免了对MarshalJSON的递归调用)。
  5. //
  6. // 由于M和MetricOne具有相同的结构,你可以轻松地在这两者之间进行转换。
  7. // 例如,M(MetricOne{})和MetricOne(M{})都是有效的表达式。
  8. type M MetricOne
  9. // 声明一个新类型,该类型具有“desired”类型的字段,
  10. // 并且还**嵌入**了M类型。嵌入将M的字段提升为T,
  11. // 并且encoding/json将对这些字段进行扁平化编组,
  12. // 即与channels_info字段处于同一级别。
  13. type T struct {
  14. M
  15. ChannelsInfo []*statusChannel `json:"channels_info"`
  16. }
  17. // 将map元素移动到slice中
  18. channels := make([]*statusChannel, 0, len(m.ChannelsInfo))
  19. for _, c := range m.ChannelsInfo {
  20. channels = append(channels, c)
  21. }
  22. // 将新类型T的实例传递给json.Marshal。
  23. // 对于嵌入的M字段,使用接收器的转换实例。
  24. // 对于ChannelsInfo字段,使用channels切片。
  25. return json.Marshal(T{
  26. M: M(m),
  27. ChannelsInfo: channels,
  28. })
  29. }
  1. // 与MarshalJSON相同,但是反向操作。
  2. func (m *MetricOne) UnmarshalJSON(data []byte) error {
  3. type M MetricOne
  4. type T struct {
  5. *M
  6. ChannelsInfo []*statusChannel `json:"channels_info"`
  7. }
  8. t := T{M: (*M)(m)}
  9. if err := json.Unmarshal(data, &t); err != nil {
  10. panic(err)
  11. }
  12. m.ChannelsInfo = make(map[string]*statusChannel, len(t.ChannelsInfo))
  13. for _, c := range t.ChannelsInfo {
  14. m.ChannelsInfo[c.ChannelId] = c
  15. }
  16. return nil
  17. }
英文:

https://play.golang.org/p/jzU_lHj1wk7

  1. type MetricOne struct {
  2. // ...
  3. // Have this field be ignored.
  4. ChannelsInfo map[string]*statusChannel `json:&quot;-&quot;`
  5. }
  1. func (m MetricOne) MarshalJSON() ([]byte, error) {
  2. // Declare a new type using the definition of MetricOne,
  3. // the result of this is that M will have the same structure
  4. // as MetricOne but none of its methods (this avoids recursive
  5. // calls to MarshalJSON).
  6. //
  7. // Also because M and MetricOne have the same structure you can
  8. // easily convert between those two. e.g. M(MetricOne{}) and
  9. // MetricOne(M{}) are valid expressions.
  10. type M MetricOne
  11. // Declare a new type that has a field of the &quot;desired&quot; type and
  12. // also **embeds** the M type. Embedding promotes M&#39;s fields to T
  13. // and encoding/json will marshal those fields unnested/flattened,
  14. // i.e. at the same level as the channels_info field.
  15. type T struct {
  16. M
  17. ChannelsInfo []*statusChannel `json:&quot;channels_info&quot;`
  18. }
  19. // move map elements to slice
  20. channels := make([]*statusChannel, 0, len(m.ChannelsInfo))
  21. for _, c := range m.ChannelsInfo {
  22. channels = append(channels, c)
  23. }
  24. // Pass in an instance of the new type T to json.Marshal.
  25. // For the embedded M field use a converted instance of the receiver.
  26. // For the ChannelsInfo field use the channels slice.
  27. return json.Marshal(T{
  28. M: M(m),
  29. ChannelsInfo: channels,
  30. })
  31. }
  1. // Same as MarshalJSON but in reverse.
  2. func (m *MetricOne) UnmarshalJSON(data []byte) error {
  3. type M MetricOne
  4. type T struct {
  5. *M
  6. ChannelsInfo []*statusChannel `json:&quot;channels_info&quot;`
  7. }
  8. t := T{M: (*M)(m)}
  9. if err := json.Unmarshal(data, &amp;t); err != nil {
  10. panic(err)
  11. }
  12. m.ChannelsInfo = make(map[string]*statusChannel, len(t.ChannelsInfo))
  13. for _, c := range t.ChannelsInfo {
  14. m.ChannelsInfo[c.ChannelId] = c
  15. }
  16. return nil
  17. }

huangapple
  • 本文由 发表于 2021年8月18日 20:52:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/68832776.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定