easyjson icon indicating copy to clipboard operation
easyjson copied to clipboard

Embedded reference struct should not need to be "new"'ed unless there are corresponding fields

Open garukun opened this issue 9 years ago • 0 comments

Consider the following JSON:

{
  "Direct1":1,
  "Embedded2": 2,
  "Embedded3": 3
}

And given the following structs:

type A struct {
  Direct1 int
  *B
}

type B struct {
  Embedded2 int,
  Embedded3 int
}

When one generate the custom unmarshaling, there will be a line in the implementation that would read something like:

out.B = new(B)

regardless of if he has those embedded fields in the raw JSON string; e.g. when the JSON looks like:

{"Direct1":1}

he will still end up creating a new B. I think this B type shouldn't be created in this case because 1) it would waste additional memory, 2) it would make marshaling back into a different object when the string was directly marshaled from an A without B.

garukun avatar Oct 11 '16 16:10 garukun