Metall  v0.28
A persistent memory allocator for data-centric analytics
json_create.cpp

This is an example of how to create a JSON object with Metall.

// Copyright 2021 Lawrence Livermore National Security, LLC and other Metall
// Project Developers. See the top-level COPYRIGHT file for details.
//
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
#include <iostream>
int main() {
std::string json_string = R"(
{
"pi": 3.141,
"happy": true,
"name": "Niels",
"nothing": null,
"answer": {
"everything": 42
},
"list": [1, 0, 2],
"object": {
"currency": "USD",
"value": 42.99
}
}
)";
std::cout << "Create" << std::endl;
metall::manager manager(metall::create_only, "./test");
auto *value = manager.construct<metall_value_type>(metall::unique_instance)(
metall::json::parse(json_string, manager.get_allocator()));
metall::json::pretty_print(std::cout, *value);
value->as_object()["name"].as_string() = "Alice"; // Change a string value
value->as_object()["temperature"] = 25.2; // Insert a double value
value->as_object()["unit"] = "celsius"; // Insert a string value
value->as_object().erase("pi"); // Erase a value
auto pos = value->as_object().find("happy");
std::cout << "Happy? : " << pos->value() << std::endl;
metall::json::pretty_print(std::cout, *value);
const auto clone(*value);
std::cout << (clone == *value) << std::endl;
return 0;
}
A generalized Metall manager class.
Definition: basic_manager.hpp:40
JSON value. A container that holds a single bool, int64, uint64, double, JSON string,...
Definition: value.hpp:82
int main()
Definition: json_create.cpp:10
basic_string< char > string
A string container that uses char as its character type and Metall as its default allocator.
Definition: string.hpp:23
void pretty_print(std::ostream &os, const value< allocator_type > &json_value)
Pretty-prints a JSON value.
Definition: pretty_print.hpp:75
value< allocator_type > parse(std::string_view input_json_string, const allocator_type &allocator=allocator_type())
Parses a JSON represented as a string.
Definition: parse.hpp:28
basic_manager<> manager
Default Metall manager class which is an alias of basic_manager with the default template parameters.
Definition: metall.hpp:34