some code style #166

Merged
peri4 merged 1 commits from json into master 2022-09-30 20:27:04 +03:00
2 changed files with 30 additions and 29 deletions

View File

@@ -88,7 +88,6 @@
//! //!
PIJSON PIJSON::newObject() { PIJSON PIJSON::newObject() {
PIJSON ret; PIJSON ret;
ret.c_type = Object; ret.c_type = Object;
@@ -111,16 +110,20 @@ PIJSON PIJSON::newString(const PIString & v) {
const PIVector<PIJSON> & PIJSON::array() const { const PIVector<PIJSON> & PIJSON::array() const {
if (!isArray()) if (!isArray()) {
return nullEntry().c_array; return nullEntry().c_array;
} else {
return c_array; return c_array;
}
} }
const PIMap<PIString, PIJSON> & PIJSON::object() const { const PIMap<PIString, PIJSON> & PIJSON::object() const {
if (!isObject()) if (!isObject()) {
return nullEntry().c_object; return nullEntry().c_object;
} else {
return c_object; return c_object;
}
} }
@@ -165,10 +168,8 @@ void PIJSON::clear() {
int PIJSON::size() const { int PIJSON::size() const {
if (isArray()) if (isArray()) return c_array.size_s();
return c_array.size_s(); if (isObject()) return c_object.size_s();
if (isObject())
return c_object.size_s();
return 0; return 0;
} }
@@ -186,16 +187,16 @@ void PIJSON::resize(int new_size) {
const PIJSON & PIJSON::operator[](int index) const { const PIJSON & PIJSON::operator[](int index) const {
if (!isArray()) if (!isArray()) return nullEntry();
return nullEntry();
return c_array[index]; return c_array[index];
} }
PIJSON & PIJSON::operator[](int index) { PIJSON & PIJSON::operator[](int index) {
c_type = Array; c_type = Array;
if (index >= c_array.size_s()) if (index >= c_array.size_s()) {
c_array.resize(index + 1, newString()); c_array.resize(index + 1, newString());
}
PIJSON & ret(c_array[index]); PIJSON & ret(c_array[index]);
return ret; return ret;
} }
@@ -211,10 +212,8 @@ PIJSON & PIJSON::operator=(const PIJSON & v) {
PIJSON PIJSON::operator[](const PIString & key) const { PIJSON PIJSON::operator[](const PIString & key) const {
if (!isObject()) if (!isObject()) return nullEntry();
return nullEntry(); if (!c_object.contains(key)) return nullEntry();
if (!c_object.contains(key))
return nullEntry();
return c_object.value(key); return c_object.value(key);
} }
@@ -263,11 +262,11 @@ PIJSON PIJSON::parseValue(PIString & s) {
//piCout << "\n\n\n parseValue" << s; //piCout << "\n\n\n parseValue" << s;
s.trim(); s.trim();
if (s.isEmpty()) return ret; if (s.isEmpty()) return ret;
if (s[0] == '{') if (s[0] == '{') {
ret = parseObject(s.takeRange('{', '}')); ret = parseObject(s.takeRange('{', '}'));
else if (s[0] == '[') } else if (s[0] == '[') {
ret = parseArray(s.takeRange('[', ']')); ret = parseArray(s.takeRange('[', ']'));
else { } else {
s.trim(); s.trim();
if (s.startsWith('"')) { if (s.startsWith('"')) {
ret.c_type = PIJSON::String; ret.c_type = PIJSON::String;
@@ -420,9 +419,10 @@ void PIJSON::print(PIString & s, const PIJSON & v, PIString tab, bool spaces, bo
PIString ntab = tab + " "; PIString ntab = tab + " ";
auto it = v.c_object.makeIterator(); auto it = v.c_object.makeIterator();
int cnt = 0; int cnt = 0;
while (it.next()) while (it.next()) {
print(s, it.value(), ntab, spaces, transform, ++cnt < v.c_object.size_s()); print(s, it.value(), ntab, spaces, transform, ++cnt < v.c_object.size_s());
} }
}
if (spaces) s += tab; if (spaces) s += tab;
s += "}"; s += "}";
break; break;
@@ -431,9 +431,10 @@ void PIJSON::print(PIString & s, const PIJSON & v, PIString tab, bool spaces, bo
if (spaces) s += '\n'; if (spaces) s += '\n';
{ {
PIString ntab = tab + " "; PIString ntab = tab + " ";
for (int i = 0; i < v.c_array.size_s(); ++i) for (int i = 0; i < v.c_array.size_s(); ++i) {
print(s, v.c_array[i], ntab, spaces, transform, i < v.c_array.size_s() - 1); print(s, v.c_array[i], ntab, spaces, transform, i < v.c_array.size_s() - 1);
} }
}
if (spaces) s += tab; if (spaces) s += tab;
s += "]"; s += "]";
break; break;

View File

@@ -165,7 +165,7 @@ public:
//! \~english Returns text representation of JSON tree. //! \~english Returns text representation of JSON tree.
//! \~russian Возвращает текстовое представление дерева JSON. //! \~russian Возвращает текстовое представление дерева JSON.
PIString toJSON(PrintType print_type = Tree) const; PIString toJSON(PrintType print_type = Compact) const;
//! \~english Parse text representation of JSON "str" and returns it root element. //! \~english Parse text representation of JSON "str" and returns it root element.
//! \~russian Разбирает текстовое представление JSON "str" и возвращает его корневой элемент. //! \~russian Разбирает текстовое представление JSON "str" и возвращает его корневой элемент.
@@ -181,8 +181,8 @@ private:
static PIJSON parseValue(PIString & s); static PIJSON parseValue(PIString & s);
static PIJSON parseObject(PIString s); static PIJSON parseObject(PIString s);
static PIJSON parseArray(PIString s); static PIJSON parseArray(PIString s);
static PIString stringMask(const PIString & s);; static PIString stringMask(const PIString & s);
static PIString stringUnmask(const PIString & s);; static PIString stringUnmask(const PIString & s);
static void print(PIString & s, const PIJSON & v, PIString tab, bool spaces, bool transform = false, bool comma = false); static void print(PIString & s, const PIJSON & v, PIString tab, bool spaces, bool transform = false, bool comma = false);
PIString c_name; PIString c_name;