pydantic / jiter

Fast iterable JSON parser.
https://crates.io/crates/jiter
MIT License
182 stars 11 forks source link

Optimistically close string values in from_json if allow_partial is set to true #100

Closed mwildehahn closed 3 months ago

mwildehahn commented 5 months ago

Right now if you're trying to parse a partial object, pydantic won't return the key until the string value is complete:

In [4]: pydantic_core.from_json('{"test": "this', allow_partial=True)
Out[4]: {}

I'd expect this to result in:

In [5]: pydantic_core.from_json('{"test": "this', allow_partial=True)
Out[5]: {'test': 'this'}

similarly to how objects and lists are optimistically closed during partial rendering.

adriangb commented 5 months ago

I'm not sure I agree. Lists are heterogenous: if a full list is valid, half a list should be as well. Objects are not. Cutting an object in half does not guarantee you can validate it.

mwildehahn commented 5 months ago

Hm, I'm specifically talking about closing strings. I mentioned objects because they are optimistically closed already:

In [4]: pydantic_core.from_json('{"test": "this", "something": {', allow_partial=True)
Out[4]: {'test': 'this', 'something': {}}

If a value of a key is a string, it will always be a string, optimistically closing that string just lets us serve a partial string faster.

adriangb commented 5 months ago

Ah sorry, misunderstood. That makes sense.

samuelcolvin commented 3 months ago

Just noticed this is a duplicate of #82.