Skip to content

Conversation

@kaladron
Copy link
Contributor

Add byte_array wrapper for explicit fixed-size character array serialization. This introduces glz::byte_array.

Currently:
char arr[4] = {0, 0, 1, 0};
std::string json;
glz::write_json(arr, json);
// Previously: json == """" (empty string - WRONG) // Expected: json == ""\u0000\u0000\u0001\u0000"" (desired behavior)

This happens because there's no way to distinguish between a regular character array and a byte array in C/C++.

This introduces:

char arr[4] = {0, 0, 1, 0};

// Traditional behavior (unchanged)
glz::write_json(arr, json); // → ""

// NEW: Explicit byte array behavior
glz::write_json(glz::byte_array{arr}, json); // → "\u0000\u0000\u0001\u0000"

Tested:
Null-only arrays: glz::byte_array{char[3]{0,0,0}} → "\u0000\u0000\u0000" Mixed content: glz::byte_array{char[4]{'a',0,'b','\n'}} → "a\u0000b\n" Normal strings: glz::byte_array{char[5]{'h','e','l','l','o'}} → "hello" Round-trip: Full serialization and deserialization support

Add byte_array wrapper for explicit fixed-size character array
serialization.  This introduces glz::byte_array.

Currently:
char arr[4] = {0, 0, 1, 0};
std::string json;
glz::write_json(arr, json);
// Previously: json == "\"\""  (empty string - WRONG)
// Expected: json == "\"\\u0000\\u0000\\u0001\\u0000\""  (desired behavior)

This happens because there's no way to distinguish between a regular
character array and a byte array in C/C++.

This introduces:

char arr[4] = {0, 0, 1, 0};

// Traditional behavior (unchanged)
glz::write_json(arr, json);                    // → ""

// NEW: Explicit byte array behavior
glz::write_json(glz::byte_array{arr}, json);   // → "\u0000\u0000\u0001\u0000"

Tested:
Null-only arrays: glz::byte_array{char[3]{0,0,0}} → "\u0000\u0000\u0000"
Mixed content: glz::byte_array{char[4]{'a',0,'b','\n'}} → "a\u0000b\n"
Normal strings: glz::byte_array{char[5]{'h','e','l','l','o'}} → "hello"
Round-trip: Full serialization and deserialization support
@kaladron
Copy link
Contributor Author

This was built in collaboration with GitHub Copilot. My apologies if I got something wrong. I'm also happy to take a different approach if you'd rather.

@stephenberry
Copy link
Owner

@kaladron, this approach works. But, what you're wanting is already supported by wrapping your char array in a std::string_view and then using the option: escape_control_characters

char raw[4] = {0, 0, 1, 0};
std::string_view view{raw, sizeof(raw)};

std::string buffer{};
expect(not glz::write<opts_escape_control_characters{}>(view, buffer));

expect(buffer == R"("\u0000\u0000\u0001\u0000")");

I do think an explicitly named wrapper type would make sense. But, I'm not sure what to name it. I think glz::byte_array is a little too generic, and doesn't sound like a slow, auto escaping wrapper. Maybe something like glz::escape_bytes_json. What do you think?

@kaladron
Copy link
Contributor Author

I got a bit swamped this week - I'll get to this shortly. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants