PIP 5.6.1
Platform-Independent Primitives
Public Types | Public Member Functions | Static Public Member Functions | List of all members
PIJSON Class Reference

JSON tree node. More...

#include <pijson.h>

Public Types

enum  Type {
  Invalid , Null , Boolean , Number ,
  String , Object , Array
}
 Type of JSON node. More...
 
enum  PrintType { Compact , Tree }
 Formatting mode for JSON text output. More...
 

Public Member Functions

 PIJSON ()
 Constructs invalid PIJSON.
 
 PIJSON (const PIJSON &o)=default
 Copy constructor.
 
const PIStringname () const
 Returns node name, or empty string for unnamed nodes.
 
const PIVector< PIJSON > & array () const
 Returns array children, or an empty array when the node is not PIJSON::Array.
 
const PIMap< PIString, PIJSON > & object () const
 Returns object members, or an empty map when the node is not PIJSON::Object.
 
const PIVariantvalue () const
 Returns scalar node value.
 
bool toBool () const
 Returns scalar value as bool.
 
int toInt () const
 Returns scalar value as integer.
 
double toDouble () const
 Returns scalar value as floating-point number.
 
PIString toString () const
 Returns scalar value as string.
 
Type type () const
 Returns node type.
 
bool isValid () const
 Returns whether the node is valid.
 
bool isObject () const
 Returns whether the node is PIJSON::Object.
 
bool isArray () const
 Returns whether the node is PIJSON::Array.
 
void setValue (const PIVariant &v)
 Sets scalar value and updates node type for bool, numeric and string variants. More...
 
void clear ()
 Clears node content and sets type to PIJSON::Invalid.
 
int size () const
 Returns array length or object member count. Other node types return 0.
 
bool contains (const PIString &key) const
 Returns whether an object node contains key key.
 
void resize (int new_size)
 Converts node to array and resizes it. New entries become empty strings.
 
PIJSONoperator= (const PIVariant &v)
 Synonym of setValue().
 
PIJSONoperator= (const PIJSON &v)
 Copy assignment operator.
 
const PIJSONoperator[] (int index) const
 Returns array element by index, or invalid PIJSON when the node is not an array.
 
PIJSONoperator[] (int index)
 Converts node to array, grows it when needed and returns element by index.
 
PIJSONoperator<< (const PIJSON &element)
 Converts node to array and appends another JSON node.
 
PIJSONoperator<< (const PIVariant &value)
 Converts node to array and appends a scalar value.
 
PIJSON operator[] (const PIString &key) const
 Returns object member by key, or invalid PIJSON when the node is not an object or the key is absent.
 
PIJSONoperator[] (const PIString &key)
 Converts node to object and returns member by key, creating it when needed.
 
PIJSON operator[] (const char *key) const
 Convenience overload for UTF-8 key access.
 
PIJSONoperator[] (const char *key)
 Convenience overload for UTF-8 key access.
 
PIString toJSON (PrintType print_type=Compact, bool mask_unicode=true) const
 Returns JSON text representation of the node tree.
 

Static Public Member Functions

static PIJSON fromJSON (PIString str)
 Parses JSON text and returns the root node.
 
static PIJSON newObject (const PIVariantMap &fields={})
 Creates an object node initialized from variant fields.
 
static PIJSON newArray (const PIVariantVector &fields={})
 Creates an array node initialized from variant elements.
 
static PIJSON newString (const PIString &v=PIString())
 Creates a string node.
 
template<typename T >
static PIJSON serialize (const T &v)
 Serializes value v with the overload set from pijsonserialization.h. More...
 
template<typename T >
static T deserialize (const PIJSON &json)
 Deserializes value of type T with the overload set from pijsonserialization.h. More...
 

Detailed Description

JSON tree node.

PIJSON class provides a tree structure for JSON data representation. Each element can be a value (with name), an array, or an object.

Synopsis

PIJSON tree

PIJSON creation

Mask/unmask

Examples

"["
" true,"
" 10,"
" {"
" \"num\":1.E-2,"
" \"str\":\"text\""
" },"
"]");
piCout << json;
piCout << json.toJSON();
JSON tree node.
Definition: pijson.h:40
PIString toJSON(PrintType print_type=Compact, bool mask_unicode=true) const
Returns JSON text representation of the node tree.
Definition: pijson.cpp:366
static PIJSON fromJSON(PIString str)
Parses JSON text and returns the root node.
Definition: pijson.cpp:373
#define piCout
Definition: picout.h:36
[
true,
10,
{
"num": 1.e-2,
"str": "text"
}
]
[true,10,{"num":1.e-2,"str":"text"}]

Разбор:

"["
" true,"
" 10,"
" {"
" \"num\":1.E-2,"
" \"str\":\"text\""
" },"
"]");
piCout << "top-level:";
for (const auto & i: json.array())
piCout << i.value();
piCout << "[2][\"str\"]:";
piCout << json[2]["str"].toString();
PIString toString() const
Returns scalar value as string.
Definition: pijson.h:104
const PIVector< PIJSON > & array() const
Returns array children, or an empty array when the node is not PIJSON::Array.
Definition: pijson.cpp:233
top-level:
PIVariant(Bool, 1)
[2]["str"]:
text
@ String
Definition: pijson.h:51
@ Invalid
Definition: pijson.h:47
Variant type.
Definition: pivariant.h:216

Создание:
Простой массив

PIJSON json;
json[0] = -1;
json[1] = "text";
json[3] = false;
piCout << json.toJSON();
[-1,"text","",false]

Массив объектов

struct {
PIString surname;
PIString email;
} persons[] = {
{"Ivan", "Ivanov", "ivan@pip.ru"},
{"Igor", "Igorevich", "igor@pip.ru"},
{"Andrey", "Andreevich", "andrey@pip.ru"}
};
PIJSON obj;
PIJSON json;
int index = 0;
for (const auto & p: persons) {
obj["index"] = index++;
obj["name"] = p.name;
obj["surname"] = p.surname;
obj["email"] = p.email;
json << obj;
}
piCout << json;
const PIString & name() const
Returns node name, or empty string for unnamed nodes.
Definition: pijson.h:75
String class.
Definition: pistring.h:42
[
{
"name": "Ivan",
"email": "ivan@pip.ru",
"index": 0,
"surname": "Ivanov"
},
{
"name": "Igor",
"email": "igor@pip.ru",
"index": 1,
"surname": "Igorevich"
},
{
"name": "Andrey",
"email": "andrey@pip.ru",
"index": 2,
"surname": "Andreevich"
}
]

Member Enumeration Documentation

◆ Type

Type of JSON node.

Enumerator
Invalid 

Invalid type

Null 

Without value, null

Boolean 

Boolean, true or false

Number 

Integer or floating-point number

String 

Text

Object 

Object, {}

Array 

Array, []

◆ PrintType

Formatting mode for JSON text output.

Enumerator
Compact 

Without spaces, minimum size

Tree 

With spaces and new-lines, human-readable

Member Function Documentation

◆ setValue()

void PIJSON::setValue ( const PIVariant v)

Sets scalar value and updates node type for bool, numeric and string variants.

If "v" type is boolean set type to PIJSON::Boolean.
If "v" type is any numeric set type to PIJSON::Number.
If "v" type is string set type to PIJSON::String.
In case of any other type set element type to PIJSON::Invalid.

◆ serialize()

template<typename T >
PIJSON PIJSON::serialize ( const T &  v)
static

Serializes value v with the overload set from pijsonserialization.h.

Convenience wrapper around piSerializeJSON().

◆ deserialize()

template<typename T >
T PIJSON::deserialize ( const PIJSON json)
static

Deserializes value of type T with the overload set from pijsonserialization.h.

Convenience wrapper around piDeserializeJSON().