You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I suspect this might be intentional since there can be cases where decoding in the face of UNSET values can present ambiguity, as least in the face of roundtripping, when attributes that can be UNSET are interspersed with those that cannot be UNSET.
However, I would argue that the developer should make judicious use of array_like=True to avoid such ambiguity. In other words, it seems reasonable to me that msgspec should indeed skip UNSET values in this case anyway, documenting that this is sort of a "use at your own risk of ambiguity" for the developer, particularly since decoding works without issue.
>>> msgspec.json.encode(pos)
Traceback (most recent call last):
...
TypeError: Encoding objects of type msgspec.UnsetType is unsupported
Instead of that TypeError, I expect encoding to result in b'[-3.70379, 40.416775]' since altitude is UNSET.
When I remove array_like=True (or set it explicitly to False), decoding (a JSON object in place of a JSON array, without a value for altitude) and encoding work as expected.
The text was updated successfully, but these errors were encountered:
chuckwondo
changed the title
JSON encoding fails to skip UNSET values in array_like Struct
JSON and msgpack encoding fails to skip UNSET values in array_like Struct
Aug 22, 2024
Description
Per the documentation of UNSET:
Unfortunately, this fails for Structs with
array_like=True
, resulting in the following error:This appears to be an oversight in the function json_encode_struct_array, which does not check for (and ignore) UNSET values, whereas json_encode_struct_object does ignore UNSET values, as expected.
I suspect this might be intentional since there can be cases where decoding in the face of UNSET values can present ambiguity, as least in the face of roundtripping, when attributes that can be UNSET are interspersed with those that cannot be UNSET.
However, I would argue that the developer should make judicious use of
array_like=True
to avoid such ambiguity. In other words, it seems reasonable to me that msgspec should indeed skip UNSET values in this case anyway, documenting that this is sort of a "use at your own risk of ambiguity" for the developer, particularly since decoding works without issue.For example, consider this struct:
Decoding works perfectly well:
Unfortunately, encoding does not:
Instead of that
TypeError
, I expect encoding to result inb'[-3.70379, 40.416775]'
sincealtitude
isUNSET
.When I remove
array_like=True
(or set it explicitly toFalse
), decoding (a JSON object in place of a JSON array, without a value foraltitude
) and encoding work as expected.The text was updated successfully, but these errors were encountered: