2015-10-17 05:21
浏览 560


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 =>

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doukun0888
    doukun0888 2015-10-17 05:37

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

    var cust1_recovered Customer


    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:

    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.

    点赞 评论
  • duanlei8119
    duanlei8119 2015-10-17 05:35

    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.

    点赞 评论
  • doubi6303
    doubi6303 2018-09-06 13:35

    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.

    点赞 评论