![]() |
PIP 5.6.1
Platform-Independent Primitives
|
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 PIString & | name () 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 PIVariant & | value () 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. | |
| PIJSON & | operator= (const PIVariant &v) |
| Synonym of setValue(). | |
| PIJSON & | operator= (const PIJSON &v) |
| Copy assignment operator. | |
| const PIJSON & | operator[] (int index) const |
| Returns array element by index, or invalid PIJSON when the node is not an array. | |
| PIJSON & | operator[] (int index) |
| Converts node to array, grows it when needed and returns element by index. | |
| PIJSON & | operator<< (const PIJSON &element) |
| Converts node to array and appends another JSON node. | |
| PIJSON & | operator<< (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. | |
| PIJSON & | operator[] (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. | |
| PIJSON & | operator[] (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... | |
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.
Разбор:
Создание:
Простой массив
Массив объектов
| enum PIJSON::Type |
| enum PIJSON::PrintType |
| 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.
|
static |
Serializes value v with the overload set from pijsonserialization.h.
Convenience wrapper around piSerializeJSON().
|
static |
Deserializes value of type T with the overload set from pijsonserialization.h.
Convenience wrapper around piDeserializeJSON().