FAQ
I have a struct when serialized produces a

Json string.

*
*

{
"TotalRecords": "11",
"MaxRecords": "1000",
"Version": "1.000",

}

Sometimes I want to suppress some of struct

elements of showing up in Json string.


For example I want to remove Version element:

{
"TotalRecords": "11",
"MaxRecords": "1000",}


In VB.NET I would do:
response.Version = Nothing
and Version element will not show up in serialization.


Here is my Golang code:


*type* Res *struct* {

TotalRecords *string*

MaxRecords *string*

Version *string*

}
*
*

*var* bResponse []*byte*

*var* response Res

response.TotalRecords = "11"
response.MaxRecords = "1000"
response.Version = "1.000"

bResponse, _ = json.Marshal(response)


How to do this in Golang?

Thanks,
--Constantine

--
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/groups/opt_out.

Search Discussions

  • Andreas Krennmair at Feb 11, 2013 at 5:04 pm
    If you want to disable elements from being considered during marshaling,
    you can use struct tags to do so: http://play.golang.org/p/HrxVqPR0q4

    Hope that helps,
    Andreas Krennmair

    On Mon, Feb 11, 2013 at 5:32 PM, Constantine Vasil wrote:

    I have a struct when serialized produces a

    Json string.

    *
    *

    {
    "TotalRecords": "11",
    "MaxRecords": "1000",
    "Version": "1.000",

    }

    Sometimes I want to suppress some of struct

    elements of showing up in Json string.


    For example I want to remove Version element:

    {
    "TotalRecords": "11",
    "MaxRecords": "1000",}


    In VB.NET I would do:
    response.Version = Nothing
    and Version element will not show up in serialization.


    Here is my Golang code:


    *type* Res *struct* {

    TotalRecords *string*

    MaxRecords *string*

    Version *string*

    }
    *
    *

    *var* bResponse []*byte*

    *var* response Res

    response.TotalRecords = "11"
    response.MaxRecords = "1000"
    response.Version = "1.000"

    bResponse, _ = json.Marshal(response)


    How to do this in Golang?

    Thanks,
    --Constantine

    --
    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/groups/opt_out.

    --
    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/groups/opt_out.
  • Constantine Vasil at Feb 11, 2013 at 7:08 pm
    Thank you,

    The problem I have is a little different.

    I want the struct to define all elements but to show them
    if they are not empty.

    For example:
    response.TotalRecords = "11"
    response.MaxRecords = "1000"
    response.Version = "1.000" //* not empty*

    {
    "TotalRecords": "11",
    "MaxRecords": "1000",
    "Version": "1.000",

    }


    response.TotalRecords = "11"
    response.MaxRecords = "1000"
    response.Version = "" //* empty*
    *
    *
    *The problem is that there is no way in Golang to make*
    response.Version = Nothing or nil*
    *

    thus suppressing this element to showing up in run time:


    {
    "TotalRecords": "11",
    "MaxRecords": "1000",}


    --Constantine

    On Monday, February 11, 2013 9:04:47 AM UTC-8, Andreas Krennmair wrote:

    If you want to disable elements from being considered during marshaling,
    you can use struct tags to do so: http://play.golang.org/p/HrxVqPR0q4

    Hope that helps,
    Andreas Krennmair


    On Mon, Feb 11, 2013 at 5:32 PM, Constantine Vasil <ths...@gmail.com<javascript:>
    wrote:
    I have a struct when serialized produces a

    Json string.

    *
    *

    {
    "TotalRecords": "11",
    "MaxRecords": "1000",
    "Version": "1.000",

    }

    Sometimes I want to suppress some of struct

    elements of showing up in Json string.


    For example I want to remove Version element:

    {
    "TotalRecords": "11",
    "MaxRecords": "1000",}


    In VB.NET I would do:
    response.Version = Nothing
    and Version element will not show up in serialization.


    Here is my Golang code:


    *type* Res *struct* {

    TotalRecords *string*

    MaxRecords *string*

    Version *string*

    }
    *
    *

    *var* bResponse []*byte*

    *var* response Res

    response.TotalRecords = "11"
    response.MaxRecords = "1000"
    response.Version = "1.000"

    bResponse, _ = json.Marshal(response)


    How to do this in Golang?

    Thanks,
    --Constantine

    --
    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...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    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/groups/opt_out.
  • Gary b at Feb 11, 2013 at 7:18 pm

    I want the struct to define all elements but to show them
    if they are not empty.
    Use omitempty. http://play.golang.org/p/KmToGTutg8

    --
    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/groups/opt_out.
  • Constantine Vasil at Feb 11, 2013 at 9:22 pm
    Gary,

    I tested it and confirm it is working.

    Thank you,
    --Constantine
    On Monday, February 11, 2013 11:18:26 AM UTC-8, gary b wrote:


    I want the struct to define all elements but to show them
    if they are not empty.
    Use omitempty. http://play.golang.org/p/KmToGTutg8
    --
    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/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedFeb 11, '13 at 4:38p
activeFeb 11, '13 at 9:22p
posts5
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase