There are other JSON packages (jsoniter, gojay come to mind) which may do more of what you want.
The standard library's JSON package is fine for simple things, but because it relies on struct tagging, it's not terribly well suited for situations where you want to separate the serialized representation from the internal data (e.g. it gets a little crowded if you need to map to other formats (maybe you also need XML and something else), and it's right out if you're separating a REST interface from business logic). None of the off-the-shelf packages do *exactly* what I want to do in those regards, but gojay comes pretty close. https://github.com/francoispqt/gojay https://github.com/json-iterator/go - Dave > On Jun 1, 2019, at 10:58 PM, hieuht...@gmail.com wrote: > > Have you solved this? > > On Monday, December 17, 2012 at 9:03:50 AM UTC+7, Boris wrote: > On Sun, Dec 16, 2012 at 5:45 PM, Dustin Sallings <dsal...@gmail.com> wrote: > Boris <boris....@gmail.com> writes: > > > I have JSON web API that accepts fields URL parameter, like this: > > > > GET /something/?fields=a,b,c > > > > This is common pattern in web APIs to send partial objects instead of > > full objects. Suppose struct has fields a,b,c,...z, I don't want to > > fetch all fields from database, marshall, send, unmarshall, etc. I > > know I can add tag to struct fields to suppress them in JSON, but that > > is done at compile-time. Is there a way to do this at run-time, or > > should I write custom marshaller for this? > > You need to implement a custom MarshalJSON for your object that > considers some internal fields you configure to specify which parameters > are emitted. > > > Interesting, so http://golang.org/pkg/encoding/json/#Marshaler is the way to > override default marshaling. I guess I will implement some helper function so > I don't re-implement this for every single struct type I use, and have an > internal field in the item, which I set to indicate which fields to include. > If anyone has anything else to add, like an example implementation or tips > for efficiency, I will appreciate. > > Thanks > Boris > > -- > 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+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/09b0a4c5-92a6-4948-aae9-77cc713d4438%40googlegroups.com. > For more options, visit https://groups.google.com/d/optout. -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/1F8B9E51-09E6-49B4-ABDB-673E820A7426%40gmail.com. For more options, visit https://groups.google.com/d/optout.