AFLOW
 
Loading...
Searching...
No Matches
aurostd::JSON Namespace Reference

unified namespace to read and write JSON More...

Classes

struct  object
 storge container for a JSON object More...
 

Typedefs

template<typename T>
using is_string_like = std::is_same<std::decay_t<T>, std::string>
 
template<typename T>
using is_json_list_like = std::conjunction<aurostd::is_list_like<T>, std::negation<is_string_like<T>>>
 
template<typename T>
using enable_list_like = std::enable_if_t<is_json_list_like<T>::value, bool>
 
template<typename T>
using enable_dict_like = std::enable_if_t<aurostd::is_dict_like_v<T>, bool>
 
template<typename T>
using enable_serializable = std::enable_if_t<std::is_base_of_v<JsonSerializable<T>, T>, bool>
 
template<typename T>
using enable_arithmetic = std::enable_if_t<std::is_arithmetic_v<T>, bool>
 
typedef std::vector< objectList
 shortcut for JSON::object_types::LIST
 
typedef std::map< std::string, objectDictionary
 shortcut for JSON::object_types::DICTIONARY
 

Enumerations

enum class  object_types {
  DICTIONARY , LIST , STRING , FLOAT ,
  INTEGER , T , F , NONE
}
 

Functions

std::string unescape_unicode (const std::string &raw, size_t &pos)
 unescape JSON unicode instances
 
std::string escape (const std::string &raw, bool unicode=true)
 prepare string for JSON output with or without Unicode escapes
 
std::string char32_to_string (char32_t cp)
 convert a unicode codepoint to a series of utf8 chars
 
std::string char_escape (char16_t c)
 escape characters to JSON
 
object loadFile (const std::string &file_path)
 create a JSON::object from file
 
object loadString (const std::string &content)
 create a JSON::object from raw string
 
void saveFile (const object &root, const std::string &file_path, compression_type ct=compression_type::None, bool escape_unicode=true)
 save JSON::object to file
 
std::string toString (const object &root, bool escape_unicode=false)
 convert JSON::object to string
 
std::pair< size_t, size_t > find_string (const std::string &raw_content, std::pair< size_t, size_t > border={0, 0})
 find the border of a JSON string
 
std::pair< size_t, size_t > find_bracket (const std::string &raw_content, char kind_open, std::pair< size_t, size_t > border={0, 0})
 find the border of an encapsulated by brackets
 
std::pair< size_t, size_t > find_strip (const std::string &raw_content, std::pair< size_t, size_t > border={0, 0})
 strip whitespaces
 
object parse (const std::string &raw_content, std::pair< size_t, size_t > border={0, 0})
 parse a raw JSON string
 
std::string parse_string (const std::string &raw_content, std::pair< size_t, size_t > border={0, 0})
 parse JSON string
 

Detailed Description

unified namespace to read and write JSON

Authors
2020 | AS - created JSONWriter
20220924 | Hagen Eckert - rewrite to include parsing
See also
"JSON definition"
"Parsing JSON is a Minefield"

Load data example

jo = aurostd::JSON::loadFile("testcases.json");
// output complete file as JSON
cout << jo.toString() << endl;
// output element (alternative to .toSring())
cout << (string) jo["xvector"][3] << endl;
// save element
xvector<double> xvd = jo["xvector"];
cout << xvd << endl;
// save sub element
uint el_uint = jo["xvector"][3];
cout << el_uint << endl;
// cast to type
cout << (float) jo["xvector"][3] << endl;
// iterate over Dictionary
for (const auto & [key, value]: static_cast<aurostd::JSON::Dictionary>(jo["a_dict"])) {
cout << key << " | " << (string) value << endl;
}
// iterate over List
for (const auto & value: static_cast<aurostd::JSON::List>(jo["a_list"])) {
cout << (string) value << endl;
}
unsigned uint
Definition aurostd.h:39
std::map< std::string, object > Dictionary
shortcut for JSON::object_types::DICTIONARY
std::vector< object > List
shortcut for JSON::object_types::LIST
object loadFile(const std::string &file_path)
create a JSON::object from file
storge container for a JSON object
std::string toString(bool json_format=true, bool escape_unicode=true) const
converts a JSON::object into a string

Save data example

jo["string"] = "Hello World";
jo["number"] = 4562318;
jo["null"] = nullptr;
jo["unicode"] = "🎃";
jo["vector"] = (vector<float>){2.3,5.6,34.6};
jo["xvector"] = (vector<double>){9.634,4.6,1E20};
cout << jo.toString() << endl;
jo.saveFile("testme.json");
// or
aurostd::JSON::saveFile(jo, "testme.json");
void saveFile(const object &root, const std::string &file_path, compression_type ct=compression_type::None, bool escape_unicode=true)
save JSON::object to file
void saveFile(const std::string &file_path, compression_type ct=compression_type::None, bool escape_unicode=true) const
save JSON::object to file

Typedef Documentation

◆ is_string_like

template<typename T>
using aurostd::JSON::is_string_like = std::is_same<std::decay_t<T>, std::string>

Definition at line 30 of file aurostd_xparser_json.h.

◆ is_json_list_like

template<typename T>
using aurostd::JSON::is_json_list_like = std::conjunction<aurostd::is_list_like<T>, std::negation<is_string_like<T>>>

Definition at line 31 of file aurostd_xparser_json.h.

◆ enable_list_like

template<typename T>
using aurostd::JSON::enable_list_like = std::enable_if_t<is_json_list_like<T>::value, bool>

Definition at line 32 of file aurostd_xparser_json.h.

◆ enable_dict_like

template<typename T>
using aurostd::JSON::enable_dict_like = std::enable_if_t<aurostd::is_dict_like_v<T>, bool>

Definition at line 33 of file aurostd_xparser_json.h.

◆ enable_serializable

template<typename T>
using aurostd::JSON::enable_serializable = std::enable_if_t<std::is_base_of_v<JsonSerializable<T>, T>, bool>

Definition at line 34 of file aurostd_xparser_json.h.

◆ enable_arithmetic

template<typename T>
using aurostd::JSON::enable_arithmetic = std::enable_if_t<std::is_arithmetic_v<T>, bool>

Definition at line 35 of file aurostd_xparser_json.h.

◆ List

typedef std::vector<object> aurostd::JSON::List

shortcut for JSON::object_types::LIST

Definition at line 37 of file aurostd_xparser_json.h.

◆ Dictionary

typedef std::map<std::string, object> aurostd::JSON::Dictionary

shortcut for JSON::object_types::DICTIONARY

Definition at line 38 of file aurostd_xparser_json.h.

Enumeration Type Documentation

◆ object_types

enum class aurostd::JSON::object_types
strong
Enumerator
DICTIONARY 
LIST 
STRING 
FLOAT 
INTEGER 
NONE 

Definition at line 40 of file aurostd_xparser_json.h.

Function Documentation

◆ unescape_unicode()

std::string aurostd::JSON::unescape_unicode ( const std::string & raw,
size_t & pos )

unescape JSON unicode instances

Parameters
rawraw content string
posstarting position of the hex representation
Returns
unescaped utf8 characters
Note
this function supports pairs
Authors
20221109 | Hagen Eckert - created

Definition at line 840 of file aurostd_xparser_json.cpp.

◆ escape()

std::string aurostd::JSON::escape ( const std::string & raw,
bool unicode = true )

prepare string for JSON output with or without Unicode escapes

Parameters
rawstring to escape
unicodeif true escape Unicode characters (default true)
Returns
JSON string
Authors
20221109 | Hagen Eckert - created

Definition at line 776 of file aurostd_xparser_json.cpp.

◆ char32_to_string()

std::string aurostd::JSON::char32_to_string ( char32_t cp)

convert a unicode codepoint to a series of utf8 chars

Parameters
cp32bit codepoint
Returns
utf8 representation
Authors
20221109 | Hagen Eckert - created

Definition at line 808 of file aurostd_xparser_json.cpp.

◆ char_escape()

std::string aurostd::JSON::char_escape ( char16_t c)

escape characters to JSON

Definition at line 756 of file aurostd_xparser_json.cpp.

◆ loadFile()

JSON::object aurostd::JSON::loadFile ( const std::string & file_path)

create a JSON::object from file

Parameters
file_pathfile path to load from
Returns
parsed JSON::object

Definition at line 1106 of file aurostd_xparser_json.cpp.

◆ loadString()

JSON::object aurostd::JSON::loadString ( const std::string & content)

create a JSON::object from raw string

Parameters
contentraw JSON content
Returns
parsed JSON::object

Definition at line 1114 of file aurostd_xparser_json.cpp.

◆ saveFile()

void aurostd::JSON::saveFile ( const object & root,
const std::string & file_path,
compression_type ct = compression_type::None,
bool escape_unicode = true )

save JSON::object to file

Parameters
rootJSON::object to save
file_pathfile path to save to
escape_unicodeif true escape Unicode characters

Definition at line 1130 of file aurostd_xparser_json.cpp.

◆ toString()

std::string aurostd::JSON::toString ( const object & root,
bool escape_unicode = false )

convert JSON::object to string

Parameters
rootJSON::object to convert
escape_unicodeif true escape Unicode characters
Returns
converted string

Definition at line 1122 of file aurostd_xparser_json.cpp.

◆ find_string()

std::pair< size_t, size_t > aurostd::JSON::find_string ( const std::string & raw_content,
std::pair< size_t, size_t > border = {0, 0} )

find the border of a JSON string

Parameters
raw_contentfull json string
borderworking border
Returns
string borders
Authors
20220924 | Hagen Eckert - created

Definition at line 884 of file aurostd_xparser_json.cpp.

◆ find_bracket()

std::pair< size_t, size_t > aurostd::JSON::find_bracket ( const std::string & raw_content,
char kind_open,
std::pair< size_t, size_t > border = {0, 0} )

find the border of an encapsulated by brackets

Parameters
raw_contentfull json string
kind_opentype of bracket [ { (
borderworking border
Returns
bracket borders
Authors
20220924 | Hagen Eckert - created

Definition at line 920 of file aurostd_xparser_json.cpp.

◆ find_strip()

std::pair< size_t, size_t > aurostd::JSON::find_strip ( const std::string & raw_content,
std::pair< size_t, size_t > border = {0, 0} )

strip whitespaces

Parameters
raw_contentfull json string
borderworking border
Returns
stripped raw string borders
Authors
20220924 | Hagen Eckert - created

Definition at line 869 of file aurostd_xparser_json.cpp.

◆ parse()

JSON::object aurostd::JSON::parse ( const std::string & raw_content,
std::pair< size_t, size_t > border = {0, 0} )

parse a raw JSON string

Parameters
raw_contentfull json string
borderworking border
Returns
parsed json object
Authors
20220924 | Hagen Eckert - created

Definition at line 971 of file aurostd_xparser_json.cpp.

◆ parse_string()

std::string aurostd::JSON::parse_string ( const std::string & raw_content,
std::pair< size_t, size_t > border = {0, 0} )

parse JSON string

Parameters
raw_contentfull json string
borderstring borders
Returns
c++ string
Authors
20220924 | Hagen Eckert - created

Definition at line 696 of file aurostd_xparser_json.cpp.