go-json-experiment / json

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

Can jsontext.Value contain whitespace? #14

Closed nigeltao closed 2 months ago

nigeltao commented 8 months ago

https://github.com/go-json-experiment/json/blob/8aa127fd5801b9db7792f3467d6a426a8f83b875/jsontext/value.go#L29

says "Value may contain leading and/or trailing whitespace". But also,

https://github.com/go-json-experiment/json/blob/8aa127fd5801b9db7792f3467d6a426a8f83b875/jsontext/decode.go#L576-L577

says that Decoder.ReadValue "returns the next raw JSON value... stripped of any leading or trailing whitespace".

If I'm processing a Value, do I need to handle whitespace? Is Decoder.ReadValue basically the only blessed way to create Value values?

dsnet commented 8 months ago

Decoder.ReadValue gives guarantee of what the implementation does (i.e., it strips whitespace).

Value just documents that surrounding whitespace is permissible (e.g., if you want to call Encoder.WriteValue with surrounding whitespace).

For an UnmarshalJSON method, the implementation cannot safely assume that it is stripped of whitespace. When called by the "json" package it will always be, but nothing stops users from directly calling UnmarshalJSON.

dsnet commented 2 months ago

Closing as the documentation says:

Value may contain leading and/or trailing whitespace.