#Using reserved words as struct field name

1 messages · Page 1 of 1 (latest)

desert mirage
#

So, i'm trying to read a json file. From the examples i found, i need to define a struct mimicking the json data's layout and pass it to json.unmarshal to be populated. Fine.

Problem is, the json data contains a field named "map", which conflicts with the built in map type when i try to define it on my struct.

Is there a way around this ?

boreal fiber
#

I believe you can use struct tags to tell the JSON marshaller what the field name should be in the JSON.

#

Something like

map_: Whatever_Type `json:"SOMETHING-HERE"`,
#

I believe the SOMETHING-HERE is the name of the field to use(?)

desert mirage
#

Thanks, didn't know about struct tags.

This exact syntax does not work (unless i'm misunderstanding/mistyping something) but unmarshall_object does seem to check for a "json" tag: https://github.com/odin-lang/Odin/blob/e52cc73d50b088c5c899e656f29e3717f4d104d7/core/encoding/json/unmarshal.odin#L362C62-L362C62, so i think we're on the right track 👍

GitHub

Odin Programming Language. Contribute to odin-lang/Odin development by creating an account on GitHub.

boreal fiber
#

Hmm. Not sure if the code for getting a tag is correct or not

#

If not, then maybe that has something to do with it

#

#bug-discussions message

desert mirage
#

Nevermind, i think your proposed syntax is right. Didn't work at first because my mapping was not quite right, but now i'm getting another error which hints at the proper field being parsed.

Thanks!

#

yep, fixed a few unrelated errors and this is now working like a charm. Thanks again!