Slemgrim
Slemgrim

Reputation: 957

Unmarshal json to reflected struct

Is it possible to unmarshal JSON into a struct made from reflection without hardcoding the original type?

package main

import (
  "fmt"
  "encoding/json"
  "reflect"
)

type Employee struct {
  Firstname string     `json:"firstname"`
}

func main() {
  //Original struct
  orig := new(Employee)

  t := reflect.TypeOf(orig)
  v := reflect.New(t.Elem())

  //Reflected struct
  new := v.Elem().Interface().(Employee)

  // Unmarshal to reflected struct
  json.Unmarshal([]byte("{\"firstname\": \"bender\"}"), &new)

  fmt.Printf("%+v\n", new)
}

I used a cast to Employee in this example. But what if i don't know the type?

When i just use v for the unmarhaling the struct will be zeroed.

json.Unmarshal([]byte("{\"firstname\": \"bender\"}"), v)

When I omit the cast I get a map. which is understandable

json.Unmarshal([]byte("{\"firstname\": \"bender\"}"), v.Elem().Interface())

Upvotes: 17

Views: 9599

Answers (3)

jun xiao
jun xiao

Reputation: 31


// https://github.com/xiaojun207/go-base-utils/blob/master/utils/Clone.go
func NewInterface(typ reflect.Type, data []byte) interface{} {
    if typ.Kind() == reflect.Ptr {
        typ = typ.Elem()
        dst := reflect.New(typ).Elem()
        json.Unmarshal(data, dst.Addr().Interface())
        return dst.Addr().Interface()
    }else {
        dst := reflect.New(typ).Elem()
        json.Unmarshal(data, dst.Addr().Interface())
        return dst.Interface()
    }
}

type Employee struct {
    Firstname string     `json:"firstname"`
}

func main() {
    data := "{\"firstName\": \"John\"}"
    obj := NewInterface(reflect.TypeOf(Employee{}), []byte(data))
    fmt.Println("Employee:", obj)
}

Upvotes: 3

eugenioy
eugenioy

Reputation: 12393

The problem here is that if you omit the type assertion here:

new := v.Elem().Interface()

The new is inferred to have a interface{} type.

Then when you take the address to unmarshal, the type of &new is *interface{} (pointer to interface{}) and unmarshal does not work as you expect.

You can avoid the type assertion if instead of getting the Elem() you work directly with the pointer reference.

func main() {
  //Original struct
  orig := new(Employee)

  t := reflect.TypeOf(orig)
  v := reflect.New(t.Elem())

  // reflected pointer
  newP := v.Interface()

  // Unmarshal to reflected struct pointer
  json.Unmarshal([]byte("{\"firstname\": \"bender\"}"), newP)

  fmt.Printf("%+v\n", newP)
}

Playground: https://play.golang.org/p/lTBU-1PqM4

Upvotes: 19

M3talM0nk3y
M3talM0nk3y

Reputation: 1432

If you don't know the type at all, you can Unmarshal the JSON string into an interface{}. If you then need to work with the Unmarshaled data, you can convert it to the desired type.

Here's an example:

package main

import (
    "encoding/json"
    "fmt"
    "reflect"
    "unsafe"
)

type Employee struct {
    Firstname string `json:"firstName"`
}

func deserialize(jsonData string) interface{} {
    var obj interface{}

    if err := json.Unmarshal([]byte(jsonData), &obj); err != nil {
        panic(err)
    }

    return obj
}

func NewEmployee(objData map[string]interface{}) *Employee {
    s := (*Employee)(nil)
    t := reflect.TypeOf(s).Elem()
    employeePtr := reflect.New(t)
    employee := (*Employee)(unsafe.Pointer(employeePtr.Pointer()))
    employee.Firstname = objData["firstName"].(string)

    return employee
}

func main() {
    jsonData := "{\"firstName\": \"John\"}"

    obj := deserialize(jsonData)

    objData := obj.(map[string]interface{})
    employee := NewEmployee(objData)

    fmt.Printf("%s\n", employee.Firstname)
}

You can check it on the Go Playground.

Upvotes: 0

Related Questions