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?
>>
>
> https://play.golang.org/p/EP2F-__URO
>
> 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.
 

>  
>
>>
>> Other notes:
>> I've found gabs <https://github.com/Jeffail/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.
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to