vivkin / gason

Lightweight and fast JSON parser for C++
MIT License
338 stars 51 forks source link

can JsonValue support std::string #11

Closed qicosmos closed 9 years ago

qicosmos commented 9 years ago

JsonValue save the address of pointer, but if the value is std::string, and maybe it is a temporary variables, in this situation, the address is held by JsonValue is valid.

how to reslove the problem?

vivkin commented 9 years ago

All JsonValue's valid until JsonAllocator alive. In this situation allocating buffer from allocator and copy string to them can solve the problem

qicosmos commented 9 years ago

hi, thanks for your reply, could you give some sample about the solution?

vivkin commented 9 years ago
std::string source = "{\"foo\" : \"bar\"}";

JsonAllocator allocator;
memcpy(allocator.allocate(source.size() + 1), source.c_str(), source.size() + 1);

char *endptr;
JsonValue value;
int status = jsonParse(source, &endptr, &value, allocator);
// ... and use value as usual
qicosmos commented 9 years ago

thanks for your reply, however the solution is a little complicated, indeed i want to make a dynamic type by nan-boxing, actually lua, js, use nan-boxing to create dynamic type, so i think c++ also can do this. just like this: dynamic a = 1; dynamic b = 2.5; dynamic c = "hello world"; //more natural and easy to use it is a problem for string, cause the address maybe invalid, maybe need reference count to solve the problem.