Skip to content

Support for custom tags #399

Open
Open

Description

@jsjain

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 - master...jsjain:go-json:master

Usage -

json.UnmarshalWithOption(responseBytes, &responseStruct, json.DecodeWithTagName("cust"))

PS: Currently i have not added support for stream decoder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ 换网址