go-json-experiment / json

Experimental implementation of a proposed v2 encoding/json package
BSD 3-Clause "New" or "Revised" License
380 stars 12 forks source link

Add option to accept mis-sized arrays #47

Open dominikh opened 1 month ago

dominikh commented 1 month ago

Would you consider adding an option to accept JSON arrays of the wrong size when unmarshaling into a Go array?

This came up for me while decoding Lottie, which has various arrays that should contain 2 elements, as they represent 2D vectors, but the schema doesn't forbid having fewer or more than the required number of elements, and in fact many files in the wild have more than 2 elements (for reasons I do not know). I'd like to use the type system to ensure that there is always the correct number of elements, as opposed to using slices and having to use length checks everywhere.

I could work around this by defining a new type and implementing a custom unmarshaler.

dsnet commented 1 month ago

The v1 "json" package permitted arrays with any size, while v2 "json" made the behavior strict by default.

As part of the v1 compatibility work, we'll be providing a option to have the older behavior.

That said, the current plan is only to have a global option, rather than a per-field option. Would that be acceptable for you?

dominikh commented 1 month ago

That said, the current plan is only to have a global option, rather than a per-field option. Would that be acceptable for you?

That seems reasonable.