Comments (4)
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.
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.
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.
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)
- Detailed unmarshalling error information
- go1.18
- Struct with embedding struct pointer set to nill causes panic HOT 1
- go 1.18 generics support HOT 3
- easyjson didn't generate all data. MFTI/perfomance/json example HOT 1
- Tag a new version
- Undocumented 'required' struct tag
- String JSON field to int struct field
- Interface types generate associated functions. Maybe they shouldn't.
- (v *RawMessage) UnmarshalJSON(data []byte) does not make copy of data
- Cannot handle uint64 just like std json
- cant gen code in main package HOT 1
- Impact of sanctions on this library
- json.Valid breaks in SkipRecursive
- errors!
- Cli tool type missmatch
- []byte field is incorrectly generated as a string field, making it unmarshalable
- golang 1.21 and 1.22
- why not use map/hash instead of for-loop HOT 1
- go vet swears to json "required" tag
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from easyjson.