- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 178
 
Open
Description
This feature would enable users to serialize/deserialize the same Go struct to/from different JSON formats based on a specified tag name (e.g., for different API versions, client needs, or internal/external representations).
Here's an example to illustrate the desired behavior:
type Request struct {
    UserId   string `json:"user_id" custom_scene:"uid"`
    NickName string `json:"nick_name"`
    Age      string `json:"age"`
}Proposed Usage:
A new function signature or an option for json.Marshal and json.Unmarshal that accepts a tagName argument. When tagName is provided, go-json would prioritize using the value from that tag; if the specified tag doesn't exist for a field, it would fall back to the default json tag.
Code Example:
req := Request{
    UserId:   "user1",
    NickName: "mono",
    Age:      "30",
}
// Marshal using "custom_scene" tag if present, otherwise default to "json" tag
// (This is conceptual syntax, exact API could vary)
// Example 1: With a new function
// jsonData, err := json.MarshalWithTag(req, "custom_scene")
// Example 2: With an option struct
// jsonData, err := json.MarshalWithOptions(req, json.MarshalOption{TagName: "custom_scene"})
// For demonstration, let's assume `json.Marshal(req, "custom_scene")` is the proposed syntax
jsonData, err := json.Marshal(req, "custom_scene") // custom_scene is the specified tag name
if err != nil {
    // handle error
}
fmt.Println(string(jsonData))Expected Result:
{
    "uid": "user1",
    "nick_name": "mono",
    "age": "30"
}iwyrkore
Metadata
Metadata
Assignees
Labels
No labels