go-json
go-json copied to clipboard
Support for custom tags
Can support for custom tag while marshalling and unmarshalling struct be added to the library ?
Usecase - We receive json response in wierd key name but while storing and forwarding the same response I want to fix the key names in response.
Current solution -
- I am aware that I can create 2 struct like below and typecast them
type Incoming struct {
Name string `cust:"n"`
Age string `cust:"a"`
}
type Out struct {
Name string `json:"name"`
Age string `json:"age"`
}
incoming := Incoming{Name: "name", age: "25"}
out := Out(incoming)
This would give exactly the same result but would require me to maintain 2 exactly same structs.
Therefore I was wondering if we can directly add the support for custom tag which would be ewxposed with Options flag I have implemented the same in my forked version - https://github.com/goccy/go-json/compare/master...jsjain:go-json:master
Usage -
json.UnmarshalWithOption(responseBytes, &responseStruct, json.DecodeWithTagName("cust"))
PS: Currently i have not added support for stream decoder