LCOV - code coverage report
Current view: top level - src/univalue/lib - univalue.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 98.4 % 129 127
Test Date: 2026-02-04 04:43:42 Functions: 100.0 % 25 25
Branches: 65.7 % 105 69

             Branch data     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 <sstream>
      11                 :             : #include <string>
      12                 :             : #include <utility>
      13                 :             : #include <vector>
      14                 :             : 
      15                 :             : const UniValue NullUniValue;
      16                 :             : 
      17                 :      443417 : void UniValue::clear()
      18                 :             : {
      19                 :      443417 :     typ = VNULL;
      20                 :      443417 :     val.clear();
      21                 :      443417 :     keys.clear();
      22                 :      443417 :     values.clear();
      23                 :      443417 : }
      24                 :             : 
      25                 :           1 : void UniValue::setNull()
      26                 :             : {
      27                 :           1 :     clear();
      28                 :           1 : }
      29                 :             : 
      30                 :      168455 : void UniValue::setBool(bool val_)
      31                 :             : {
      32                 :      168455 :     clear();
      33                 :      168455 :     typ = VBOOL;
      34         [ +  + ]:      168455 :     if (val_)
      35                 :        1531 :         val = "1";
      36                 :      168455 : }
      37                 :             : 
      38                 :        2496 : static bool validNumStr(const std::string& s)
      39                 :             : {
      40         [ -  + ]:        2496 :     std::string tokenVal;
      41                 :        2496 :     unsigned int consumed;
      42   [ -  +  +  - ]:        2496 :     enum jtokentype tt = getJsonToken(tokenVal, consumed, s.data(), s.data() + s.size());
      43                 :        2496 :     return (tt == JTOK_NUMBER);
      44                 :        2496 : }
      45                 :             : 
      46                 :        2496 : void UniValue::setNumStr(std::string str)
      47                 :             : {
      48         [ +  + ]:        2496 :     if (!validNumStr(str)) {
      49   [ +  -  +  - ]:           3 :         throw std::runtime_error{"The string '" + str + "' is not a valid JSON number"};
      50                 :             :     }
      51                 :             : 
      52                 :        2495 :     clear();
      53                 :        2495 :     typ = VNUM;
      54                 :        2495 :     val = std::move(str);
      55                 :        2495 : }
      56                 :             : 
      57                 :         448 : void UniValue::setInt(uint64_t val_)
      58                 :             : {
      59                 :         448 :     std::ostringstream oss;
      60                 :             : 
      61         [ +  - ]:         448 :     oss << val_;
      62                 :             : 
      63   [ +  -  +  - ]:         896 :     return setNumStr(oss.str());
      64                 :         448 : }
      65                 :             : 
      66                 :        1919 : void UniValue::setInt(int64_t val_)
      67                 :             : {
      68                 :        1919 :     std::ostringstream oss;
      69                 :             : 
      70         [ +  - ]:        1919 :     oss << val_;
      71                 :             : 
      72   [ +  -  +  - ]:        3838 :     return setNumStr(oss.str());
      73                 :        1919 : }
      74                 :             : 
      75                 :          93 : void UniValue::setFloat(double val_)
      76                 :             : {
      77                 :          93 :     std::ostringstream oss;
      78                 :             : 
      79         [ +  - ]:          93 :     oss << std::setprecision(16) << val_;
      80                 :             : 
      81   [ +  -  +  - ]:         186 :     return setNumStr(oss.str());
      82                 :          93 : }
      83                 :             : 
      84                 :      272158 : void UniValue::setStr(std::string str)
      85                 :             : {
      86                 :      272158 :     clear();
      87                 :      272158 :     typ = VSTR;
      88                 :      272158 :     val = std::move(str);
      89                 :      272158 : }
      90                 :             : 
      91                 :          73 : void UniValue::setArray()
      92                 :             : {
      93                 :          73 :     clear();
      94                 :          73 :     typ = VARR;
      95                 :          73 : }
      96                 :             : 
      97                 :          61 : void UniValue::setObject()
      98                 :             : {
      99                 :          61 :     clear();
     100                 :          61 :     typ = VOBJ;
     101                 :          61 : }
     102                 :             : 
     103                 :        1073 : void UniValue::push_back(UniValue val)
     104                 :             : {
     105                 :        1073 :     checkType(VARR);
     106                 :             : 
     107                 :        1072 :     values.push_back(std::move(val));
     108                 :        1072 : }
     109                 :             : 
     110                 :           2 : void UniValue::push_backV(const std::vector<UniValue>& vec)
     111                 :             : {
     112                 :           2 :     checkType(VARR);
     113                 :             : 
     114                 :           1 :     values.insert(values.end(), vec.begin(), vec.end());
     115                 :           1 : }
     116                 :             : 
     117                 :        1054 : void UniValue::pushKVEnd(std::string key, UniValue val)
     118                 :             : {
     119                 :        1054 :     checkType(VOBJ);
     120                 :             : 
     121                 :        1053 :     keys.push_back(std::move(key));
     122                 :        1053 :     values.push_back(std::move(val));
     123                 :        1053 : }
     124                 :             : 
     125                 :         842 : void UniValue::pushKV(std::string key, UniValue val)
     126                 :             : {
     127                 :         842 :     checkType(VOBJ);
     128                 :             : 
     129                 :         841 :     size_t idx;
     130         [ +  + ]:         841 :     if (findKey(key, idx))
     131                 :           1 :         values[idx] = std::move(val);
     132                 :             :     else
     133         [ +  - ]:        1680 :         pushKVEnd(std::move(key), std::move(val));
     134                 :         841 : }
     135                 :             : 
     136                 :           2 : void UniValue::pushKVs(UniValue obj)
     137                 :             : {
     138                 :           2 :     checkType(VOBJ);
     139                 :           1 :     obj.checkType(VOBJ);
     140                 :             : 
     141   [ -  +  +  + ]:           3 :     for (size_t i = 0; i < obj.keys.size(); i++)
     142   [ +  -  +  - ]:           4 :         pushKVEnd(std::move(obj.keys.at(i)), std::move(obj.values.at(i)));
     143                 :           1 : }
     144                 :             : 
     145                 :           1 : void UniValue::getObjMap(std::map<std::string,UniValue>& kv) const
     146                 :             : {
     147         [ +  - ]:           1 :     if (typ != VOBJ)
     148                 :             :         return;
     149                 :             : 
     150                 :           1 :     kv.clear();
     151   [ -  +  +  + ]:           3 :     for (size_t i = 0; i < keys.size(); i++)
     152                 :           2 :         kv[keys[i]] = values[i];
     153                 :             : }
     154                 :             : 
     155                 :        1239 : bool UniValue::findKey(const std::string& key, size_t& retIdx) const
     156                 :             : {
     157   [ -  +  +  + ]:        3261 :     for (size_t i = 0; i < keys.size(); i++) {
     158         [ +  + ]:        2412 :         if (keys[i] == key) {
     159                 :         390 :             retIdx = i;
     160                 :         390 :             return true;
     161                 :             :         }
     162                 :             :     }
     163                 :             : 
     164                 :             :     return false;
     165                 :             : }
     166                 :             : 
     167                 :           2 : bool UniValue::checkObject(const std::map<std::string,UniValue::VType>& t) const
     168                 :             : {
     169         [ +  - ]:           2 :     if (typ != VOBJ) {
     170                 :             :         return false;
     171                 :             :     }
     172                 :             : 
     173         [ +  + ]:          16 :     for (const auto& object: t) {
     174                 :          15 :         size_t idx = 0;
     175         [ +  - ]:          15 :         if (!findKey(object.first, idx)) {
     176                 :             :             return false;
     177                 :             :         }
     178                 :             : 
     179         [ +  + ]:          15 :         if (values.at(idx).getType() != object.second) {
     180                 :             :             return false;
     181                 :             :         }
     182                 :             :     }
     183                 :             : 
     184                 :             :     return true;
     185                 :             : }
     186                 :             : 
     187                 :         364 : const UniValue& UniValue::operator[](const std::string& key) const
     188                 :             : {
     189         [ +  - ]:         364 :     if (typ != VOBJ)
     190                 :             :         return NullUniValue;
     191                 :             : 
     192                 :         364 :     size_t index = 0;
     193         [ +  + ]:         364 :     if (!findKey(key, index))
     194                 :             :         return NullUniValue;
     195                 :             : 
     196                 :         363 :     return values.at(index);
     197                 :             : }
     198                 :             : 
     199                 :       17247 : const UniValue& UniValue::operator[](size_t index) const
     200                 :             : {
     201         [ +  - ]:       17247 :     if (typ != VOBJ && typ != VARR)
     202                 :             :         return NullUniValue;
     203   [ -  +  +  + ]:       17247 :     if (index >= values.size())
     204                 :             :         return NullUniValue;
     205                 :             : 
     206                 :       17145 :     return values.at(index);
     207                 :             : }
     208                 :             : 
     209                 :      141416 : void UniValue::checkType(const VType& expected) const
     210                 :             : {
     211         [ +  + ]:      141416 :     if (typ != expected) {
     212   [ +  -  +  -  :          54 :         throw type_error{"JSON value of type " + std::string{uvTypeName(typ)} + " is not of expected type " +
                   +  - ]
     213   [ +  -  +  -  :         108 :                                  std::string{uvTypeName(expected)}};
                   +  - ]
     214                 :             :     }
     215                 :      141389 : }
     216                 :             : 
     217                 :          58 : const char *uvTypeName(UniValue::VType t)
     218                 :             : {
     219   [ +  +  +  +  :          58 :     switch (t) {
                +  -  + ]
     220                 :             :     case UniValue::VNULL: return "null";
     221                 :           4 :     case UniValue::VBOOL: return "bool";
     222                 :          10 :     case UniValue::VOBJ: return "object";
     223                 :           9 :     case UniValue::VARR: return "array";
     224                 :          15 :     case UniValue::VSTR: return "string";
     225                 :          14 :     case UniValue::VNUM: return "number";
     226                 :             :     }
     227                 :             : 
     228                 :             :     // not reached
     229                 :           0 :     return nullptr;
     230                 :             : }
     231                 :             : 
     232                 :         493 : const UniValue& UniValue::find_value(std::string_view key) const
     233                 :             : {
     234   [ -  +  +  + ]:        1050 :     for (unsigned int i = 0; i < keys.size(); ++i) {
     235   [ -  +  +  + ]:        1002 :         if (keys[i] == key) {
     236                 :         445 :             return values.at(i);
     237                 :             :         }
     238                 :             :     }
     239                 :             :     return NullUniValue;
     240                 :             : }
     241                 :             : 
     242                 :           4 : void UniValue::reserve(size_t new_cap)
     243                 :             : {
     244                 :           4 :     values.reserve(new_cap);
     245         [ -  + ]:           4 :     if (typ == VOBJ) {
     246                 :           0 :         keys.reserve(new_cap);
     247                 :             :     }
     248                 :           4 : }
        

Generated by: LCOV version 2.0-1