Skip to content

Custom JSON marshal/unmarshal #2005

Closed
@mitar

Description

@mitar

Is your feature request related to a problem? Please describe.

I have two use cases where I think custom function to do JSON marshal/unmarshal would help (instead of default json.Marshal/json.Unmarshal:

  • I want to marshal with SetEscapeHTML(false) set, because there is no point in escaping HTML when storing into a database. Similarly, I want to set KnownFields(true) when unmarshal, to detect errors early.
  • I want to store a polymorphic JSON into a database (one where depending on its type field it gets unmarshal into different structs), the type being an interface. So I would use a custom unmarshal function which first checks the type and then does the rest.

Describe the solution you'd like

Some easy way to specify the default json.Marshal and json.Unmarshal function to be used.

Describe alternatives you've considered

It seems I could remove the json codec and create my own copy of it, with only difference being that it does not call json.Marshal. I would like to avoid having to maintain such a copy where the only difference is in the function which gets called.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions