PIJSON::toJSON with optionally unicode masking

PIVariantTypes::Color::toName()
This commit is contained in:
2022-12-08 13:21:11 +03:00
parent b609ce8027
commit c4cb81a104
6 changed files with 39 additions and 25 deletions

View File

@@ -349,9 +349,9 @@ PIJSON & PIJSON::operator[](const PIString & key) {
}
PIString PIJSON::toJSON(PrintType print_type) const {
PIString PIJSON::toJSON(PrintType print_type, bool mask_unicode) const {
PIString ret;
print(ret, *this, "", print_type == Tree, true);
print(ret, *this, "", print_type == Tree, true, false, mask_unicode);
return ret;
}
@@ -457,11 +457,14 @@ PIJSON PIJSON::parseArray(PIString s) {
}
PIString PIJSON::stringMask(const PIString & s) {
PIString PIJSON::stringMask(const PIString & s, bool mask_unicode) {
PIString ret;
for (auto c: s) {
if (!c.isAscii()) {
ret += "\\u" + PIString::fromNumber(c.unicode16Code(), 16).expandLeftTo(4, '0');
if (mask_unicode)
ret += "\\u" + PIString::fromNumber(c.unicode16Code(), 16).expandLeftTo(4, '0');
else
ret += c;
} else {
char ca = c.toAscii();
switch (ca) {
@@ -512,7 +515,7 @@ PIString PIJSON::stringUnmask(const PIString & s) {
}
void PIJSON::print(PIString & s, const PIJSON & v, PIString tab, bool spaces, bool transform, bool comma) {
void PIJSON::print(PIString & s, const PIJSON & v, PIString tab, bool spaces, bool transform, bool comma, bool mask_unicode) {
if (spaces) s += tab;
/*
switch (v.c_type) {
@@ -526,7 +529,7 @@ void PIJSON::print(PIString & s, const PIJSON & v, PIString tab, bool spaces, bo
}
*/
if (v.name().isNotEmpty()) {
s += '"' + (transform ? stringMask(v.name()) : v.name()) + "\":";
s += '"' + (transform ? stringMask(v.name(), mask_unicode) : v.name()) + "\":";
if (spaces) s += ' ';
}
switch (v.c_type) {
@@ -534,7 +537,7 @@ void PIJSON::print(PIString & s, const PIJSON & v, PIString tab, bool spaces, bo
case PIJSON::Null: s += "null"; break;
case PIJSON::Boolean: s += PIString::fromBool(v.c_value.toBool()); break;
case PIJSON::Number: s += v.c_value.toString(); break;
case PIJSON::String: s += '"' + (transform ? stringMask(v.c_value.toString()) : v.c_value.toString()) + '"'; break;
case PIJSON::String: s += '"' + (transform ? stringMask(v.c_value.toString(), mask_unicode) : v.c_value.toString()) + '"'; break;
case PIJSON::Object:
s += "{";
if (spaces) s += '\n';
@@ -543,7 +546,7 @@ void PIJSON::print(PIString & s, const PIJSON & v, PIString tab, bool spaces, bo
auto it = v.c_object.makeIterator();
int cnt = 0;
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(), mask_unicode);
}
}
if (spaces) s += tab;
@@ -555,7 +558,7 @@ void PIJSON::print(PIString & s, const PIJSON & v, PIString tab, bool spaces, bo
{
PIString ntab = tab + " ";
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, mask_unicode);
}
}
if (spaces) s += tab;