DLopes
DLopes

Reputation: 587

Golang serialize/deserialize an Empty Array not as null

Is there a way to serialize an empty array attribute (not null) of a struct and deserialize it back to an empty array (not null again)?

Considering that an empty array is actually a pointer to null, is the perceptible initial difference between an empty array and pointer to null completely lost after serialize/deserialize?

The worst practical scenario is that when I show an empty array attribute to my REST client, as a json "att":[], at first time, and, after cache register to redis and recover it, the same attribute is shown to my client as "att":null, causing a contract broken and a lot of confusing.

Summing up: is possible to show the Customer 2 addresses like an json empty array, after serialize/deserialize => https://play.golang.org/p/TVwvTWDyHZ

Upvotes: 21

Views: 20053

Answers (3)

cbelsole
cbelsole

Reputation: 131

For another solution, we have forked encoding/json to add a new method called MarshalSafeCollections(). This method will marshal Slices/Arrays/Maps as their respective empty values ([]/{}). Since most of our instantiation happens on the data layer we did not want to add code that fixed issues in our http response layer. The changes to the library are minimal and follow go releases.

Upvotes: 2

sberry
sberry

Reputation: 132088

I am pretty sure the easiest way you can do it is to change your line

var cust1_recovered Customer

to

cust1_recovered := Customer{Addresses: []Address{}}

Unless I am reading your question incorrectly, I believe this is your desired output:

ORIGINAL  Customer 2 {
  "Name": "Customer number 2",
  "Addresses": []
}
RECOVERED Customer 2 {
  "Name": "Customer number 2",
  "Addresses": []
}

Here is a playground to verify with: https://play.golang.org/p/T9K1VSTAM0

The limitation here, as @mike pointed out, is if Addresses is truly nil before you encode, once you decode you do not get the json equivalent null, but would instead end up with an empty list.

Upvotes: 18

Mike Precup
Mike Precup

Reputation: 4218

No, it's not possible. To understand why, let's look at the Go spec. For it to output two different results for empty vs. nil, any serialization method would need to be able to tell the difference between the two. However, according to the Go spec,

Two array types are identical if they have identical element types and the same array length.

Since neither contains any elements and have the same element type, the only difference could be in length, but it also states that

The length of a nil slice, map or channel is 0

So through comparison, it would be unable to tell. Of course, there are methods other than comparison, so to really put the nail in the coffin, here's the portion that shows they have the same underlying representation. The spec also guarantees that

A struct or array type has size zero if it contains no fields (or elements, respectively) that have a size greater than zero.

so the actual allocated structure of a zero length array has to be of size zero. If it's of size zero, it can't store any information about whether it's empty or nil, so the object itself can't know either. In short, there is no difference between a nil array and a zero length array.

The "perceptible initial difference between an empty array and pointer to null" is not lost during serialization/deserialization, it's lost from the moment initial assignment is complete.

Upvotes: 13

Related Questions