typedef basic_json<char,
ImplementationPolicy = original_order_policy,
Allocator = std::allocator<char>> ojson
The 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.
The jsoncons
library will always rebind the supplied allocator from the template parameter to internal data structures.
#include <jsoncons/json.hpp>
The interface is the same as json, with these provisos:
-
ojson
, likejson
, supports object memberinsert_or_assign
methods that take anobject_iterator
as the first parameter. But while withjson
that parameter is just a hint that allows optimization, withojson
it is the actual location where to insert the member. -
In
ojson
, theinsert_or_assign
members that just take a name and a value always insert the member at the end.
-
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
ojson o = ojson::parse(R"(
{
"street_number" : "100",
"street_name" : "Queen St W",
"city" : "Toronto",
"country" : "Canada"
}
)");
std::cout << pretty_print(o) << std::endl;
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) << std::endl;
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) << std::endl;
Output:
{
"street_number": "100",
"street_name": "Queen St W",
"city": "Toronto",
"province": "Ontario",
"country": "Canada",
"postal_code": "M5H 2N2"
}