Bitcoin Core  0.19.99
P2P Digital Currency
univalue.cpp
Go to the documentation of this file.
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 http://www.opensource.org/licenses/mit-license.php.
5 
6 #include <stdint.h>
7 #include <iomanip>
8 #include <sstream>
9 #include <stdlib.h>
10 
11 #include "univalue.h"
12 
14 
16 {
17  typ = VNULL;
18  val.clear();
19  keys.clear();
20  values.clear();
21 }
22 
24 {
25  clear();
26  return true;
27 }
28 
29 bool UniValue::setBool(bool val_)
30 {
31  clear();
32  typ = VBOOL;
33  if (val_)
34  val = "1";
35  return true;
36 }
37 
38 static bool validNumStr(const std::string& s)
39 {
40  std::string tokenVal;
41  unsigned int consumed;
42  enum jtokentype tt = getJsonToken(tokenVal, consumed, s.data(), s.data() + s.size());
43  return (tt == JTOK_NUMBER);
44 }
45 
46 bool UniValue::setNumStr(const std::string& val_)
47 {
48  if (!validNumStr(val_))
49  return false;
50 
51  clear();
52  typ = VNUM;
53  val = val_;
54  return true;
55 }
56 
57 bool UniValue::setInt(uint64_t val_)
58 {
59  std::ostringstream oss;
60 
61  oss << val_;
62 
63  return setNumStr(oss.str());
64 }
65 
66 bool UniValue::setInt(int64_t val_)
67 {
68  std::ostringstream oss;
69 
70  oss << val_;
71 
72  return setNumStr(oss.str());
73 }
74 
75 bool UniValue::setFloat(double val_)
76 {
77  std::ostringstream oss;
78 
79  oss << std::setprecision(16) << val_;
80 
81  bool ret = setNumStr(oss.str());
82  typ = VNUM;
83  return ret;
84 }
85 
86 bool UniValue::setStr(const std::string& val_)
87 {
88  clear();
89  typ = VSTR;
90  val = val_;
91  return true;
92 }
93 
95 {
96  clear();
97  typ = VARR;
98  return true;
99 }
100 
102 {
103  clear();
104  typ = VOBJ;
105  return true;
106 }
107 
108 bool UniValue::push_back(const UniValue& val_)
109 {
110  if (typ != VARR)
111  return false;
112 
113  values.push_back(val_);
114  return true;
115 }
116 
117 bool UniValue::push_backV(const std::vector<UniValue>& vec)
118 {
119  if (typ != VARR)
120  return false;
121 
122  values.insert(values.end(), vec.begin(), vec.end());
123 
124  return true;
125 }
126 
127 void UniValue::__pushKV(const std::string& key, const UniValue& val_)
128 {
129  keys.push_back(key);
130  values.push_back(val_);
131 }
132 
133 bool UniValue::pushKV(const std::string& key, const UniValue& val_)
134 {
135  if (typ != VOBJ)
136  return false;
137 
138  size_t idx;
139  if (findKey(key, idx))
140  values[idx] = val_;
141  else
142  __pushKV(key, val_);
143  return true;
144 }
145 
146 bool UniValue::pushKVs(const UniValue& obj)
147 {
148  if (typ != VOBJ || obj.typ != VOBJ)
149  return false;
150 
151  for (size_t i = 0; i < obj.keys.size(); i++)
152  __pushKV(obj.keys[i], obj.values.at(i));
153 
154  return true;
155 }
156 
157 void UniValue::getObjMap(std::map<std::string,UniValue>& kv) const
158 {
159  if (typ != VOBJ)
160  return;
161 
162  kv.clear();
163  for (size_t i = 0; i < keys.size(); i++)
164  kv[keys[i]] = values[i];
165 }
166 
167 bool UniValue::findKey(const std::string& key, size_t& retIdx) const
168 {
169  for (size_t i = 0; i < keys.size(); i++) {
170  if (keys[i] == key) {
171  retIdx = i;
172  return true;
173  }
174  }
175 
176  return false;
177 }
178 
179 bool UniValue::checkObject(const std::map<std::string,UniValue::VType>& t) const
180 {
181  if (typ != VOBJ)
182  return false;
183 
184  for (std::map<std::string,UniValue::VType>::const_iterator it = t.begin();
185  it != t.end(); ++it) {
186  size_t idx = 0;
187  if (!findKey(it->first, idx))
188  return false;
189 
190  if (values.at(idx).getType() != it->second)
191  return false;
192  }
193 
194  return true;
195 }
196 
197 const UniValue& UniValue::operator[](const std::string& key) const
198 {
199  if (typ != VOBJ)
200  return NullUniValue;
201 
202  size_t index = 0;
203  if (!findKey(key, index))
204  return NullUniValue;
205 
206  return values.at(index);
207 }
208 
209 const UniValue& UniValue::operator[](size_t index) const
210 {
211  if (typ != VOBJ && typ != VARR)
212  return NullUniValue;
213  if (index >= values.size())
214  return NullUniValue;
215 
216  return values.at(index);
217 }
218 
220 {
221  switch (t) {
222  case UniValue::VNULL: return "null";
223  case UniValue::VBOOL: return "bool";
224  case UniValue::VOBJ: return "object";
225  case UniValue::VARR: return "array";
226  case UniValue::VSTR: return "string";
227  case UniValue::VNUM: return "number";
228  }
229 
230  // not reached
231  return NULL;
232 }
233 
234 const UniValue& find_value(const UniValue& obj, const std::string& name)
235 {
236  for (unsigned int i = 0; i < obj.keys.size(); i++)
237  if (obj.keys[i] == name)
238  return obj.values.at(i);
239 
240  return NullUniValue;
241 }
242 
bool setNumStr(const std::string &val)
Definition: univalue.cpp:46
const char * uvTypeName(UniValue::VType t)
Definition: univalue.cpp:219
bool findKey(const std::string &key, size_t &retIdx) const
Definition: univalue.cpp:167
std::vector< UniValue > values
Definition: univalue.h:158
bool pushKVs(const UniValue &obj)
Definition: univalue.cpp:146
const UniValue & operator[](const std::string &key) const
Definition: univalue.cpp:197
enum jtokentype getJsonToken(std::string &tokenVal, unsigned int &consumed, const char *raw, const char *end)
bool push_back(const UniValue &val)
Definition: univalue.cpp:108
const char * name
Definition: rest.cpp:40
void getObjMap(std::map< std::string, UniValue > &kv) const
Definition: univalue.cpp:157
jtokentype
Definition: univalue.h:181
bool setFloat(double val)
Definition: univalue.cpp:75
bool pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:133
bool setNull()
Definition: univalue.cpp:23
std::string val
Definition: univalue.h:156
bool checkObject(const std::map< std::string, UniValue::VType > &memberTypes) const
Definition: univalue.cpp:179
bool setStr(const std::string &val)
Definition: univalue.cpp:86
UniValue::VType typ
Definition: univalue.h:155
bool setInt(uint64_t val)
Definition: univalue.cpp:57
bool setArray()
Definition: univalue.cpp:94
bool push_backV(const std::vector< UniValue > &vec)
Definition: univalue.cpp:117
bool setObject()
Definition: univalue.cpp:101
bool setBool(bool val)
Definition: univalue.cpp:29
std::vector< std::string > keys
Definition: univalue.h:157
friend const UniValue & find_value(const UniValue &obj, const std::string &name)
Definition: univalue.cpp:234
void clear()
Definition: univalue.cpp:15
void __pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:127
auto it
Definition: validation.cpp:362
static bool validNumStr(const std::string &s)
Definition: univalue.cpp:38
const UniValue NullUniValue
Definition: univalue.cpp:13