Replies: 3 comments 19 replies
-
Thanks for bringing up your specific problem. The best solution, as you say, would be to stack allocate these objects. The What we probably want to support is the ability to use auto obj = glz::object("pi", 3.141, "happy", true, "name", "Stephen");
std::string s{};
glz::write_json(obj, s); We aren't far from supporting this, but I'm not sure when I'll be able to add this feature. However, your request has put this on my radar and I do think it is worthwhile. It would make logging extremely efficient without needing to write glz::meta structures. Thanks for your feedback, sorry we don't have an immediate solution. But, I will turn this into an issue to address. |
Beta Was this translation helpful? Give feedback.
-
Dear stephenberry! If I created an object like this: glz::obj{"pi", 3.141, "happy", true, "name", "Stephen", "arr", glz::arr{"Hello", "World", 2}}); I believe there would be no dynamic memory allocations (I am not sure, only tested the performance, but like this it was significantly faster). Although on the other hand, if I created the object like this: std::vector<int> vec = {1, 2, 3};
std::map<std::string, int> map = {{"a", 1},{"b", 2},{"c", 3}};
glz::obj{"pi", 3.141, "happy", true, "name", "Stephen", "arr", glz::arr{"Hello", "World", 2},
"map", map,
"vector", vec}; The performance plummeted. ( but it might just be due to the object being larger itself...) glz::obj{"pi", 3.141, "happy", true, "name", "Stephen", "arr", glz::arr{"Hello", "World", 2},
"map", glz::map{{"a", 1},{"b", 2},{"c", 3}}}; Other difficulty arise when I tried to pass the glz::obj as a function parameter. Because of its variadic template definition it gets really hard to pass that object around between functions. template <typename... Args>
static std::string serialize_data(glz::obj<Args...> const &data)
{
return glz::write_json(data);
} Could you somehow change this type or maybe put a wrapper class/struct around it that might make it easier to use? Thank you for your time, looking forward to your answer! |
Beta Was this translation helpful? Give feedback.
-
Dear @stephenberry ! I have found a possible bug. When I have a std::string containing a json, and I would like to convert that to a glz supported object, so later on I could merge that object with an another. I did not find any easy way to achieve this. glz::obj temp = glz::parse_json_str(string_json);
//found this approach
glz::json_t j;
glz::read_json(j,string_json); This worked, however, when I tried to std::merge glz::obj and glz::json_t object (where the glz::json_t contained a MAP) parsing problems happened. This could be due to glz::obj not handling map types. But would be nice it these 2 types could be merged together easily OR make a glz::parse_json_str like function that immediately makes a glz::obj, and handles map types inside the string as well. Made a small demo that illustrates the problem: int main() {
glz::obj o {"not", "important"};
glz::obj o2 {"map", glz::obj{"a", 1, "b", 2, "c", 3}};
std::string s = "{\"key1\":42,\"key2\":\"hello world\",\"map\":\"key\",\"v\":[1,2,3],\"m\":{\"a\":1,\"b\":2,\"c\":3}}";
glz::json_t j;
glz::read_json(j,s);
auto test1 = glz::write_json(j);
auto merged = glz::merge{o, j};
auto test2 = glz::write_json(merged);
auto merged2 = glz::merge{o, o2};
auto test3 = glz::write_json(merged2);
fmt::print("{}\n", test1);
fmt::print("{}\n", test2);
fmt::print("{}\n", test3);
// {"key1":42,"key2":"hello world","m":{"a":1,"b":2,"c":3},"map":"key","v":[1,2,3]}
// {"not":"important","key1":42,"key2":"hello world","m":"a":1,"b":2,"c":3,"map":"key","v":[1,2,3]}
// {"not":"important","map":{"a":1,"b":2,"c":3}
} Thank you for reading my message, I am looking forward to your reply for this and my previous comment. |
Beta Was this translation helpful? Give feedback.
-
In my current assignment, I would like to construct JSON objects on the fly for logging purposes. It would be necessary so that I can log anything and everything without much effort or just for debugging purposes.
Performance is really important in my case so I choose glaze as my ser/deserialization/json creation library.
When creating a json object like this:
It is said in the description of the "If absolutely nothing is known about the JSON structure, then [glz::json_t] may be helpful, but it comes at a hefty performance cost due to the heavy use of dynamic memory allocations." Indeed, after testing it out, this approached seemed slower than a purely stack memory allocated libraries solution (boost::json::value). I know it is not fair to compare these 2 options, but that is why I wrote this.
Is there a way for me to construct glaze::json objects faster and with less memory allocations? Could the smaller object be purely stack allocated and for bigger objects maybe pre-allocated heap could be a solution. If a solution already exists, please give me a short example.
Thank you for your time!
Beta Was this translation helpful? Give feedback.
All reactions