stephenberry / glaze

Extremely fast, in memory, JSON and interface library for modern C++
MIT License
1.13k stars 115 forks source link

support for std::bitset<> #508

Closed WillisMedwell closed 10 months ago

WillisMedwell commented 11 months ago

Support for std::bitset

auto nums = std::bitset<10>(2);
std::cout << glz::write_json(nums) << '\n'; 

fantastic library btw!

justend29 commented 11 months ago

Specifically, how would you want that serialized and how would you want that deserialized?

A string of 1's & 0's or an array of 1's & 0's would both make sense for humans. As for compactness, an array of the largest possible integers or a string of bytes, not characters, would be the smallest.

stephenberry commented 11 months ago

I feel like a string of 1s and 0s would be a good balance of human readable and more compact than an array.

justend29 commented 11 months ago

Seems reasonable. If MSB is on the left, it would be enacting the same behaviour as to_string().

WillisMedwell commented 11 months ago

I think a string is a good approach, as you could also add a decorator (suffix/prefix) if you wanted to as well. The choice of a decorator, and which one to use, is up to you guys. For binary representations, I would think '0b' would be a good option.

stephenberry commented 10 months ago

Yeah, matching to_string() behavior like "10101100" would be great. We could allow "0b10101100", but I'm a bit cautious about this, because any time you allow multiple formats, it becomes more confusing to document and is slower if this is a runtime option. A user could easily add their own custom serializer/de-serializer to include 0b. But, I would just have Glaze read/write as "10101100" for now.

WillisMedwell commented 10 months ago

Yeah on second thought, thats a good call

justend29 commented 10 months ago

@stephenberry Supporting bitset for binary formats seems like a no-brainer. Do bitsets map to BEVE?

stephenberry commented 10 months ago

Bitsets do map to BEVE. From the spec:

Boolean arrays are stored using single bits for booleans and packed to the nearest byte.
stephenberry commented 10 months ago

Support has been added for JSON and BEVE read/write of std::bitset as of #526