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

JSON class. More...

#include <pijson.h>

Public Types

enum  Type {
  Invalid , Null , Boolean , Number ,
  String , Object , Array
}
 Type of JSON tree element. More...
 
enum  PrintType { Compact , Tree }
 Generate JSON variant. More...
 

Public Member Functions

 PIJSON ()
 Contructs invalid PIJSON.
 
const PIStringname () const
 Returns name of element, or empty string if it doesn`t have name.
 
const PIVector< PIJSON > & array () const
 Returns elements array of this element, or empty array if element is not PIJSON::Array.
 
const PIMap< PIString, PIJSON > & object () const
 Returns elements map of this element, or empty map if element is not PIJSON::Object.
 
const PIVariantvalue () const
 Returns element value.
 
bool toBool () const
 Returns element value as bool.
 
int toInt () const
 Returns element value as integer number.
 
double toDouble () const
 Returns element value as floating-point number.
 
PIString toString () const
 Returns element value as string, valid for all types.
 
Type type () const
 Returns element type.
 
bool isValid () const
 Returns if element is valid.
 
bool isObject () const
 Returns if element is PIJSON::Object.
 
bool isArray () const
 Returns if element is PIJSON::Array.
 
void setValue (const PIVariant &v)
 Set value and type of element from "v". More...
 
void clear ()
 Clear element and set it to PIJSON::Invalid.
 
int size () const
 Returns size of elements array if type is PIJSON::Array, size of elements map if type is PIJSON::Object, otherwise returns 0.
 
bool contains (const PIString &key) const
 Returns if elements map contains key "key" if type is PIJSON::Object, otherwise returns false.
 
void resize (int new_size)
 Set element type to PIJSON::Array and resize elements array to "new_size".
 
PIJSONoperator= (const PIVariant &v)
 Synonim of setValue().
 
const PIJSONoperator[] (int index) const
 Returns element from array with index "index" if type is PIJSON::Array, otherwise returns invalid PIJSON.
 
PIJSONoperator[] (int index)
 Set element type to PIJSON::Array, resize if necessary and returns element from array with index "index".
 
PIJSONoperator<< (const PIJSON &element)
 Set element type to PIJSON::Array and add element to the end of array.
 
PIJSONoperator<< (const PIVariant &value)
 Set element type to PIJSON::Array and add element to the end of array.
 
PIJSON operator[] (const PIString &key) const
 Returns element from map with key "key" if type is PIJSON::Object, otherwise returns invalid PIJSON.
 
PIJSONoperator[] (const PIString &key)
 Set element type to PIJSON::Object and returns element from map with key "key". If element with this key doesn`t exists, it will be created.
 
PIString toJSON (PrintType print_type=Compact, bool mask_unicode=true) const
 Returns text representation of JSON tree.
 

Static Public Member Functions

static PIJSON fromJSON (PIString str)
 Parse text representation of JSON "str" and returns it root element.
 

Detailed Description

JSON class.

Synopsis

PIJSON tree

PIJSON creation

Mask/unmask

Examples

"["
" true,"
" 10,"
" {"
" \"num\":1.E-2,"
" \"str\":\"text\""
" },"
"]");
piCout << json;
piCout << json.toJSON();
JSON class.
Definition: pijson.h:36
PIString toJSON(PrintType print_type=Compact, bool mask_unicode=true) const
Returns text representation of JSON tree.
Definition: pijson.cpp:366
static PIJSON fromJSON(PIString str)
Parse text representation of JSON "str" and returns it root element.
Definition: pijson.cpp:373
#define piCout
Macro used for conditional (piDebug) output to PICout(StdOut)
Definition: picout.h:35
[
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 element value as string, valid for all types.
Definition: pijson.h:98
const PIVector< PIJSON > & array() const
Returns elements array of this element, or empty array if element is not PIJSON::Array.
Definition: pijson.cpp:233
top-level:
PIVariant(Bool, 1)
[2]["str"]:
text
@ String
Definition: pijson.h:49
@ Invalid
Definition: pijson.h:45
Variant type.
Definition: pivariant.h:219

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

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 name of element, or empty string if it doesn`t have name.
Definition: pijson.h:70
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 tree element.

Enumerator
Invalid 

Invalid type

Null 

Without value, null

Boolean 

Boolean, /b true or /b false

Number 

Integer or floating-point number

String 

Text

Object 

Object, {}

Array 

Array, []

◆ PrintType

Generate JSON variant.

Enumerator
Compact 

Without spaces, minimum size

Tree 

With spaces and new-lines, human-readable

Member Function Documentation

◆ setValue()

void PIJSON::setValue ( const PIVariant v)

Set value and type of element from "v".

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.