From 7d26a73922e6d017e96c75bf245ed7c9afdefccd Mon Sep 17 00:00:00 2001 From: "Robin E. R. Davies" Date: Fri, 20 Jun 2025 15:52:12 -0400 Subject: [PATCH] sync json/json_variant with ToobAmp --- PiPedalCommon/src/include/json_variant.hpp | 2 +- PiPedalCommon/src/json.cpp | 29 ++++++++++++++++++++-- PiPedalCommon/src/json_variant.cpp | 12 +++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/PiPedalCommon/src/include/json_variant.hpp b/PiPedalCommon/src/include/json_variant.hpp index 8ed6145..d6454f2 100644 --- a/PiPedalCommon/src/include/json_variant.hpp +++ b/PiPedalCommon/src/include/json_variant.hpp @@ -308,7 +308,7 @@ namespace pipedal iterator end() { return values.end(); } const_iterator begin() const { return values.begin(); } const_iterator end() const { return values.end(); } - + iterator erase(iterator it) { return values.erase(it); diff --git a/PiPedalCommon/src/json.cpp b/PiPedalCommon/src/json.cpp index 6b6dd73..707f543 100644 --- a/PiPedalCommon/src/json.cpp +++ b/PiPedalCommon/src/json.cpp @@ -22,6 +22,8 @@ #include #include "json_variant.hpp" #include "util.hpp" +#include + using namespace pipedal; @@ -123,7 +125,7 @@ void json_writer::write(string_view v,bool enforceValidUtf8Encoding) if ((uc >= UTF16_SURROGATE_1_BASE && uc <= UTF16_SURROGATE_1_BASE + UTF16_SURROGATE_MASK) || (uc >= UTF16_SURROGATE_2_BASE && uc <= UTF16_SURROGATE_2_BASE + UTF16_SURROGATE_MASK)) { // MUST not encode UTF16 surrogates in UTF8. - // throw_encoding_error(); + throw_encoding_error(); } if (uc == '"' || uc == '\\') @@ -268,6 +270,29 @@ void json_reader::skip_whitespace() } } +static void utf32_to_utf8_stream(std::ostream &s, uint32_t uc) +{ + if (uc < 0x80u) + { + s << (char)uc; + } else if (uc < 0x800u) { + s << (char)(0xC0 + (uc >> 6)); + s << (char)(0x80 + (uc & 0x3F)); + + } else if (uc < 0x10000u) { + s << (char)(0xE0 + (uc >> 12)); + s << (char)(0x80 + ((uc >> 6) & 0x3F)); + s << (char)(0x80 + (uc & 0x3F)); + } else if (uc < 0x0110000) { + s << (char)(0xF0 + (uc >> 18)); + s << (char)(0x80 + ((uc >> 12) & 0x3F)); + s << (char)(0x80 + ((uc >> 6) & 0x3F)); + s << (char)(0x80 + (uc & 0x3F)); + } else { + throw std::range_error("Illegal UTF-32 character."); + } +} + std::string json_reader::read_string() { // To completely normalize UTF-32 values we must covert to UTF-16, resolve surrogate pairs, and then convert UTF-32 to UTF-8. @@ -344,7 +369,7 @@ std::string json_reader::read_string() } uc = ((uc & UTF16_SURROGATE_MASK) << 10) + (uc2 & UTF16_SURROGATE_MASK) + 0x10000U; } - HtmlHelper::utf32_to_utf8_stream(s, uc); + utf32_to_utf8_stream(s, uc); } break; } diff --git a/PiPedalCommon/src/json_variant.cpp b/PiPedalCommon/src/json_variant.cpp index aa1b385..6469a78 100644 --- a/PiPedalCommon/src/json_variant.cpp +++ b/PiPedalCommon/src/json_variant.cpp @@ -50,6 +50,9 @@ void json_variant::free() case ContentType::Array: memArray().std::shared_ptr::~shared_ptr(); break; + default: + break; + } content_type = ContentType::Null; } @@ -467,6 +470,15 @@ json_variant &json_variant::operator=(json_variant &&value) case ContentType::Object: std::swap(this->memObject(), value.memObject()); return *this; + case ContentType::Null: + return *this; // nothing to do. + case ContentType::Bool: + std::swap(this->content.bool_value, value.content.bool_value); + return *this; + case ContentType::Number: + std::swap(this->content.double_value, value.content.double_value); + return *this; + } } free();