PIJSON doc and << operator

This commit is contained in:
2022-09-30 21:09:57 +03:00
parent 0f48c206c3
commit 3c7e117661
3 changed files with 147 additions and 30 deletions

View File

@@ -86,6 +86,122 @@
//! Методы \a toJSON() и \a fromJSON() маскируют и размаскируют строковые поля.
//!
//!
//! \~english \section PIJSON_sec4 Examples
//! \~russian \section PIJSON_sec4 Примеры
//! \~english
//!
//! \~russian
//! Чтение:
//! \~\code
//! PIJSON json = PIJSON::fromJSON(
//! "["
//! " true,"
//! " 10,"
//! " {"
//! " \"num\":1.E-2,"
//! " \"str\":\"text\""
//! " },"
//! "]");
//! piCout << json;
//! piCout << json.toJSON();
//! \endcode
//! \~\code
//! [
//! true,
//! 10,
//! {
//! "num": 1.e-2,
//! "str": "text"
//! }
//! ]
//!
//! [true,10,{"num":1.e-2,"str":"text"}]
//! \endcode
//! Разбор:
//! \~\code
//! PIJSON json = PIJSON::fromJSON(
//! "["
//! " 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();
//! \endcode
//! \~\code
//! top-level:
//! PIVariant(Bool, 1)
//! PIVariant(String, 10)
//! PIVariant(Invalid)
//! [2]["str"]:
//! text
//! \endcode
//!
//! Создание:\n
//! Простой массив
//! \~\code
//! PIJSON json;
//! json[0] = -1;
//! json[1] = "text";
//! json[3] = false;
//! piCout << json.toJSON();
//! \endcode
//! \~\code
//! [-1,"text","",false]
//! \endcode
//!
//! Массив объектов
//! \~\code
//! struct {
//! PIString name;
//! 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;
//! \endcode
//! \~\code
//! [
//! {
//! "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"
//! }
//! ]
//! \endcode
//!
PIJSON PIJSON::newObject() {
@@ -113,7 +229,7 @@ const PIVector<PIJSON> & PIJSON::array() const {
if (!isArray()) {
return nullEntry().c_array;
} else {
return c_array;
return c_array;
}
}
@@ -202,6 +318,13 @@ PIJSON & PIJSON::operator[](int index) {
}
PIJSON & PIJSON::operator<<(const PIJSON & element) {
c_type = Array;
c_array << element;
return *this;
}
PIJSON & PIJSON::operator=(const PIJSON & v) {
c_type = v.c_type;
c_value = v.c_value;

View File

@@ -150,6 +150,10 @@ public:
//! \~russian Устанавливает тип элемента в \a PIJSON::Array, изменяет размер массива при неоходимости и возвращает элемент из массива по индексу "index".
PIJSON & operator[](int index);
//! \~english Set element type to \a PIJSON::Array and add element to the end of array.
//! \~russian Устанавливает тип элемента в \a PIJSON::Array и добавляет элемент в массив.
PIJSON & operator <<(const PIJSON & element);
//! \~english Returns element from map with key "key" if type is \a PIJSON::Object, otherwise returns invalid %PIJSON.
//! \~russian Возвращает элемент из словаря по ключу "key" если тип \a PIJSON::Object, иначе возвращает недействительный %PIJSON.

View File

@@ -30,35 +30,25 @@ const char J[] =
int main(int argc, char * argv[]) {
PIString s;
s = PIString::fromUTF8(J).trim();
s = PIString::fromUTF8("{\"st\\u0426r\":\"\\\\ \\\" \\u0425\\u0430\\n\"}");
//s = PIString::fromUTF8("{\"str\":\"Ха\"}");
PIJSON json = PIJSON::fromJSON(s);
//piCout << json;
//piCout << json.toJSON();
//json.resize(3);
//json["0"].setValue(123);
//json["1"].setValue("sec");
//json["2"]["f"].setValue(true);
//json["2"]["s"].setValue(-1);*/
//json[0]["Passport"]["id"] = 0xFF;
//piCout << json;
json = PIJSON();
json[0] = 123;
json[1] = PIString::fromUTF8("string русский хаха !");
json[2]["b"] = true;
json[2]["i"] = -1;
auto & arr(json[3]);
arr[0] = 10;
arr[1] = 11.2E-1;
arr[2] = "!!!";
json[5] = json[2];
json[7] = false;
//piCout << json;
struct {
PIString name;
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;
piCout << json.toJSON(PIJSON::Tree);
piCout << json.toJSON(PIJSON::Compact);
//piCout << json["str__"].toString().toUTF8();
return 0;
}