daggaz / json-stream

Simple streaming JSON parser and encoder.
MIT License
122 stars 18 forks source link

json-stream is unable to parse JSON consisting of only a primitive value #37

Closed jorektheglitch closed 1 year ago

jorektheglitch commented 1 year ago

Actual JSON format fully accepts just primitive objects.

JSON can represent four primitive types (strings, numbers, booleans, and null) and two structured types (objects and arrays). — RFC 8259: The JavaScript Object Notation (JSON) Data Interchange Format

So I expect that primitive JSON can be parsed succesfully, but got ValueError while trying to parse them.

Example:

from io import BytesIO
import json_stream

primitives = [
    b"true",
    b"123",
    b"12.3",
    b"'string'",
    b"null",
]
for primitive in primitives:
    f = BytesIO(primitive)
    try:
        json_stream.load(f)
    except Exception as e:
        print(f"Can't parse {primitive!r} due to {type(e).__name__}: {e}")

Gives following output:

Can't parse b'true' due to ValueError: Unknown operator True
Can't parse b'123' due to ValueError: Unknown operator 123
Can't parse b'12.3' due to ValueError: Unknown operator 12.3
Can't parse b"'string'" due to ValueError: Invalid JSON character: '\'' at index 0
Can't parse b'null' due to ValueError: Unknown operator None
daggaz commented 1 year ago

If you could have a look at my PR, I think it address your issue.

daggaz commented 1 year ago

@smheidrich look good to you?

smheidrich commented 1 year ago

@daggaz Yep looks good :+1:

jorektheglitch commented 1 year ago

@daggaz looks good, thank you When should I expect a version with this feature?

daggaz commented 1 year ago

@jorektheglitch version 2.3.0 just released!

Thanks again for pointing out this issue.