When encountering an error using json.Marshal() (and related functions) the error returned gives no indication where the error was encountered. The only included metadata is about the type of the value that was being encoded, but that is usually insufficient to narrow down problems without the original input value. (which may not always be at hand)
What is being proposed?
The proposal would be to add the property name for marshal errors (using dot notation to signify hierarchy). For example:
json: error calling MarshalJSON for type time.Time: Time.MarshalJSON: year outside of range [0,9999]
would be updated to be more specific:
json: error calling MarshalJSON for timestamp (type time.Time): Time.MarshalJSON: year outside of range [0,9999]
and in the case of a more complex/nested data structure:
json: error calling MarshalJSON for events..timestamp (type time.Time): Time.MarshalJSON: year outside of range [0,9999]
Why is this useful?
I use Go to do a lot of data processing, which is generally from 3rd-parties (aka: customers) so the structure of data is not usually known ahead of time, not to mention weird values being sent due to programming errors and a host of other problems.
Last week, I encountered the first error message above and needed to do some extra work just to determine which property was causing the error, when the encoder should be able to communicate that to the caller.
The text was updated successfully, but these errors were encountered:
I took this opportunity to dig into the go stdlib and I wanted this to be among my first contributions to the project. I have changes ready in a git branch that I can open a PR with, and I am definitely open to feedback on this proposal ahead of that step.