LCOV - code coverage report
Current view: top level - muse/framework/global/serialization - json.cpp Coverage Total Hit
Test: filtered_coverage.info Lines: 53.6 % 476 255
Test Date: 2026-11-19 04:14:36

            Line data    Source code
       1              : /*
       2              :  * SPDX-License-Identifier: GPL-3.0-only
       3              :  * MuseScore-CLA-applies
       4              :  *
       5              :  * MuseScore Studio
       6              :  * Music Composition & Notation
       7              :  *
       8              :  * Copyright (C) 2021 MuseScore Limited and others
       9              :  *
      10              :  * This program is free software: you can redistribute it and/or modify
      11              :  * it under the terms of the GNU General Public License version 3 as
      12              :  * published by the Free Software Foundation.
      13              :  *
      14              :  * This program is distributed in the hope that it will be useful,
      15              :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      16              :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17              :  * GNU General Public License for more details.
      18              :  *
      19              :  * You should have received a copy of the GNU General Public License
      20              :  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
      21              :  */
      22              : #include "json.h"
      23              : 
      24              : #include <cmath>
      25              : #include <cassert>
      26              : 
      27              : #define PICOJSON_USE_LOCALE 1
      28              : #define PICOJSON_ASSERT assert
      29              : #include "picojson.h"
      30              : 
      31              : #include "log.h"
      32              : 
      33              : using namespace muse;
      34              : 
      35              : struct muse::JsonData
      36              : {
      37              :     picojson::value val;
      38              : };
      39              : 
      40       899681 : static inline const picojson::value& val_const(const std::shared_ptr<JsonData>& d)
      41              : {
      42       899681 :     return d->val;
      43              : }
      44              : 
      45         5225 : static inline picojson::value& val_mut(std::shared_ptr<JsonData>& d)
      46              : {
      47         5225 :     return d->val;
      48              : }
      49              : 
      50              : // =======================================
      51              : // JsonValue
      52              : // =======================================
      53          106 : JsonValue::JsonValue(bool v)
      54          106 :     : m_data(std::make_shared<JsonData>())
      55              : {
      56          106 :     val_mut(m_data).set<bool>(v);
      57          106 : }
      58              : 
      59         5102 : JsonValue::JsonValue(int v)
      60         5102 :     : m_data(std::make_shared<JsonData>())
      61              : {
      62         5102 :     double d = static_cast<double>(v);
      63         5102 :     val_mut(m_data).set<double>(d);
      64         5102 : }
      65              : 
      66            0 : JsonValue::JsonValue(double v)
      67            0 :     : m_data(std::make_shared<JsonData>())
      68              : {
      69            0 :     val_mut(m_data).set<double>(v);
      70            0 : }
      71              : 
      72            0 : JsonValue::JsonValue(const String& v)
      73            0 :     : m_data(std::make_shared<JsonData>())
      74              : {
      75            0 :     val_mut(m_data).set<std::string>(v.toStdString());
      76            0 : }
      77              : 
      78           17 : JsonValue::JsonValue(const std::string& v)
      79           17 :     : m_data(std::make_shared<JsonData>())
      80              : {
      81           17 :     val_mut(m_data).set<std::string>(v);
      82           17 : }
      83              : 
      84            0 : JsonValue::JsonValue(const char* v)
      85            0 :     : m_data(std::make_shared<JsonData>())
      86              : {
      87            0 :     val_mut(m_data).set<std::string>(std::string(v));
      88            0 : }
      89              : 
      90            0 : JsonValue::JsonValue(const JsonArray& v)
      91            0 :     : m_data(v.m_data)
      92              : {
      93            0 :     if (!m_data) {
      94            0 :         m_data = std::make_shared<JsonData>();
      95              :     }
      96            0 : }
      97              : 
      98            0 : JsonValue::JsonValue(const JsonObject& v)
      99            0 :     : m_data(v.m_data)
     100              : {
     101            0 :     if (!m_data) {
     102            0 :         m_data = std::make_shared<JsonData>();
     103              :     }
     104            0 : }
     105              : 
     106       707287 : JsonValue::JsonValue(std::shared_ptr<JsonData> d)
     107       707287 :     : m_data(d)
     108              : {
     109       707287 :     if (!m_data) {
     110       311209 :         m_data = std::make_shared<JsonData>();
     111              :     }
     112       707287 : }
     113              : 
     114            0 : void JsonValue::detach()
     115              : {
     116            0 :     if (!m_data) {
     117            0 :         return;
     118              :     }
     119              : 
     120            0 :     if (m_data.use_count() == 1) {
     121            0 :         return;
     122              :     }
     123              : 
     124            0 :     m_data = std::make_shared<JsonData>(*m_data);
     125              : }
     126              : 
     127         4948 : bool JsonValue::isNull() const
     128              : {
     129         4948 :     return val_const(m_data).is<picojson::null>();
     130              : }
     131              : 
     132            0 : void JsonValue::setNull()
     133              : {
     134            0 :     detach();
     135            0 :     val_mut(m_data) = picojson::value();
     136            0 : }
     137              : 
     138           25 : bool JsonValue::isBool() const
     139              : {
     140           25 :     return val_const(m_data).is<bool>();
     141              : }
     142              : 
     143          136 : bool JsonValue::toBool() const
     144              : {
     145          136 :     return val_const(m_data).evaluate_as_boolean();
     146              : }
     147              : 
     148            0 : JsonValue& JsonValue::operator=(bool v)
     149              : {
     150            0 :     detach();
     151            0 :     val_mut(m_data).set<bool>(v);
     152            0 :     return *this;
     153              : }
     154              : 
     155       141521 : bool JsonValue::isNumber() const
     156              : {
     157       141521 :     return val_const(m_data).is<double>();
     158              : }
     159              : 
     160          100 : int JsonValue::toInt() const
     161              : {
     162          100 :     if (!isNumber()) {
     163            8 :         return 0;
     164              :     }
     165           92 :     return static_cast<int>(std::round(val_const(m_data).get<double>()));
     166              : }
     167              : 
     168       141393 : double JsonValue::toDouble() const
     169              : {
     170       141393 :     if (!isNumber()) {
     171           36 :         return 0.0;
     172              :     }
     173       141357 :     return val_const(m_data).get<double>();
     174              : }
     175              : 
     176            0 : JsonValue& JsonValue::operator=(int v)
     177              : {
     178            0 :     detach();
     179            0 :     double d = static_cast<double>(v);
     180            0 :     val_mut(m_data).set<double>(d);
     181            0 :     return *this;
     182              : }
     183              : 
     184            0 : JsonValue& JsonValue::operator=(double v)
     185              : {
     186            0 :     detach();
     187            0 :     val_mut(m_data).set<double>(v);
     188            0 :     return *this;
     189              : }
     190              : 
     191       123262 : bool JsonValue::isString() const
     192              : {
     193       123262 :     return val_const(m_data).is<std::string>();
     194              : }
     195              : 
     196       114913 : String JsonValue::toString() const
     197              : {
     198       114913 :     if (!isString()) {
     199        53444 :         static String dummy;
     200        53444 :         return dummy;
     201              :     }
     202        61469 :     return String::fromStdString(val_const(m_data).get<std::string>());
     203              : }
     204              : 
     205         8281 : const std::string& JsonValue::toStdString() const
     206              : {
     207         8281 :     if (!isString()) {
     208            8 :         static std::string dummy;
     209            8 :         return dummy;
     210              :     }
     211         8273 :     return val_const(m_data).get<std::string>();
     212              : }
     213              : 
     214            0 : JsonValue& JsonValue::operator=(const String& str)
     215              : {
     216            0 :     detach();
     217            0 :     val_mut(m_data).set<std::string>(str.toStdString());
     218            0 :     return *this;
     219              : }
     220              : 
     221            0 : JsonValue& JsonValue::operator=(const std::string& str)
     222              : {
     223            0 :     detach();
     224            0 :     val_mut(m_data).set<std::string>(str);
     225            0 :     return *this;
     226              : }
     227              : 
     228            0 : JsonValue& JsonValue::operator=(const char* str)
     229              : {
     230            0 :     detach();
     231            0 :     val_mut(m_data).set<std::string>(std::string(str));
     232            0 :     return *this;
     233              : }
     234              : 
     235           34 : bool JsonValue::isArray() const
     236              : {
     237           34 :     return val_const(m_data).is<picojson::array>();
     238              : }
     239              : 
     240        73824 : JsonArray JsonValue::toArray() const
     241              : {
     242        73824 :     return JsonArray(m_data);
     243              : }
     244              : 
     245            0 : JsonValue& JsonValue::operator=(const JsonArray& arr)
     246              : {
     247            0 :     detach();
     248            0 :     val_mut(m_data) = val_const(arr.m_data);
     249            0 :     return *this;
     250              : }
     251              : 
     252          140 : bool JsonValue::isObject() const
     253              : {
     254          140 :     return val_const(m_data).is<picojson::object>();
     255              : }
     256              : 
     257       117926 : JsonObject JsonValue::toObject() const
     258              : {
     259       117926 :     return JsonObject(m_data);
     260              : }
     261              : 
     262            0 : JsonValue& JsonValue::operator=(const JsonObject& obj)
     263              : {
     264            0 :     detach();
     265            0 :     val_mut(m_data) = val_const(obj.m_data);
     266            0 :     return *this;
     267              : }
     268              : 
     269            0 : Val JsonValue::toVal() const
     270              : {
     271            0 :     if (isNull()) {
     272            0 :         return Val();
     273              :     }
     274            0 :     if (isBool()) {
     275            0 :         return Val(toBool());
     276              :     }
     277            0 :     if (isNumber()) {
     278            0 :         return Val(toDouble());
     279              :     }
     280            0 :     if (isString()) {
     281            0 :         return Val(toStdString());
     282              :     }
     283            0 :     if (isArray()) {
     284            0 :         muse::ValList list;
     285            0 :         const JsonArray array = toArray();
     286            0 :         for (size_t i = 0; i < array.size(); ++i) {
     287            0 :             list.emplace_back(array[i].toVal());
     288              :         }
     289            0 :         return Val(list);
     290            0 :     }
     291            0 :     if (isObject()) {
     292            0 :         muse::ValMap map;
     293            0 :         const JsonObject object = toObject();
     294            0 :         for (const auto& key : object.keys()) {
     295            0 :             map[key] = object[key].toVal();
     296            0 :         }
     297            0 :         return muse::Val(map);
     298            0 :     }
     299              : 
     300            0 :     return muse::Val();
     301              : }
     302              : 
     303            0 : JsonValue JsonValue::fromVal(const Val& val)
     304              : {
     305            0 :     switch (val.type()) {
     306            0 :     case Val::Type::Undefined: return JsonValue();
     307            0 :     case Val::Type::Bool: return JsonValue(val.toBool());
     308            0 :     case Val::Type::Int: return JsonValue(val.toInt());
     309            0 :     case Val::Type::Int64: return JsonValue(val.toString());  // as string
     310            0 :     case Val::Type::Double: return JsonValue(val.toDouble());
     311            0 :     case Val::Type::String: return JsonValue(val.toString());
     312            0 :     case Val::Type::List: {
     313            0 :         JsonArray array;
     314            0 :         for (const auto& item : val.toList()) {
     315            0 :             array.append(fromVal(item));
     316            0 :         }
     317            0 :         return array;
     318            0 :     }
     319            0 :     case Val::Type::Map: {
     320            0 :         JsonObject object;
     321            0 :         for (const auto& [key, value] : val.toMap()) {
     322            0 :             object[key] = fromVal(value);
     323            0 :         }
     324            0 :         return object;
     325            0 :     }
     326            0 :     default: {
     327            0 :         NOT_SUPPORTED << "data type: " << (int)val.type();
     328              :     }
     329              :     }
     330              : 
     331            0 :     return JsonValue();
     332              : }
     333              : 
     334              : // =======================================
     335              : // JsonValueRef
     336              : // =======================================
     337              : 
     338        13330 : JsonValueRef::JsonValueRef(const std::string& key, JsonObject* o)
     339        13330 :     : m_key(key), m_object(o)
     340              : {
     341        13330 : }
     342              : 
     343            0 : JsonValueRef::JsonValueRef(size_t i, JsonArray* a)
     344            0 :     : m_idx(i), m_array(a)
     345              : {
     346            0 : }
     347              : 
     348         2505 : JsonValueRef& JsonValueRef::operator=(bool v)
     349              : {
     350         2505 :     if (m_object) {
     351         2505 :         m_object->set(m_key, v);
     352            0 :     } else if (m_array) {
     353            0 :         m_array->set(m_idx, v);
     354              :     }
     355         2505 :     return *this;
     356              : }
     357              : 
     358         4628 : JsonValueRef& JsonValueRef::operator=(int v)
     359              : {
     360         4628 :     if (m_object) {
     361         4628 :         m_object->set(m_key, v);
     362            0 :     } else if (m_array) {
     363            0 :         m_array->set(m_idx, v);
     364              :     }
     365         4628 :     return *this;
     366              : }
     367              : 
     368         1231 : JsonValueRef& JsonValueRef::operator=(double v)
     369              : {
     370         1231 :     if (m_object) {
     371         1231 :         m_object->set(m_key, v);
     372            0 :     } else if (m_array) {
     373            0 :         m_array->set(m_idx, v);
     374              :     }
     375         1231 :     return *this;
     376              : }
     377              : 
     378          637 : JsonValueRef& JsonValueRef::operator=(const String& v)
     379              : {
     380          637 :     if (m_object) {
     381          637 :         m_object->set(m_key, v);
     382            0 :     } else if (m_array) {
     383            0 :         m_array->set(m_idx, v);
     384              :     }
     385          637 :     return *this;
     386              : }
     387              : 
     388          952 : JsonValueRef& JsonValueRef::operator=(const std::string& v)
     389              : {
     390          952 :     if (m_object) {
     391          952 :         m_object->set(m_key, v);
     392            0 :     } else if (m_array) {
     393            0 :         m_array->set(m_idx, v);
     394              :     }
     395          952 :     return *this;
     396              : }
     397              : 
     398            8 : JsonValueRef& JsonValueRef::operator=(const char* v)
     399              : {
     400            8 :     if (m_object) {
     401            8 :         m_object->set(m_key, v);
     402            0 :     } else if (m_array) {
     403            0 :         m_array->set(m_idx, v);
     404              :     }
     405            8 :     return *this;
     406              : }
     407              : 
     408            1 : JsonValueRef& JsonValueRef::operator=(const JsonValue& v)
     409              : {
     410            1 :     if (m_object) {
     411            1 :         m_object->set(m_key, v);
     412            0 :     } else if (m_array) {
     413            0 :         m_array->set(m_idx, v);
     414              :     }
     415            1 :     return *this;
     416              : }
     417              : 
     418         1715 : JsonValueRef& JsonValueRef::operator=(const JsonArray& v)
     419              : {
     420         1715 :     if (m_object) {
     421         1715 :         m_object->set(m_key, v);
     422            0 :     } else if (m_array) {
     423            0 :         m_array->set(m_idx, v);
     424              :     }
     425         1715 :     return *this;
     426              : }
     427              : 
     428         1653 : JsonValueRef& JsonValueRef::operator=(const JsonObject& v)
     429              : {
     430         1653 :     if (m_object) {
     431         1653 :         m_object->set(m_key, v);
     432            0 :     } else if (m_array) {
     433            0 :         m_array->set(m_idx, v);
     434              :     }
     435         1653 :     return *this;
     436              : }
     437              : 
     438              : // =======================================
     439              : // JsonArray
     440              : // =======================================
     441              : 
     442       154286 : static inline const picojson::array& array_const(const std::shared_ptr<JsonData>& d)
     443              : {
     444       154286 :     return d->val.get<picojson::array>();
     445              : }
     446              : 
     447         7744 : static inline picojson::array& array_mut(std::shared_ptr<JsonData>& d)
     448              : {
     449         7744 :     return d->val.get<picojson::array>();
     450              : }
     451              : 
     452        75096 : JsonArray::JsonArray(std::shared_ptr<JsonData> d)
     453        75096 :     : m_data(d)
     454              : {
     455        75096 :     if (!m_data) {
     456         1195 :         m_data = std::make_shared<JsonData>();
     457         1195 :         m_data->val = picojson::value(picojson::array());
     458              :     }
     459        75096 : }
     460              : 
     461         1120 : JsonArray::JsonArray(std::initializer_list<JsonValue> args)
     462              : {
     463         1120 :     m_data = std::make_shared<JsonData>();
     464         1120 :     m_data->val = picojson::value(picojson::array());
     465              : 
     466         1120 :     array_mut(m_data).reserve(args.size());
     467         6221 :     for (const JsonValue& v : args) {
     468         5101 :         array_mut(m_data).push_back(v.m_data->val);
     469              :     }
     470         1120 : }
     471              : 
     472         1523 : void JsonArray::detach()
     473              : {
     474         1523 :     if (!m_data) {
     475            0 :         return;
     476              :     }
     477              : 
     478         1523 :     if (m_data.use_count() == 1) {
     479         1523 :         return;
     480              :     }
     481              : 
     482            0 :     m_data = std::make_shared<JsonData>(*m_data);
     483              : }
     484              : 
     485         8695 : size_t JsonArray::size() const
     486              : {
     487         8695 :     return array_const(m_data).size();
     488              : }
     489              : 
     490            0 : void JsonArray::resize(size_t i)
     491              : {
     492            0 :     detach();
     493            0 :     array_mut(m_data).resize(i);
     494            0 : }
     495              : 
     496           18 : bool JsonArray::empty() const
     497              : {
     498           18 :     return array_const(m_data).size() == 0;
     499              : }
     500              : 
     501       145573 : JsonValue JsonArray::at(size_t i) const
     502              : {
     503       145573 :     std::shared_ptr<JsonData> d = std::make_shared<JsonData>();
     504       145573 :     d->val = array_const(m_data).at(i);
     505       291146 :     return JsonValue(d);
     506       145573 : }
     507              : 
     508            0 : JsonArray& JsonArray::set(size_t i, bool v)
     509              : {
     510            0 :     detach();
     511            0 :     array_mut(m_data)[i] = picojson::value(v);
     512            0 :     return *this;
     513              : }
     514              : 
     515            0 : JsonArray& JsonArray::set(size_t i, int v)
     516              : {
     517            0 :     detach();
     518            0 :     array_mut(m_data)[i] = picojson::value(static_cast<double>(v));
     519            0 :     return *this;
     520              : }
     521              : 
     522            0 : JsonArray& JsonArray::set(size_t i, double v)
     523              : {
     524            0 :     detach();
     525            0 :     array_mut(m_data)[i] = picojson::value(v);
     526            0 :     return *this;
     527              : }
     528              : 
     529            0 : JsonArray& JsonArray::set(size_t i, const String& str)
     530              : {
     531            0 :     detach();
     532            0 :     array_mut(m_data)[i] = picojson::value(str.toStdString());
     533            0 :     return *this;
     534              : }
     535              : 
     536            0 : JsonArray& JsonArray::set(size_t i, const std::string& str)
     537              : {
     538            0 :     detach();
     539            0 :     array_mut(m_data)[i] = picojson::value(str);
     540            0 :     return *this;
     541              : }
     542              : 
     543            0 : JsonArray& JsonArray::set(size_t i, const char* str)
     544              : {
     545            0 :     detach();
     546            0 :     array_mut(m_data)[i] = picojson::value(std::string(str));
     547            0 :     return *this;
     548              : }
     549              : 
     550            0 : JsonArray& JsonArray::set(size_t i, const JsonValue& v)
     551              : {
     552            0 :     detach();
     553            0 :     array_mut(m_data)[i] = v.m_data->val;
     554            0 :     return *this;
     555              : }
     556              : 
     557            0 : JsonArray& JsonArray::set(size_t i, const JsonArray& v)
     558              : {
     559            0 :     detach();
     560            0 :     array_mut(m_data)[i] = v.m_data->val;
     561            0 :     return *this;
     562              : }
     563              : 
     564            0 : JsonArray& JsonArray::set(size_t i, const JsonObject& v)
     565              : {
     566            0 :     detach();
     567            0 :     array_mut(m_data)[i] = v.m_data->val;
     568            0 :     return *this;
     569              : }
     570              : 
     571            0 : JsonArray& JsonArray::append(bool v)
     572              : {
     573            0 :     detach();
     574            0 :     array_mut(m_data).push_back(picojson::value(v));
     575            0 :     return *this;
     576              : }
     577              : 
     578            4 : JsonArray& JsonArray::append(int v)
     579              : {
     580            4 :     detach();
     581            4 :     array_mut(m_data).push_back(picojson::value(static_cast<double>(v)));
     582            4 :     return *this;
     583              : }
     584              : 
     585            0 : JsonArray& JsonArray::append(double v)
     586              : {
     587            0 :     detach();
     588            0 :     array_mut(m_data).push_back(picojson::value(v));
     589            0 :     return *this;
     590              : }
     591              : 
     592            0 : JsonArray& JsonArray::append(const String& str)
     593              : {
     594            0 :     detach();
     595            0 :     array_mut(m_data).push_back(picojson::value(str.toStdString()));
     596            0 :     return *this;
     597              : }
     598              : 
     599            0 : JsonArray& JsonArray::append(const std::string& str)
     600              : {
     601            0 :     detach();
     602            0 :     array_mut(m_data).push_back(picojson::value(str));
     603            0 :     return *this;
     604              : }
     605              : 
     606            0 : JsonArray& JsonArray::append(const char* str)
     607              : {
     608            0 :     detach();
     609            0 :     array_mut(m_data).push_back(picojson::value(std::string(str)));
     610            0 :     return *this;
     611              : }
     612              : 
     613           16 : JsonArray& JsonArray::append(const JsonValue& v)
     614              : {
     615           16 :     detach();
     616           16 :     array_mut(m_data).push_back(val_const(v.m_data));
     617           16 :     return *this;
     618              : }
     619              : 
     620          476 : JsonArray& JsonArray::append(const JsonArray& v)
     621              : {
     622          476 :     detach();
     623          476 :     array_mut(m_data).push_back(val_const(v.m_data));
     624          476 :     return *this;
     625              : }
     626              : 
     627         1027 : JsonArray& JsonArray::append(const JsonObject& v)
     628              : {
     629         1027 :     detach();
     630         1027 :     array_mut(m_data).push_back(val_const(v.m_data));
     631         1027 :     return *this;
     632              : }
     633              : 
     634            0 : JsonArray JsonArray::fromValList(const ValList& valList)
     635              : {
     636            0 :     JsonArray array;
     637            0 :     for (const auto& item : valList) {
     638            0 :         array.append(JsonValue::fromVal(item));
     639              :     }
     640            0 :     return array;
     641            0 : }
     642              : 
     643            0 : JsonArray JsonArray::fromStringList(const std::vector<std::string>& stringList)
     644              : {
     645            0 :     JsonArray array;
     646            0 :     for (const auto& item : stringList) {
     647            0 :         array.append(JsonValue(item));
     648              :     }
     649            0 :     return array;
     650            0 : }
     651              : 
     652            0 : JsonValue JsonArray::operator [](size_t i) const
     653              : {
     654            0 :     return at(i);
     655              : }
     656              : 
     657            0 : JsonValueRef JsonArray::operator [](size_t i)
     658              : {
     659            0 :     return JsonValueRef(i, this);
     660              : }
     661              : 
     662              : // =======================================
     663              : // JsonObject
     664              : // =======================================
     665       361611 : static const picojson::object& object_const(const std::shared_ptr<JsonData>& d)
     666              : {
     667       361611 :     return d->val.get<picojson::object>();
     668              : }
     669              : 
     670        13686 : static picojson::object& object_mut(std::shared_ptr<JsonData>& d)
     671              : {
     672        13686 :     return d->val.get<picojson::object>();
     673              : }
     674              : 
     675       120898 : JsonObject::JsonObject(std::shared_ptr<JsonData> d)
     676       120898 :     : m_data(d)
     677              : {
     678       120898 :     if (!m_data) {
     679         2792 :         m_data = std::make_shared<JsonData>();
     680         2792 :         m_data->val = picojson::value(picojson::object());
     681              :     }
     682       120898 : }
     683              : 
     684        13686 : void JsonObject::detach()
     685              : {
     686        13686 :     if (!m_data) {
     687            0 :         return;
     688              :     }
     689              : 
     690        13686 :     if (m_data.use_count() == 1) {
     691        13686 :         return;
     692              :     }
     693              : 
     694            0 :     m_data = std::make_shared<JsonData>(*m_data);
     695              : }
     696              : 
     697       416905 : bool JsonObject::isValid() const
     698              : {
     699       416905 :     return val_const(m_data).is<picojson::object>();
     700              : }
     701              : 
     702           57 : bool JsonObject::empty() const
     703              : {
     704           57 :     if (!isValid()) {
     705            0 :         return true;
     706              :     }
     707              : 
     708           57 :     return object_const(m_data).size() == 0;
     709              : }
     710              : 
     711            1 : size_t JsonObject::size() const
     712              : {
     713            1 :     if (!isValid()) {
     714            0 :         return 0;
     715              :     }
     716              : 
     717            1 :     return object_const(m_data).size();
     718              : }
     719              : 
     720        10095 : bool JsonObject::contains(const std::string& key) const
     721              : {
     722        10095 :     if (!isValid()) {
     723            0 :         return false;
     724              :     }
     725              : 
     726        10095 :     const picojson::object& o = object_const(m_data);
     727        10095 :     return o.find(key) != o.cend();
     728              : }
     729              : 
     730       306384 : JsonValue JsonObject::value(const std::string& key, JsonValue def) const
     731              : {
     732       306384 :     if (!isValid()) {
     733            0 :         return def;
     734              :     }
     735              : 
     736       306384 :     const picojson::object& o = object_const(m_data);
     737       306384 :     auto it = o.find(key);
     738       306384 :     if (it != o.cend()) {
     739       250505 :         std::shared_ptr<JsonData> d = std::make_shared<JsonData>();
     740       250505 :         d->val = it->second;
     741       250505 :         return JsonValue(d);
     742       250505 :     }
     743        55879 :     return def;
     744              : }
     745              : 
     746         2513 : JsonObject& JsonObject::set(const std::string& key, bool v)
     747              : {
     748         2513 :     detach();
     749         2513 :     object_mut(m_data).insert_or_assign(key, picojson::value(v));
     750         2513 :     return *this;
     751              : }
     752              : 
     753         4652 : JsonObject& JsonObject::set(const std::string& key, int v)
     754              : {
     755         4652 :     detach();
     756         4652 :     object_mut(m_data).insert_or_assign(key, picojson::value(static_cast<double>(v)));
     757         4652 :     return *this;
     758              : }
     759              : 
     760         1231 : JsonObject& JsonObject::set(const std::string& key, double v)
     761              : {
     762         1231 :     detach();
     763         1231 :     object_mut(m_data).insert_or_assign(key, picojson::value(v));
     764         1231 :     return *this;
     765              : }
     766              : 
     767         1183 : JsonObject& JsonObject::set(const std::string& key, const std::string& v)
     768              : {
     769         1183 :     detach();
     770         1183 :     object_mut(m_data).insert_or_assign(key, picojson::value(v));
     771         1183 :     return *this;
     772              : }
     773              : 
     774            8 : JsonObject& JsonObject::set(const std::string& key, const char* str)
     775              : {
     776            8 :     detach();
     777           16 :     object_mut(m_data).insert_or_assign(key, picojson::value(std::string(str)));
     778            8 :     return *this;
     779              : }
     780              : 
     781          637 : JsonObject& JsonObject::set(const std::string& key, const String& str)
     782              : {
     783          637 :     detach();
     784          637 :     object_mut(m_data).insert_or_assign(key, picojson::value(str.toStdString()));
     785          637 :     return *this;
     786              : }
     787              : 
     788           14 : JsonObject& JsonObject::set(const std::string& key, const JsonValue& v)
     789              : {
     790           14 :     detach();
     791           14 :     object_mut(m_data).insert_or_assign(key, v.m_data->val);
     792           14 :     return *this;
     793              : }
     794              : 
     795         1730 : JsonObject& JsonObject::set(const std::string& key, const JsonArray& v)
     796              : {
     797         1730 :     detach();
     798         1730 :     object_mut(m_data).insert_or_assign(key, v.m_data->val);
     799         1730 :     return *this;
     800              : }
     801              : 
     802         1718 : JsonObject& JsonObject::set(const std::string& key, const JsonObject& v)
     803              : {
     804         1718 :     detach();
     805         1718 :     object_mut(m_data).insert_or_assign(key, v.m_data->val);
     806         1718 :     return *this;
     807              : }
     808              : 
     809           42 : JsonValue JsonObject::operator [](const std::string& key) const
     810              : {
     811           42 :     return value(key);
     812              : }
     813              : 
     814        13330 : JsonValueRef JsonObject::operator [](const std::string& key)
     815              : {
     816        13330 :     return JsonValueRef(key, this);
     817              : }
     818              : 
     819        45074 : std::vector<std::string> JsonObject::keys() const
     820              : {
     821        45074 :     std::vector<std::string> result;
     822        45074 :     const picojson::object& o = object_const(m_data);
     823       178370 :     for (auto&& p : o) {
     824       133296 :         result.push_back(p.first);
     825              :     }
     826        45074 :     return result;
     827            0 : }
     828              : 
     829              : // =======================================
     830              : // JsonDocument
     831              : // =======================================
     832          257 : JsonDocument::JsonDocument(std::shared_ptr<JsonData> d)
     833          257 :     : m_data(d)
     834              : {
     835          257 : }
     836              : 
     837           28 : JsonDocument::JsonDocument(JsonObject o)
     838           28 :     : m_data(o.m_data)
     839              : {
     840           28 : }
     841              : 
     842           86 : JsonDocument::JsonDocument(JsonArray a)
     843           86 :     : m_data(a.m_data)
     844              : {
     845           86 : }
     846              : 
     847          114 : ByteArray JsonDocument::toJson(Format format) const
     848              : {
     849          114 :     std::string json = m_data->val.serialize(format == Format::Indented);
     850          228 :     return ByteArray(json.c_str(), json.size());
     851          114 : }
     852              : 
     853          257 : JsonDocument JsonDocument::fromJson(const ByteArray& ba, std::string* err)
     854              : {
     855          257 :     std::shared_ptr<JsonData> d = std::make_shared<JsonData>();
     856              : 
     857          257 :     std::string_view json(ba.constChar(), ba.size());
     858              : 
     859          257 :     const char* currentLoc = setlocale(LC_NUMERIC, "C");
     860          257 :     picojson::parse(d->val, json.begin(), json.end(), err);
     861          257 :     setlocale(LC_NUMERIC, currentLoc);
     862              : 
     863          257 :     JsonDocument doc(d);
     864          257 :     return doc;
     865          257 : }
     866              : 
     867           10 : bool JsonDocument::isObject() const
     868              : {
     869           10 :     return m_data->val.is<picojson::object>();
     870              : }
     871              : 
     872           76 : bool JsonDocument::isArray() const
     873              : {
     874           76 :     return m_data->val.is<picojson::array>();
     875              : }
     876              : 
     877          180 : JsonObject JsonDocument::rootObject() const
     878              : {
     879          180 :     return JsonObject(m_data);
     880              : }
     881              : 
     882           77 : JsonArray JsonDocument::rootArray() const
     883              : {
     884           77 :     return JsonArray(m_data);
     885              : }
        

Generated by: LCOV version 2.0-1