whatwg / infra

Infra Standard
https://infra.spec.whatwg.org/
Other
118 stars 95 forks source link

JSON serialization/deserialization to/from bytes doesn't handle undefined and 0 bytes #264

Closed kenchris closed 4 years ago

kenchris commented 5 years ago

image

JSON.stringify can return undefined if called with things like undefined or Symbol('mySymbol')

This should result in 0 bytes (null) being the result of the algorithm. We can of course also just make it return an error and handle it elsewhere

deserialization should probably also assert that there is actually a non-zero length byte sequence

annevk commented 5 years ago

Parsing should throw on the empty byte sequence, no? I'm not sure why we'd require a non-throwing input. We'd have to modify a bunch of callers if we'd decide to do that.

For serialization I'm not sure it makes sense to return the empty byte sequence, as that's not valid JSON. I'd be somewhat inclined to throw as well, though not entirely sure what exception.

kenchris commented 5 years ago

So JSONparse has this

image

But not sure what toString would be called with when there is an empty byte sequence? (null or undefined?)

image

I'd be somewhat inclined to throw as well, though not entirely sure what exception.

SyntaxError seems consistent with the above

annevk commented 5 years ago

@kenchris jsonText is not a byte sequence, we pass that function a string.

kenchris commented 5 years ago

Right, we just need to handle 0 bytes ourselves, as UTF8Encode of 0 bytes makes no sense.

annevk commented 5 years ago

I'm not sure I understand, the parser decodes and decoding the empty byte sequence works fine.

kenchris commented 5 years ago

OK, what does it decode it to? @zolkis and I found it hard following the algorithms in the encoding spec

annevk commented 5 years ago

To an empty code point stream, which can be coerced automatically into a string.

andreubotella commented 4 years ago

Looking at this, it seems like there's still an issue with the call to %JSONStringify% being able to return undefined. Presumably the operation should throw in that case, so any result of serialization can round-trip.

Additionally, there should probably be a note indicating that those operations can throw, and that serialize can execute user code and cause side effects.

domenic commented 4 years ago

Round-tripping is not necessarily a goal. And undefined is a valid stringification, if you pass it undefined.

andreubotella commented 4 years ago

undefined may be a valid stringification in Javascript land, but here Infra proceeds to UTF-8 encode it, which is not valid. And I wasn't talking so much about round-tripping of Infra values as that the output of serialization doesn't throw when deserialized.