GenericContainer
GenericContainer a tool for C++ programming
Loading...
Searching...
No Matches
GenericContainer JSON Interface

This module provides interface with JSON files. More...

Functions

bool GC_namespace::file_JSON_to_GC (string const &file_name, GenericContainer &gc)
bool GC_namespace::JSON_to_GC (istream_type &stream, GenericContainer &gc)
bool GC_namespace::JSON_to_GC (string const &DATA, GenericContainer &gc)
void GC_namespace::GC_to_JSON (GenericContainer const &gc, std::string &res)
void GC_namespace::GC_to_JSON (GenericContainer const &gc, ostream_type &stream)

Detailed Description

This module provides interface with JSON files.

The GenericContainer (GC) module offers a flexible container for storing various data types such as booleans, integers, floating-point numbers, complex numbers, and strings. It also supports structured data types such as vectors and maps. The C++ interface allows for seamless interaction with the container, enabling users to perform operations like adding elements, retrieving data, and managing hierarchical structures.

Overview

This tutorial explains how to use the GenericContainer JSON interface for converting between JSON data and the GenericContainer object.

The GenericContainer is a flexible structure that can store various data types like booleans, integers, floating-point numbers, complex numbers, strings, vectors, and maps. The interface provides multiple ways to convert JSON files, streams, or strings into GenericContainer objects and vice versa.

Loading JSON Data into a GenericContainer

There are several ways to load JSON data into a GenericContainer.

Loading JSON from a File

The file_JSON_to_GC function reads JSON data from a file and populates a GenericContainer. This is useful for handling configuration files or other data stored in JSON format.

bool file_JSON_to_GC( std::string const & file_name_JSON, GenericContainer & gc );
bool file_JSON_to_GC(string const &file_name, GenericContainer &gc)
Definition GenericContainerInterface_json.hh:50

Example:

GenericContainer gc;
std::string file_name = "config.JSON";
if (file_JSON_to_GC(file_name, gc)) {
std::cout << "JSON file successfully loaded into GenericContainer." << std::endl;
} else {
std::cerr << "Failed to load JSON file." << std::endl;
}

Loading JSON from an Input Stream

The JSON_to_GC function allows loading JSON data from an input stream, such as a file stream or a string stream.

bool JSON_to_GC( istream_type & file_JSON, GenericContainer & gc );
bool JSON_to_GC(istream_type &stream, GenericContainer &gc)
Definition GenericContainerInterface_json.hh:71
std::basic_istream< char > istream_type
Alias for a character-based input stream.
Definition GenericContainer.hh:118

Example:

GenericContainer gc;
std::ifstream file_stream("config.JSON");
if (JSON_to_GC(file_stream, gc)) {
std::cout << "JSON stream successfully loaded into GenericContainer." << std::endl;
} else {
std::cerr << "Failed to load JSON stream." << std::endl;
}

Loading JSON from a String

You can convert a JSON string directly into a GenericContainer using the JSON_to_GC function.

bool JSON_to_GC( std::string const & JSON, GenericContainer & gc );

Example:

GenericContainer gc;
std::string JSON_data = R"(
{
"products": [
{
"id": 1,
"name": "Laptop",
"price": 999.99,
"in_stock": true
},
{
"id": 2,
"name": "Smartphone",
"price": 499.99,
"in_stock": false
}
],
"meta": {
"total_products": 2,
"available_products": 1
}
}
)";
if (JSON_to_GC(JSON_data, gc)) {
std::cout << "JSON string successfully loaded into GenericContainer." << std::endl;
} else {
std::cerr << "Failed to load JSON string." << std::endl;
}

Loading JSON from a Vector of Strings

If you have multiple JSON strings in a vector, you can load them into a GenericContainer using the JSON_to_GC function.

bool JSON_to_GC( std::vector<std::string> const & JSON, GenericContainer & gc );

Example:

GenericContainer gc;
std::vector<std::string> JSON_fragments = {
"{",
" \"products\": [",
" {",
" \"id\": 1,",
" \"name\": "Laptop",",
" \"price\": 999.99,",
" \"in_stock\": true",
" },",
" {",
" \"id\": 2,",
" \"name\": "Smartphone",",
" \"price\": 499.99,",
" \"in_stock\": false",
" }",
" ],",
" \"meta\": {",
" "total_products": 2,",
" "available_products": 1",
" }",
"}"
};
if (JSON_to_GC(JSON_fragments, gc)) {
std::cout << "JSON fragments successfully loaded into GenericContainer." << std::endl;
} else {
std::cerr << "Failed to load JSON fragments." << std::endl;
}

Converting a GenericContainer to JSON

You can also convert a GenericContainer back to JSON format.

Converting to a Vector of JSON Strings

The GC_to_JSON function converts the contents of a GenericContainer into a vector of JSON strings.

void GC_to_JSON( GenericContainer const & gc, std::vector<std::string> & JSON );
void GC_to_JSON(GenericContainer const &gc, std::string &res)
Definition GenericContainerInterface_json.hh:111

Example:

GenericContainer gc;
gc["key1"] = "value1";
gc["key2"] = 42;
std::vector<std::string> JSON_output;
GC_to_JSON(gc, JSON_output);
for (const auto &line : JSON_output) {
std::cout << line << std::endl;
}

Converting to a JSON File Stream

You can write the contents of a GenericContainer to an output stream (e.g., a file) in JSON format using the GC_to_JSON function.

void GC_to_JSON( GenericContainer const & gc, ostream_type & file_JSON );
std::basic_ostream< char > ostream_type
Alias for a character-based output stream.
Definition GenericContainer.hh:109

Example:

GenericContainer gc;
gc["key1"] = "value1";
gc["key2"] = 42;
std::ofstream file_stream("output.JSON");
GC_to_JSON(gc, file_stream);
std::cout << "JSON data written to output.JSON" << std::endl;

Functions

◆ file_JSON_to_GC()

bool GC_namespace::file_JSON_to_GC ( string const & file_name,
GenericContainer & gc )
inline

Convert a JSON file to a GenericContainer.

This function reads JSON data from the provided input stream and populates the given GenericContainer with the parsed data.

Parameters
[in]file_nameInput file name containing JSON data.
[out]gcThe GenericContainer to be populated.
Returns
true if the conversion was successful, false otherwise.

◆ GC_to_JSON() [1/2]

void GC_namespace::GC_to_JSON ( GenericContainer const & gc,
ostream_type & stream )
inline

Convert a GenericContainer to a JSON file stream.

This function converts the contents of the provided GenericContainer into JSON format and writes it to the specified output stream.

Parameters
[in]gcThe GenericContainer to convert.
[out]streamOutput stream to write the JSON data.

◆ GC_to_JSON() [2/2]

void GC_namespace::GC_to_JSON ( GenericContainer const & gc,
std::string & res )
inline

Convert a GenericContainer to a JSON string.

This function converts the contents of the provided GenericContainer into JSON format and stores it in the specified string.

Parameters
[in]gcThe GenericContainer to convert.
[out]DATAString to store the JSON encoded GenericContainer.

◆ JSON_to_GC() [1/2]

bool GC_namespace::JSON_to_GC ( istream_type & stream,
GenericContainer & gc )
inline

Convert a JSON file stream to a GenericContainer.

This function reads JSON data from the provided input stream and populates the given GenericContainer with the parsed data.

Parameters
[in]streamInput stream containing JSON data.
[out]gcThe GenericContainer to be populated.
Returns
true if the conversion was successful, false otherwise.

◆ JSON_to_GC() [2/2]

bool GC_namespace::JSON_to_GC ( string const & DATA,
GenericContainer & gc )
inline

Convert a JSON string to a GenericContainer.

This function parses the given JSON string and populates the specified GenericContainer with the resulting data.

Parameters
[in]DATAThe JSON string to parse.
[out]gcThe GenericContainer to be populated.
Returns
true if the conversion was successful, false otherwise.