Giter Site home page Giter Site logo

Comments (4)

vstarodub avatar vstarodub commented on May 22, 2024

Hi! I think I'd need more info on the particular use case to come up with a solution or, at least, a suggestion. First of all, what's your business logic scenario for this? Is it something that comes up more or less often in general?

Do you need a shallow or a deep "selector", i.e. do you need to say that you don't want to marshal field 'A' of field 'B' of the struct? Do you need to marshal different set of fields for different elements of an array?

A shallow one is rather simple to implement and it does not require modifying the struct, but now I don't see how it will be useful.

For a deep selector, we are actually talking about querying a hierarchy of structs, which seems to be application specific and somewhat unrelated to serialization. It also looks like a thing like this might grow into something like LINQ (of which there are already multiple attempts to implement in golang).

This being said, it is quite possible to add an exported (it has to be) integer field that is tagged in a special way and that will act as a bitmask of fields to omit. The generator in turn will generate consts that can be used to actually mark the fields that will be omitted. But frankly I'd rather not do something like this without a reasonable use-case.

from easyjson.

fcheslack avatar fcheslack commented on May 22, 2024

In my case, the primary use was this:
I have a struct with a good number of fields, many of which are likely to be empty strings on a regular basis. Enough so that it seems cleaner, for possible human consumption, to have most of them be omitempty for JSON.

However, this struct may also be used in elasticsearch (or various other systems that speak JSON) which does updates with patch semantics, merging the update document into the existing document. This is desirable for various reasons, but requires the ability to explicitly set fields to empty values.

The way json encoding in the standard library (and the alternatives I'm aware of) works means I need to either have a struct field present in the JSON I marshal, or always leave it out when it's empty.

Instead, I'd like to, effectively, change the omitempty flag for each field at runtime, so I can leave fields out of an API response, while still being able to use patch semantics to update them.

A secondary aspect of it was a case where fields B and C are only relevant if field A has a particular value. Otherwise fields B and C will always been empty and consumers of the JSON could ignore them. So again, simply choosing contextually when I want a field to be omitempty.

I was assuming a shallow selector. Deeper selectors would be done by those nested struct's own Marshal functions (which is why I thought it requires the struct to carry this setting with it somehow).

from easyjson.

vstarodub avatar vstarodub commented on May 22, 2024

So basically you need to distinguish empty and missing values during marshalling?

There are "optional" wrappers around primitive types in easyjson/opt for this use case. It requires slightly more code, but, on the other hand, these can be passed around as arguments.

from easyjson.

rvasily avatar rvasily commented on May 22, 2024

easyjson is simple instrument for json (de)serialization
it's main purpose is achive performance by avoid runtime magic

if you want to cut some fields from result - you can use opt.* fields and control it by you app logic

from easyjson.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.