-
Notifications
You must be signed in to change notification settings - Fork 339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support configure tag when marshal/unmarshal #440
Comments
There are some ways to do it. For example, use a mock type, and change - to other tags if you doest not want to ignore it.
|
Yes, it works.but if there are a lot of such structs. mocking them would not be very elegant. to the same struct, In some scenario i want them to be omitted, and others not :) Maybe my request can be expressed as a more generic one: support setting more than one tag key, such as. json:"id",other:"id2" then, I can use a tagKey not exists, the process will fallback to no tag |
this case is not generic, you can use a marshaler/unmarshaler interface. |
golang/go#45812 It's really useful feature in GraphQL or FieldMask. |
Thank you for sharing, we will survey it~ |
In jsoniter, I can appoint specific tagKey to marshal, like this: If this feature is supported by sonic, I can remove jsoniter totally. thanks |
thanks, we will discuss it |
Since sonic's JIT assumes field tag is immutable and complies the field key into assembly (https://github.com/bytedance/sonic/blob/main/internal/encoder/compiler.go#L738), I don't think it is worth supporting this config. The simplest way is compiling and caching different JIT-produced codes according to this config, but it will cost more memory and CPU in runtime |
I think this can be done in Higher level the JSON key lookup is like: EDIT 0: But then we'll need to have different cache storage for them. Ref: https://go.googlesource.com/go/+/go1.20.4/src/encoding/json/encode.go#1257 |
Thank you. I think we can create a fieldmap that contains the mapping of each field's JSON tag name to its field name and its corresponding quoted field name when resolving the struct. Then, we can pass the tag name as a parameter to the encoder and decoder so that we don't need to compile and cache different machine code for different tags. To make it more convenient, we can store the tag on a fixed location on the stack, and select the appropriate fieldmap during decoding based on the tag. For encoding, we can also select the quoted field name constant to copy based on the tag. Chinese: 为了方便,我们可以将tag 存放到 stack上面的固定位置,然后decode 时根据tag 来选择fieldmap,encode时根据 tag 来选择需要拷贝的 quoted fieldname 常量。 |
this will make encoder a little bit slower, since query field name need at least O(1) calculation |
any update on this? any hero can make it done? |
Thanks. We will change lots of code and don't have enough time to do it now. Maybe support in the future. cc @AsterDY |
Got it. Hope it will be done as soon as possible. it's not easy to find such an excellent library, but unfortunately, I can only watch it with nothing to do :) |
If there is only one tagKey or no tagKey, keep the current solution ; If there is more than one tagKey, then use the tag field map solution. Only the user who uses more tagKeys pay the cost does this feasible? |
For example, I have a struct as follows, I want the id field to be omitted in json when writing http response, so I set tag
json:"-"
, it's ok。 But when I want to marshal the same struct to redis (or unmarshal from redis) , I want the id field not to be omitted .So would you please allow developers to decide whether to ignore tag when marshal/unmarshaltype Example struct {
Id int64
json:"-"
IdStr string
json:"id"
}
The text was updated successfully, but these errors were encountered: