#include <jsoncons/json.hpp>
typedef basic_json<char,
Policy = order_preserving_policy,
Allocator = std::allocator<char>> ojsonThe ojson class is an instantiation of the basic_json class template that uses char as the character type. The original insertion order of an object's name/value pairs is preserved.
ojson behaves similarly to json, with these particularities:
-
ojson, likejson, supports object memberinsert_or_assignmethods that take anobject_iteratoras the first parameter. But while withjsonthat parameter is just a hint that allows optimization, withojsonit is the actual location where to insert the member. -
In
ojson, theinsert_or_assignmembers that just take a name and a value always insert the member at the end.
ojson o = ojson::parse(R"(
{
"street_number" : "100",
"street_name" : "Queen St W",
"city" : "Toronto",
"country" : "Canada"
}
)");
std::cout << pretty_print(o) << '\n';Output:
{
"street_number": "100",
"street_name": "Queen St W",
"city": "Toronto",
"country": "Canada"
}Insert "postal_code" at end
o.insert_or_assign("postal_code", "M5H 2N2");
std::cout << pretty_print(o) << '\n';Output:
{
"street_number": "100",
"street_name": "Queen St W",
"city": "Toronto",
"country": "Canada",
"postal_code": "M5H 2N2"
}Insert "province" before "country"
auto it = o.find("country");
o.insert_or_assign(it,"province","Ontario");
std::cout << pretty_print(o) << '\n';Output:
{
"street_number": "100",
"street_name": "Queen St W",
"city": "Toronto",
"province": "Ontario",
"country": "Canada",
"postal_code": "M5H 2N2"
}json constructs a json value that sorts name-value members alphabetically
wjson constructs a wide character json value that sorts name-value members alphabetically
wojson constructs a wide character json value that preserves the original insertion order of an object's name/value pairs