Skip to content

Latest commit

 

History

History
87 lines (71 loc) · 2.29 KB

ojson.md

File metadata and controls

87 lines (71 loc) · 2.29 KB

jsoncons::ojson

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.

Header

#include <jsoncons/json.hpp>

Interface

The interface is the same as json, with these provisos:

  • ojson, like json, supports object member insert_or_assign methods that take an object_iterator as the first parameter. But while with json that parameter is just a hint that allows optimization, with ojson it is the actual location where to insert the member.

  • In ojson, the insert_or_assign members that just take a name and a value always insert the member at the end.

See also

  • 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

Examples

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"
}