FAQ
Hi,

I have a struct like this:

type Action struct {

     // etc...

     Links []string `json:"links,omitempty"`
}


Json body of a POST gets converted (decoded) into a struct like this.
It works fine, because if 'links' is omitted from the body of the POST, the
decoder will handle this.

But my question is, how do I now make any json Encoder of such a struct
_include_ an empty 'links' list?

I tried doing this before encoding:

[assume 'act' is a pointer to an Action struct]

     if len(act.Links) == 0 {
         act.Links = make([]string, 0)
     }


But after encoding, it's still blanking out the links element from any JSON
output.

Any guidance would be much appreciated.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • C Banning at Mar 21, 2016 at 4:36 pm
    You're "json" tag says "omitempty". It appears to be doing what you asked.
    On Monday, March 21, 2016 at 11:27:24 AM UTC-5, kiru....@gmail.com wrote:

    Hi,

    I have a struct like this:

    type Action struct {

    // etc...

    Links []string `json:"links,omitempty"`
    }


    Json body of a POST gets converted (decoded) into a struct like this.
    It works fine, because if 'links' is omitted from the body of the POST,
    the decoder will handle this.

    But my question is, how do I now make any json Encoder of such a struct
    _include_ an empty 'links' list?

    I tried doing this before encoding:

    [assume 'act' is a pointer to an Action struct]

    if len(act.Links) == 0 {
    act.Links = make([]string, 0)
    }


    But after encoding, it's still blanking out the links element from any
    JSON output.

    Any guidance would be much appreciated.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Shawn Milochik at Mar 21, 2016 at 4:39 pm

    On Mon, Mar 21, 2016 at 11:28 AM, wrote:
    But my question is, how do I now make any json Encoder of such a struct
    _include_ an empty 'links' list?
    That's what you are getting. If it wasn't an empty links list, it would be
    nil.

    http://play.golang.org/p/hwxKnusoIa

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • C Banning at Mar 21, 2016 at 4:49 pm
    I think he's asking about "encoding" where Links = make([]string,0)
    - http://play.golang.org/p/kKzPjrWqvb

    Well, it seems to be fallowing the tag instructions and omitting an empty
    array.
    On Monday, March 21, 2016 at 11:39:46 AM UTC-5, Shawn Milochik wrote:

    On Mon, Mar 21, 2016 at 11:28 AM, <kiru....@gmail.com <javascript:>>
    wrote:
    But my question is, how do I now make any json Encoder of such a struct
    _include_ an empty 'links' list?
    That's what you are getting. If it wasn't an empty links list, it would be
    nil.

    http://play.golang.org/p/hwxKnusoIa

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Shawn Milochik at Mar 21, 2016 at 4:52 pm

    On Mon, Mar 21, 2016 at 12:49 PM, C Banning wrote:

    I think he's asking about "encoding" where Links = make([]string,0) -
    http://play.golang.org/p/kKzPjrWqvb

    Well, it seems to be fallowing the tag instructions and omitting an empty
    array.

    Yeah, looks like I misinterpreted the original message.
    Then as you said, removing the "omitempty" is the solution. If the problem
    is that the JSON bytes don't have a "links":[] as desired.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • C Banning at Mar 21, 2016 at 4:52 pm
    I believe that question was about "encoding" an empty
    array: http://play.golang.org/p/wBkO2BjCGs

    It seems to work correctly and follow the tag instruction to "omitempty".
    On Monday, March 21, 2016 at 11:39:46 AM UTC-5, Shawn Milochik wrote:

    On Mon, Mar 21, 2016 at 11:28 AM, <kiru....@gmail.com <javascript:>>
    wrote:
    But my question is, how do I now make any json Encoder of such a struct
    _include_ an empty 'links' list?
    That's what you are getting. If it wasn't an empty links list, it would be
    nil.

    http://play.golang.org/p/hwxKnusoIa

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMar 21, '16 at 4:27p
activeMar 21, '16 at 4:52p
posts6
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase