Take the 2-minute tour ×

Here is the code:

package main

import (


    datajson []byte

type User struct {
    Name string
    Type reflect.Type

func MustJSONEncode(i interface{}) []byte {
    result, err := json.Marshal(i)
    if err != nil {
    return result
func MustJSONDecode(b []byte, i interface{}) {
    err := json.Unmarshal(b, i)
    if err != nil {
func Store(a interface{}) {
    datajson = MustJSONEncode(a)

func Get(a []byte, b interface{}) {
    MustJSONDecode(a, b)

func main() {
    dummy := &User{}
    david := &User{Name: "DavidMahon"}
    typ := reflect.TypeOf(david)
    david.Type = typ
    Get(datajson, dummy)

I can successfully marshal reflect.Type but when I do the reverse, it panics. I know reflect.Type is an interface. So what am I doing wrong here? How can I store a reflect.Type value in json and then retrieve back safely?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

That doesn't make sense. There is no way for the JSON package to know what data should be decoded. This interface might be implemented by a struct{} type, an int type and maybe a struct{ Value1, Value2 int } type. Even worse, there might be other types as well, that are aren`t even part of your binary (missing imports, dead code elimination, etc.). So, which type do you expect to get back from JSON?

You probably need to find another way. For example, you could implement the json.Unmarshaler interface, unmarshal an identifier and use that identifier to unmarshal the following data into a concrete type. The switch statement that you will probably need for that, will also ensure that the concrete type is part of your binary (since you are using it).

But there is probably an easier solution. Your question doesn't tell what you want to archive though. Would it be enough to just marshal / unmarshal the name of the type as string?

share|improve this answer
I want to store data(as in object of certain type) into database. In order to store that data, I need to marshal it into json. My object contains reflection details (Type of object). So basically I can store any data of any type and get it back. Database is a custom one that I am using it. I just want a way to store data of type reflect.Type and get it back safely. –  Minty Aug 17 '13 at 18:54
I thought of storing the name of the type, but then how would I get concrete Type from that? I am perplexed –  Minty Aug 17 '13 at 18:58
If you want to store objects of different types in your database, you could add a "Type" field that contains just the name of the type. Then you would have to decode the type field first (just decode into a struct {Type string}) before you decode your real object based on that type. –  tux21b Aug 17 '13 at 19:00
What api shall I use to decode a string into reflect.Type? I am unable to find one. –  Minty Aug 17 '13 at 19:03
There is none. Use a map[string]reflect.Type. You need such a map anyway to ensure that those types are part of your binary... init() functions might be useful to populate the map. –  tux21b Aug 17 '13 at 19:05
show 6 more comments

Your Answer


