vendredi 27 janvier 2017

UnmarshalJSON any type from []interface{} possible?

When you unmarshal JSON to []interface{} is there any way to automatically detect the type besides some standard types like bool, int and string?

What I noticed is the following, Let's say I marshal [uuid.UUID, bool] then the JSON I get looks like:

[[234,50,7,116,194,41,64,225,177,151,60,195,60,45,123,106],true]

When I unmarshal it again, I get the types as shown through reflect:

[]interface{}, bool

I don't understand why it picked []interface{}. If it cannot detect it, shouldn't it be at least interface{}?

In any case, my question is, is it possible to unmarshal any type when the target is of type []interface{}? It seems to work for standard types like string, bool, int but for custom types I don't think that's possible, is it? You can define custom JSON marshal/unmarshal methods but that only works if you decode it into a target type so that it can look up which custom marshal/unmarshal methods to use.





Aucun commentaire:

Enregistrer un commentaire