go-json icon indicating copy to clipboard operation
go-json copied to clipboard

Support for custom tags

Open jsjain opened this issue 2 years ago • 0 comments

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 -

  1. 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

jsjain avatar Sep 06 '22 10:09 jsjain