cereal icon indicating copy to clipboard operation
cereal copied to clipboard

feature: represent char arrays as strings in json

Open amiartus opened this issue 5 years ago • 3 comments

Hello,

This feature allows one dimensional array to be represented as string in json.

example output:

  1. g++ --std=c++17 m.cpp
{
    "value0": {
        "name": {
            "value0": 97,
            "value1": 100,
            "value2": 97,
            "value3": 109,
            "value4": 0,
            "value5": 0,
            "value6": 0,
            "value7": 0
        }
    }
}
  1. g++ -DCEREAL_NAMED_VARIANT --std=c++17 m.cpp
{
    "value0": {
        "name": "adam"
    }
}

multidimensional array would look like:

{
    "value0": {
        "name": {
            "value0": "adam",
            "value1": "mada"
        }
    }
}

code example: https://gist.github.com/miartad/83ff9033da1080e55103e12e7763c772

amiartus avatar Jan 31 '21 14:01 amiartus

What's the point of not using a std::string to begin with?

I think this pull request should be rejected since:

  • It changes global behavior based on a preprocessor directive.
  • Its benefit is dubious.

WSoptics avatar Feb 15 '21 14:02 WSoptics

In my use case I'm using fixed size char arrays to represent strings because std::string allocates heap. I guess it is a niche use case and will be deprecated when c++ std has some kind of support for stack allocated strings, which I dont think it currently has.

amiartus avatar Feb 15 '21 23:02 amiartus

In my use case I'm using fixed size char arrays to represent strings because std::string allocates heap. I guess it is a niche use case and will be deprecated when c++ std has some kind of support for stack allocated strings, which I dont think it currently has.

C++ does support this already through polymorphic memory resources, see std::pmr::string. So I guess the correct approach would to support those natively in cereal.

WSoptics avatar Feb 16 '21 07:02 WSoptics

std::string is stack-only for short strings (SSO). If used in a loop, put the string outside the loop and clear it once per iteration, this removes most heap allocations from the loop body.

std::string_view can be put on arbitrary binary storage.

StefanBruens avatar Jan 21 '23 19:01 StefanBruens