Removing fields from struct or hiding them in JSON Response

后端 未结 13 1346
孤街浪徒
孤街浪徒 2020-12-12 09:37

I\'ve created an API in Go that, upon being called, performs a query, creates an instance of a struct, and then encodes that struct as JSON before sending back to the caller

相关标签:
13条回答
  • 2020-12-12 10:31

    Other answers already explained that this isn't possible using the encoding/json package features directly.

    Instead, I'd like to offer an alternative solution using a third-party library. Disclaimer: I am the author of the library.

    https://github.com/wI2L/jettison

    Jettison is a faster and flexible alternative to encoding/json, while still retaining a behavior identical to the standard library by default.

    You can use the DenyList option to filter-out fields that should be omitted from the result. Note that the feature is limited, and only works for 1st-level fields.

    0 讨论(0)
提交回复
热议问题