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.01k stars 59 forks source link

Implementing optional bytes type for json. #650

Closed dg-pb closed 4 months ago

dg-pb commented 4 months ago

Description

So the feature is simple. To OPTIONALLY allow raw, unencoded bytes type to be a value of json. Something along the lines of:

{
  "a": 1,
  "bytes_key": b"<length_of_bytes>:<byte_stream>"
}

I have already created an issue and laid out my reasoning in yyjson github as I was using orjson and tried implementing it there. However, I am switching to msgspec due to its speed, the fact that it does not depend on other libraries and other benefits.

See: https://github.com/ibireme/yyjson/issues/160

Any chance this could be considered?

jcrist commented 4 months ago

Sorry, but I won't be adding this. msgspec.json implements a standards compliant JSON parser, not some custom JSON-like format. If you want to efficiently serialize/deserialize bytes objects I recommend using msgspec.msgpack instead (as recommended in that issue). It has native support for bytes/bytearray/memoryview` objects, and even can support zero-copy when decoding them for cases where that makes sense (https://jcristharif.com/msgspec/supported-types.html#bytes-bytearray-memoryview).