Partial object write for JSON string. #204
-
Hi is there a way to write out a partial json string? I see you can do it for binary output but would be very useful for as string as well.
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
If I remember correctly it was not added because unlike the binary spec there was not a good way of handling a partial where and element in an array was requested. Take the structure: {
"foo": [0,1,2,3,4,5,6,7,8,9],
"bar": true,
"biz": {
"x": 1.2,
"y": 3.7,
"z": 1009,
"w": 3.14
}
} The following partial would be straightforward and obvious. static constexpr auto partial = glz::json_ptrs("/bar","/biz/y"); {
"bar": true,
"biz": {
"y": 3.7
}
} But lets say I include an array index in the json pointer static constexpr auto partial = glz::json_ptrs("/foo/5","/biz/y"); What does the partial message look like? Writing out the whole array results in allot of extra stuff {
"foo": [0,1,2,3,4,5,6,7,8,9],
"biz": {
"y": 3.7
}
} The following solves the problem but is not really standard json for handling this {
"/biz/y": 3.7,
"/foo/5": 4
} As a side note: I agree that being able to customize messages would be nice but am unsure of the best way to enable this. I'm open to api suggestions. |
Beta Was this translation helpful? Give feedback.
-
Hi I guess one way would be to state that traversal of the json structure is only valid with string keys. I don't view an index of an array as an explicit key of json. So in the case of "/foo/5" it should be invalid. |
Beta Was this translation helpful? Give feedback.
-
Partial JSON write support has been added in v2.2.2 (#815) struct animals_t
{
std::string lion = "Lion";
std::string tiger = "Tiger";
std::string panda = "Panda";
struct glaze
{
using T = animals_t;
static constexpr auto value = glz::object(&T::lion, &T::tiger, &T::panda);
};
};
struct zoo_t
{
animals_t animals{};
std::string name{"My Awesome Zoo"};
struct glaze
{
using T = zoo_t;
static constexpr auto value = glz::object(&T::animals, &T::name);
};
};
"partial write"_test = [] {
static constexpr auto partial = glz::json_ptrs("/name", "/animals/tiger");
zoo_t obj{};
std::string s{};
const auto ec = glz::write_json<partial>(obj, s);
expect(!ec);
expect(s == R"({"animals":{"tiger":"Tiger"},"name":"My Awesome Zoo"})") << s;
}; |
Beta Was this translation helpful? Give feedback.
Partial JSON write support has been added in v2.2.2 (#815)