pquerna / ffjson

faster JSON serialization for Go
Apache License 2.0
2.97k stars 234 forks source link

mechanism to construct objects? #236

Open predmond opened 7 years ago

predmond commented 7 years ago

I'm wondering if there is a solution for telling ffjson how to construct objects? Ideally ffjson would call a function that I define to return a new object. Then I could set default values etc., before unmarshalling (in the case that the field is not in the json)

In my case I have a large structure with many nested structures which ffjson ends up creating so it is not possible to set the defaults before unmarshalling.

e.g.,

type Foo struct {
  A int
  B int
}

type Bar struct {
  A int
  Foo *Foo
}

var bar Bar
ffjson.Unmarshal([]byte(`{ "a": 1, "foo": { "b": 2 } }`, &bar)

If the ffjson generated code called, for example, NewFoo() instead of new(Foo) I could set defaults (or perhaps do something else like Pooling if appropriate)