2#ifndef AUROSTD_XPARSER_JSON_H
3#define AUROSTD_XPARSER_JSON_H
18#include "aurostd_type_traits.tpp"
30 template <
typename T>
using is_string_like = std::is_same<std::decay_t<T>, std::string>;
31 template <
typename T>
using is_json_list_like = std::conjunction<aurostd::is_list_like<T>, std::negation<is_string_like<T>>>;
32 template <
typename T>
using enable_list_like = std::enable_if_t<is_json_list_like<T>::value,
bool>;
33 template <
typename T>
using enable_dict_like = std::enable_if_t<aurostd::is_dict_like_v<T>,
bool>;
34 template <
typename T>
using enable_serializable = std::enable_if_t<std::is_base_of_v<JsonSerializable<T>,
T>,
bool>;
35 template <
typename T>
using enable_arithmetic = std::enable_if_t<std::is_arithmetic_v<T>,
bool>;
37 typedef std::vector<object>
List;
53 std::shared_ptr<void>
obj =
nullptr;
78 object(
const char* content);
79 object(
const std::string& content);
81 object(std::nullptr_t content);
85 template <
typename utype, enable_arithmetic<utype> = true>
object(utype content);
87 template <
typename utype>
object(
const std::vector<utype>& content);
88 template <
typename utype>
object(
const std::deque<utype>& content);
89 template <
typename utype>
object(
const std::map<std::string, utype>& content);
93 template <
typename T, enable_list_like<T> = true>
object(
const T& content);
94 template <
typename T, enable_dict_like<T> = true>
object(
const T& content);
97 explicit operator bool()
const;
98 explicit operator std::string()
const;
99 explicit operator double()
const;
100 explicit operator long double()
const;
101 explicit operator float()
const;
102 explicit operator long long()
const;
103 explicit operator long()
const;
104 explicit operator int()
const;
105 explicit operator char()
const;
106 explicit operator unsigned long long()
const;
107 explicit operator unsigned long()
const;
108 explicit operator unsigned int()
const;
112 template <
class utype>
operator std::vector<utype>()
const;
113 template <
class utype>
operator std::deque<utype>()
const;
114 template <
class utype>
operator std::set<utype>()
const;
115 template <
class utype>
operator std::map<std::string, utype>()
const;
118 template <
class T, enable_list_like<T> = true>
explicit operator T()
const;
119 template <
class T, enable_dict_like<T> = true>
explicit operator T()
const;
120 template <
class T, enable_serializable<T> = true>
explicit operator T()
const;
125 [[nodiscard]]
size_t size()
const;
126 [[nodiscard]]
bool empty()
const;
127 [[nodiscard]]
size_t count(
const std::string&)
const;
128 [[nodiscard]] std::map<std::string, JSON::object>::iterator
find(
const std::string& key)
const;
129 template <
class utype>
typename std::list<utype>::iterator
find_list(
const utype& key)
const;
130 [[nodiscard]] std::map<std::string, JSON::object>::iterator
end()
const;
131 [[nodiscard]] std::map<std::string, JSON::object>::iterator
begin()
const;
132 [[nodiscard]] std::vector<std::string>
keys()
const;
140 template <
class utype>
void fromVector(
const std::vector<utype>& content);
141 template <
class utype>
void fromDeque(
const std::deque<utype>& content);
142 template <
class utype>
void fromMap(
const std::map<std::string, utype>& content);
145 template <
class T, enable_list_like<T> = true>
void fromListLike(
const T& content);
146 template <
class T, enable_dict_like<T> = true>
void fromDictLike(
const T& content);
147 template <
class T, enable_list_like<T> = true>
T toListLike()
const;
148 template <
class T, enable_dict_like<T> = true>
T toDictLike()
const;
150 [[nodiscard]] std::string
toString(
bool json_format =
true,
bool escape_unicode =
true)
const;
156 std::string
escape(
const std::string& raw,
bool unicode =
true);
161 object loadFile(
const std::string& file_path);
162 object loadString(
const std::string& content);
164 std::string
toString(
const object& root,
bool escape_unicode =
false);
167 std::pair<size_t, size_t>
find_string(
const std::string& raw_content, std::pair<size_t, size_t> border = {0, 0});
168 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});
169 std::pair<size_t, size_t>
find_strip(
const std::string& raw_content, std::pair<size_t, size_t> border = {0, 0});
172 object parse(
const std::string& raw_content, std::pair<size_t, size_t> border = {0, 0});
173 std::string
parse_string(
const std::string& raw_content, std::pair<size_t, size_t> border = {0, 0});
181#include "aurostd_xparser_json.tpp"
Interface for Json serialization functionality using CRTP.
unified namespace to read and write JSON
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::enable_if_t< std::is_arithmetic_v< T >, bool > enable_arithmetic
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::string toString(const object &root, bool escape_unicode=false)
convert JSON::object to string
std::enable_if_t< aurostd::is_dict_like_v< T >, bool > enable_dict_like
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 parse_string(const std::string &raw_content, std::pair< size_t, size_t > border={0, 0})
parse JSON string
std::map< std::string, object > Dictionary
shortcut for JSON::object_types::DICTIONARY
std::enable_if_t< std::is_base_of_v< JsonSerializable< T >, T >, bool > enable_serializable
std::enable_if_t< is_json_list_like< T >::value, bool > enable_list_like
object parse(const std::string &raw_content, std::pair< size_t, size_t > border={0, 0})
parse a raw JSON string
std::string unescape_unicode(const std::string &raw, size_t &pos)
unescape JSON unicode instances
std::vector< object > List
shortcut for JSON::object_types::LIST
std::string char32_to_string(char32_t cp)
convert a unicode codepoint to a series of utf8 chars
object loadString(const std::string &content)
create a JSON::object from raw string
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 loadFile(const std::string &file_path)
create a JSON::object from file
std::is_same< std::decay_t< T >, std::string > is_string_like
std::string escape(const std::string &raw, bool unicode=true)
prepare string for JSON output with or without Unicode escapes
std::conjunction< aurostd::is_list_like< T >, std::negation< is_string_like< T > > > is_json_list_like
std::string char_escape(char16_t c)
escape characters to JSON
b64_encoder_proxy operator<<(std::ostream &os, b64_encoder_creator)
storge container for a JSON object
JSON::object & operator=(const xvector< utype > &content)
JSON::object & operator=(const xcomplex< utype > &content)
void fromListLike(const T &content)
size_t count(const std::string &) const
count the occurrence of key string in a JSON::object_type::DICTIONARY
JSON::object & operator[](size_t index) const
direct index access to JSON::object_types::LIST objects
void fromDictionary(const Dictionary &content)
change this JSON::object to a JSON::object_types::DICTIONARY
std::vector< std::string > keys() const
returns a vector of all keys in a JSON::object_type::DICTIONARY
JSON::object & operator=(utype content)
void fromDeque(const std::deque< utype > &content)
size_t size() const
gives the size of JSON::object_tpe::LIST, JSON::object_tpe::DICTIONARY or JSON::object_tpe::STRING
object(const JsonSerializable< T > &content)
JSON::object & operator=(const JsonSerializable< utype > &content)
void fromDictLike(const T &content)
void fromMap(const std::map< std::string, utype > &content)
JSON::object & operator=(const char *content)
assignment operator for char
void fromList(const List &content)
change this JSON::object to a JSON::object_types::LIST
std::map< std::string, JSON::object >::iterator begin() const
returns an iterator to the begin() of a JSON::object_type::DICTIONARY
std::map< std::string, JSON::object >::iterator find(const std::string &key) const
returns an iterator to the result of find in JSON::object_type::DICTIONARY
JSON::object & operator=(const std::deque< utype > &content)
object(const std::deque< utype > &content)
void fromNumber(utype content)
void fromXvector(const xvector< utype > &content)
void fromComplex(const xcomplex< utype > &content)
void push_back(const JSON::object &content) const
allow to append to JSON::object_type::LIST
void fromVector(const std::vector< utype > &content)
JSON::object & operator=(const T &content)
std::shared_ptr< void > obj
std::string toString(bool json_format=true, bool escape_unicode=true) const
converts a JSON::object into a string
object(const Dictionary &content)
object(const xcomplex< utype > &content)
std::map< std::string, JSON::object >::iterator end() const
returns an iterator to the end() of a JSON::object_type::DICTIONARY
void fromString(const std::string &content)
change this JSON::object to a JSON::object_types::STRING
bool empty() const
checks if JSON::object_tpe::LIST, JSON::object_tpe::DICTIONARY or JSON::object_tpe::STRING is empty
object(const std::vector< utype > &content)
void join(const JSON::object &content) const
allow to merge two dictionaries or lists
object(const List &content)
std::list< utype >::iterator find_list(const utype &key) const
JSON::object & operator=(const std::vector< utype > &content)
void saveFile(const std::string &file_path, compression_type ct=compression_type::None, bool escape_unicode=true) const
save JSON::object to file
void fromXmatrix(const xmatrix< utype > &content)
JSON::object & operator=(const std::map< std::string, utype > &content)
object(const xmatrix< utype > &content)
JSON::object & operator=(const xmatrix< utype > &content)
object(const std::map< std::string, utype > &content)
object(const xvector< utype > &content)