Use Specific json.Unmarshaler for a dynamic JSON

92 views
Skip to first unread message

dc0d

unread,
Apr 4, 2017, 6:54:34 AM4/4/17
to golang-nuts
Target: Assuming we want to deserialize some JSON dynamically and we want to unmarshal some fields using specific unmarshaling code (a certain UnmarshalJSON). The problem is using something like map[string]interface{} there is no way to specify that.

Thought Experiment: I like to have something like:

ourUnmarshaler.AddField("name", func() json.Unmarshaler /* default field creator for unmarshaling */, /* other options like omitempty */)

or perhaps possibility of using:

map[string]struct{
    Name string
    Constructor func() json.Unmarshaler
    OmitEmpty bool
    String bool
    // ...
}

That would be the ideal thing. But is it possible to do so with current builtin tools? Or some package?

Other notes:
I've found gabs but it does not what I want. Some of my specific cases are big int64 values which should serialized as string because they get converted to ugly float things and some custom parsing for specific strings.

Egon

unread,
Apr 4, 2017, 8:25:39 AM4/4/17
to golang-nuts
What is the JSON input that you need to parse?

+ Egon

Egon

unread,
Apr 4, 2017, 8:33:19 AM4/4/17
to golang-nuts
On Tuesday, 4 April 2017 13:54:34 UTC+3, dc0d wrote:
Target: Assuming we want to deserialize some JSON dynamically and we want to unmarshal some fields using specific unmarshaling code (a certain UnmarshalJSON). The problem is using something like map[string]interface{} there is no way to specify that.

Thought Experiment: I like to have something like:

ourUnmarshaler.AddField("name", func() json.Unmarshaler /* default field creator for unmarshaling */, /* other options like omitempty */)

or perhaps possibility of using:

map[string]struct{
    Name string
    Constructor func() json.Unmarshaler
    OmitEmpty bool
    String bool
    // ...
}

That would be the ideal thing. But is it possible to do so with current builtin tools? Or some package?


If you need the field names to construct the data, do a separate pass over the data.

Egon

unread,
Apr 4, 2017, 8:35:53 AM4/4/17
to golang-nuts


On Tuesday, 4 April 2017 15:33:19 UTC+3, Egon wrote:
On Tuesday, 4 April 2017 13:54:34 UTC+3, dc0d wrote:
Target: Assuming we want to deserialize some JSON dynamically and we want to unmarshal some fields using specific unmarshaling code (a certain UnmarshalJSON). The problem is using something like map[string]interface{} there is no way to specify that.

Thought Experiment: I like to have something like:

ourUnmarshaler.AddField("name", func() json.Unmarshaler /* default field creator for unmarshaling */, /* other options like omitempty */)

or perhaps possibility of using:

map[string]struct{
    Name string
    Constructor func() json.Unmarshaler
    OmitEmpty bool
    String bool
    // ...
}

That would be the ideal thing. But is it possible to do so with current builtin tools? Or some package?


If you need the field names to construct the data, do a separate pass over the data.

Also you can now use the Decoder as a tokenizer https://golang.org/pkg/encoding/json/#Decoder.Token, so implementing custom unmarshaling isn't that difficult.

dc0d

unread,
Apr 4, 2017, 8:58:17 AM4/4/17
to golang-nuts
I do not know what's the JSON! I just know it contains some certain fields, somewhere inside - some date-times, geo info (string formatted) and the like. Now parsing these data needs certain unmarshalers. But I know the name of those fields - not their position (they are not necessarily a direct field of the JSON, but maybe a field of a child JSON object).

Token seems interesting! I'll look into it.
Reply all
Reply to author
Forward
0 new messages