jcrist / msgspec

A fast serialization and validation library, with builtin support for JSON, MessagePack, YAML, and TOML
https://jcristharif.com/msgspec/
BSD 3-Clause "New" or "Revised" License
2.45k stars 76 forks source link

maybe an exception should be thrown when there are bytes in the dict passed to json.encode #584

Closed bymoye closed 1 year ago

bymoye commented 1 year ago

Description

In the example, they are not equal because I used bytes for one of the fields in the dict:


temp = {
    "a": 1,
    "b": "2",
    "c": 3.0,
    "d": True,
    "f": [1, 2, 3],
    "g": b"123",
    "i": {
        "a": {"c": 3, "l": [2, 5]},
        "b": {"c": 3, "l": [2, 5]},
    },
}
a = msgspec.json.encode(temp)
b = msgspec.json.decode(a)
assert b == temp

maybe it should throw an exception like orjson: Type is not JSON serializable: bytes

jcrist commented 1 year ago

For JSON binary objects (bytes, bytearray, and memoryview) serialize as base64-encoded strings. This is intentional and is documented here. To decode back into a bytes object you'd need to provide a type to the decode method to tell msgspec to coerce the string back to a bytes object.