LCOV - code coverage report
Current view: top level - src/univalue/lib - univalue.cpp (source / functions) Hit Total Coverage
Test: test_dash_coverage.info Lines: 139 144 96.5 %
Date: 2026-06-25 07:23:51 Functions: 25 25 100.0 %

          Line data    Source code
       1             : // Copyright 2014 BitPay Inc.
       2             : // Copyright 2015 Bitcoin Core Developers
       3             : // Distributed under the MIT software license, see the accompanying
       4             : // file COPYING or https://opensource.org/licenses/mit-license.php.
       5             : 
       6             : #include <univalue.h>
       7             : 
       8             : #include <iomanip>
       9             : #include <map>
      10             : #include <memory>
      11             : #include <sstream>
      12             : #include <string>
      13             : #include <utility>
      14             : #include <vector>
      15             : 
      16         225 : const UniValue NullUniValue;
      17             : 
      18      277227 : void UniValue::clear()
      19             : {
      20      277227 :     typ = VNULL;
      21      277227 :     val.clear();
      22      277227 :     keys.clear();
      23      277227 :     values.clear();
      24      277227 : }
      25             : 
      26           1 : void UniValue::setNull()
      27             : {
      28           1 :     clear();
      29           1 : }
      30             : 
      31       98386 : void UniValue::setBool(bool val_)
      32             : {
      33       98386 :     clear();
      34       98386 :     typ = VBOOL;
      35       98386 :     if (val_)
      36        1045 :         val = "1";
      37       98386 : }
      38             : 
      39        3358 : static bool validNumStr(const std::string& s)
      40             : {
      41        3358 :     std::string tokenVal;
      42             :     unsigned int consumed;
      43        3358 :     enum jtokentype tt = getJsonToken(tokenVal, consumed, s.data(), s.data() + s.size());
      44        3358 :     return (tt == JTOK_NUMBER);
      45        3358 : }
      46             : 
      47        3358 : void UniValue::setNumStr(std::string str)
      48             : {
      49        3358 :     if (!validNumStr(str)) {
      50           1 :         throw std::runtime_error{"The string '" + str + "' is not a valid JSON number"};
      51             :     }
      52             : 
      53        3357 :     clear();
      54        3357 :     typ = VNUM;
      55        3357 :     val = std::move(str);
      56        3358 : }
      57             : 
      58         260 : void UniValue::setInt(uint64_t val_)
      59             : {
      60         260 :     std::ostringstream oss;
      61             : 
      62         260 :     oss << val_;
      63             : 
      64         260 :     return setNumStr(oss.str());
      65         260 : }
      66             : 
      67        2966 : void UniValue::setInt(int64_t val_)
      68             : {
      69        2966 :     std::ostringstream oss;
      70             : 
      71        2966 :     oss << val_;
      72             : 
      73        2966 :     return setNumStr(oss.str());
      74        2966 : }
      75             : 
      76          97 : void UniValue::setFloat(double val_)
      77             : {
      78          97 :     std::ostringstream oss;
      79             : 
      80          97 :     oss << std::setprecision(16) << val_;
      81             : 
      82          97 :     return setNumStr(oss.str());
      83          97 : }
      84             : 
      85      174948 : void UniValue::setStr(std::string str)
      86             : {
      87      174948 :     clear();
      88      174948 :     typ = VSTR;
      89      174948 :     val = std::move(str);
      90      174948 : }
      91             : 
      92         163 : void UniValue::setArray()
      93             : {
      94         163 :     clear();
      95         163 :     typ = VARR;
      96         163 : }
      97             : 
      98          85 : void UniValue::setObject()
      99             : {
     100          85 :     clear();
     101          85 :     typ = VOBJ;
     102          85 : }
     103             : 
     104         958 : void UniValue::push_back(UniValue val)
     105             : {
     106         958 :     checkType(VARR);
     107             : 
     108         958 :     values.push_back(std::move(val));
     109         958 : }
     110             : 
     111           2 : void UniValue::push_backV(const std::vector<UniValue>& vec)
     112             : {
     113           2 :     checkType(VARR);
     114             : 
     115           2 :     values.insert(values.end(), vec.begin(), vec.end());
     116           2 : }
     117             : 
     118        1561 : void UniValue::__pushKV(std::string key, UniValue val)
     119             : {
     120        1561 :     checkType(VOBJ);
     121             : 
     122        1561 :     keys.push_back(std::move(key));
     123        1561 :     values.push_back(std::move(val));
     124        1561 : }
     125             : 
     126        1342 : void UniValue::pushKV(std::string key, UniValue val)
     127             : {
     128        1342 :     checkType(VOBJ);
     129             : 
     130             :     size_t idx;
     131        1342 :     if (findKey(key, idx))
     132           1 :         values[idx] = std::move(val);
     133             :     else
     134        1341 :         __pushKV(std::move(key), std::move(val));
     135        1342 : }
     136             : 
     137           8 : void UniValue::pushKVs(UniValue obj)
     138             : {
     139           8 :     checkType(VOBJ);
     140           8 :     obj.checkType(VOBJ);
     141             : 
     142          28 :     for (size_t i = 0; i < obj.keys.size(); i++)
     143          20 :         __pushKV(std::move(obj.keys.at(i)), std::move(obj.values.at(i)));
     144           8 : }
     145             : 
     146           1 : void UniValue::getObjMap(std::map<std::string,UniValue>& kv) const
     147             : {
     148           1 :     if (typ != VOBJ)
     149           0 :         return;
     150             : 
     151           1 :     kv.clear();
     152           3 :     for (size_t i = 0; i < keys.size(); i++)
     153           2 :         kv[keys[i]] = values[i];
     154           1 : }
     155             : 
     156        1678 : bool UniValue::findKey(const std::string& key, size_t& retIdx) const
     157             : {
     158        5724 :     for (size_t i = 0; i < keys.size(); i++) {
     159        4355 :         if (keys[i] == key) {
     160         309 :             retIdx = i;
     161         309 :             return true;
     162             :         }
     163        4046 :     }
     164             : 
     165        1369 :     return false;
     166        1678 : }
     167             : 
     168           9 : bool UniValue::checkObject(const std::map<std::string,UniValue::VType>& t) const
     169             : {
     170           9 :     if (typ != VOBJ) {
     171           0 :         return false;
     172             :     }
     173             : 
     174          43 :     for (const auto& object: t) {
     175          36 :         size_t idx = 0;
     176          36 :         if (!findKey(object.first, idx)) {
     177           0 :             return false;
     178             :         }
     179             : 
     180          36 :         if (values.at(idx).getType() != object.second) {
     181           2 :             return false;
     182             :         }
     183             :     }
     184             : 
     185           7 :     return true;
     186           9 : }
     187             : 
     188         233 : const UniValue& UniValue::operator[](const std::string& key) const
     189             : {
     190         233 :     if (typ != VOBJ)
     191           0 :         return NullUniValue;
     192             : 
     193         233 :     size_t index = 0;
     194         233 :     if (!findKey(key, index))
     195           1 :         return NullUniValue;
     196             : 
     197         232 :     return values.at(index);
     198         233 : }
     199             : 
     200       20944 : const UniValue& UniValue::operator[](size_t index) const
     201             : {
     202       20944 :     if (typ != VOBJ && typ != VARR)
     203        6186 :         return NullUniValue;
     204       14758 :     if (index >= values.size())
     205          27 :         return NullUniValue;
     206             : 
     207       14731 :     return values.at(index);
     208       20944 : }
     209             : 
     210      104532 : void UniValue::checkType(const VType& expected) const
     211             : {
     212      104532 :     if (typ != expected) {
     213          54 :         throw type_error{"JSON value of type " + std::string{uvTypeName(typ)} + " is not of expected type " +
     214          27 :                                  std::string{uvTypeName(expected)}};
     215             :     }
     216      104532 : }
     217             : 
     218          56 : const char *uvTypeName(UniValue::VType t)
     219             : {
     220          56 :     switch (t) {
     221           5 :     case UniValue::VNULL: return "null";
     222           4 :     case UniValue::VBOOL: return "bool";
     223          10 :     case UniValue::VOBJ: return "object";
     224           8 :     case UniValue::VARR: return "array";
     225          15 :     case UniValue::VSTR: return "string";
     226          14 :     case UniValue::VNUM: return "number";
     227             :     }
     228             : 
     229             :     // not reached
     230           0 :     return nullptr;
     231          56 : }
     232             : 
     233         416 : const UniValue& UniValue::find_value(std::string_view key) const
     234             : {
     235        1043 :     for (unsigned int i = 0; i < keys.size(); ++i) {
     236         982 :         if (keys[i] == key) {
     237         355 :             return values.at(i);
     238             :         }
     239         627 :     }
     240          61 :     return NullUniValue;
     241         416 : }
     242             : 

Generated by: LCOV version 1.16