123 lines
3.4 KiB
C++
123 lines
3.4 KiB
C++
/*! \file pijson.h
|
|
* \ingroup Core
|
|
* \brief
|
|
* \~english JSON class
|
|
* \~russian Класс JSON
|
|
*/
|
|
/*
|
|
PIP - Platform Independent Primitives
|
|
JSON class
|
|
Ivan Pelipenko peri4ko@yandex.ru
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#ifndef PIJSON_H
|
|
#define PIJSON_H
|
|
|
|
#include "pivariant.h"
|
|
|
|
|
|
//! \ingroup Core
|
|
//! \~\brief
|
|
//! \~english JSON class.
|
|
//! \~russian Класс JSON.
|
|
class PIP_EXPORT PIJSON {
|
|
friend PICout operator <<(PICout s, const PIJSON & v);
|
|
public:
|
|
enum Type {
|
|
Invalid,
|
|
Null,
|
|
Boolean,
|
|
Number,
|
|
String,
|
|
Object,
|
|
Array
|
|
};
|
|
enum PrintType {
|
|
Compact,
|
|
Tree
|
|
};
|
|
PIJSON() {}
|
|
PIJSON(const PIJSON & o) = default;
|
|
|
|
static PIJSON newObject();
|
|
static PIJSON newArray();
|
|
|
|
const PIString & name() const {return c_name;}
|
|
const PIVector<PIJSON> & array() const {return c_array;}
|
|
const PIMap<PIString, PIJSON> & object() const {return c_object;}
|
|
const PIVariant & value() const {return c_value;}
|
|
bool toBool() const {return c_value.toBool();}
|
|
int toInt() const {return c_value.toInt();}
|
|
double toDouble() const {return c_value.toDouble();}
|
|
PIString toString() const {return c_value.toString();}
|
|
Type type() const {return c_type;}
|
|
bool isValid() const {return c_type != Invalid;}
|
|
bool isObject() const {return c_type == Object;}
|
|
bool isArray() const {return c_type == Array;}
|
|
void setValue(const PIVariant & v);
|
|
void clear();
|
|
int size() const;
|
|
bool contains(const PIString & key) const;
|
|
void resize(int new_size);
|
|
|
|
const PIJSON & operator[](int index) const;
|
|
PIJSON & operator[](int index);
|
|
PIJSON & operator=(const PIVariant & v) {setValue(v); return *this;}
|
|
PIJSON & operator=(const PIJSON & v);
|
|
|
|
PIJSON operator[](const PIString & key) const;
|
|
PIJSON & operator[](const PIString & key);
|
|
PIJSON operator[](const char * key) const {return (*this)[PIString::fromUTF8(key)];}
|
|
PIJSON & operator[](const char * key) {return (*this)[PIString::fromUTF8(key)];}
|
|
|
|
PIString toJSON() const;
|
|
static PIJSON fromJSON(PIString str);
|
|
|
|
private:
|
|
static PIJSON & nullEntry();
|
|
static PIString parseName(PIString & s);
|
|
static PIJSON parseValue(PIString & s);
|
|
static PIJSON parseObject(PIString s);
|
|
static PIJSON parseArray(PIString s);
|
|
static PIString stringMask(const PIString & s);;
|
|
static PIString stringUnmask(const PIString & s);;
|
|
static void print(PIString & s, const PIJSON & v, PIString tab, bool transform = false, bool comma = false);
|
|
|
|
PIString c_name;
|
|
PIVariant c_value;
|
|
PIVector<PIJSON> c_array;
|
|
PIMap<PIString, PIJSON> c_object;
|
|
Type c_type = Invalid;
|
|
};
|
|
|
|
|
|
|
|
//! \relatesalso PICout
|
|
//! \~english Output operator to \a PICout.
|
|
//! \~russian Оператор вывода в \a PICout.
|
|
inline PICout operator <<(PICout s, const PIJSON & v) {
|
|
s.space();
|
|
s.saveAndSetControls(0);
|
|
PIString str;
|
|
PIJSON::print(str, v, "");
|
|
s << str;
|
|
s.restoreControls();
|
|
return s;
|
|
}
|
|
|
|
|
|
#endif // PICONSTCHARS_H
|