Golang无法解析反射创建的对象的JSON。

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

golang fails to parse json for reflection created object

问题

我尝试在Go语言中编写一个简单的消息协议,但遇到了一个问题。我有很多消息类型,我想要一个类似于这样的字典来处理消息:

var dict map[reflect.Type]int = map[reflect.Type]int{
    reflect.TypeOf(DataMessage{}): 1000,
    reflect.TypeOf(TextMessage{}): 1001,
    //....
}

func GetMessageTypeId(value interface{}) int {
    if id, ok := dict[reflect.TypeOf(value)]; ok {
        return id
    } else {
        return -1
    }
}

func GetValueByTypeId(typeId int) interface{} {
    for typeDec, id := range dict {
        if id == typeId {
            return reflect.Zero(typeDec).Interface()
        }
    }
    fmt.Println("Unknown message type", typeId)
    return nil
}

它工作得很好,但是当我使用GetValueByTypeId实例化消息并尝试将JSON解组成它时,我收到的是map[string]interface而不是我的消息。
我创建了一个简单的示例来重现这个问题:

http://play.golang.org/p/QEyDN9vztr

英文:

I try to write simple message protocol in go and i've encountered a problem. I have a lot of message types and i want to have a dictionary like this to manipulate with messages:

var dict map[reflect.Type]int = map[reflect.Type]int{
    reflect.TypeOf(DataMessage{}):          1000,
    reflect.TypeOf(TextMessage{}):          1001,
    //....
}

func GetMessageTypeId(value interface{}) int {
    if id, ok := dict[reflect.TypeOf(value)]; ok {
        return id
    } else {
        return -1
    }
}

func GetValueByTypeId(typeId int) interface{} {
    for typeDec, id := range dict {
        if id == typeId {
            return reflect.Zero(typeDec).Interface()
        }
    }
    fmt.Println("Unknown message type", typeId)
    return nil
}

It works fine, but when i instantiate message with GetValueByTypeId and try to unmarshall json into it - i receive map[string]interface instead of my message.
I've made simple example to reproduce the problem:

http://play.golang.org/p/QEyDN9vztr

答案1

得分: 2

请阅读这篇文章:http://research.swtch.com/interfaces,特别是"Memory Optimizations"部分。

根据定义,interface{}由两个指针组成——一个指向方法表(例如类型),一个指向它所持有的数据。因此,在以下代码中:

var destination3 interface{} = reflect.Zero(reflect.TypeOf(Message{})).Interface()

它是一个空的方法表(因为interface{}没有方法),并且引用了Message{}。从中获取引用会返回对该结构体的引用,因此unmarshal会用与interface{}匹配的内容覆盖它。

如果interface{}变量所持有的数据本身是一个指针,那么它会被优化,使用该指针而不是创建interface{}结构体。因此,获取对它的引用会返回对原始变量的引用。

在你的情况下,使用Zero等同于上面示例中的m3。使用New等同于m2。

英文:

Please read this article: http://research.swtch.com/interfaces, especially the "Memory Optimizations".

The interface{} by definition consists of two pointers - to method table (e.g. type) and to data it holds. So for

var destination3 interface{} = reflect.Zero(reflect.TypeOf(Message{})).Interface()

it is empty method table (as interface{} has no methods) and reference to Message{}. Taking reference from it returns the reference to this struct so the unmarhal overwrites it with whatever matches interface{}.

If the data interface{} variable holds is a pointer itself, then it is optimized in a way that this pointer is used instead creating interface{} structure. So getting reference to it gives the reference to original variable.

http://play.golang.org/p/KsIS29rUAX

package main

import "fmt"

func main() {
    var m1 struct{ Data string }
    var m2 interface{}
    var m3 interface{}

    m2 = &m1
    m3 = m1

    fmt.Printf("&m1=%p m2=%p &m3=%p\n", &m1, m2, &m3)
}

In your case, using Zero is equivalent to m3 in the example above. Using New is equivalent to m2.

答案2

得分: 0

我找到了如何做我需要的事情的方法:

val := reflect.New(reflect.TypeOf(Message{}))
json.Unmarshal(data, val.Interface())

return val.Elem().Interface()

http://play.golang.org/p/8g9FSg3MSj

但是第一个版本有什么问题吗?看起来reflect.Zero(type)应该等同于reflect.New(type).Elem(),我错了吗?

英文:

I've found the way how to do what i need

val := reflect.New(reflect.TypeOf(Message{}))
json.Unmarshal(data, val.Interface())

return val.Elem().Interface()

http://play.golang.org/p/8g9FSg3MSj

But was was wrong wit the first version???
It Looks like reflect.Zero(type) should be equivalent to reflect.New(type).Elem() - am i wrong?

huangapple
  • 本文由 发表于 2015年8月12日 17:20:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/31960996.html
匿名

发表评论

匿名网友

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

确定