Changeset View
Changeset View
Standalone View
Standalone View
src/univalue/include/univalue.h
// Copyright 2014 BitPay Inc. | // Copyright 2014 BitPay Inc. | ||||
// Copyright 2015 Bitcoin Core Developers | // Copyright 2015 Bitcoin Core Developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#ifndef __UNIVALUE_H__ | #ifndef __UNIVALUE_H__ | ||||
#define __UNIVALUE_H__ | #define __UNIVALUE_H__ | ||||
#include <stdint.h> | #include <stdint.h> | ||||
#include <string.h> | |||||
#include <string> | #include <string> | ||||
#include <vector> | #include <vector> | ||||
#include <map> | #include <map> | ||||
#include <cassert> | #include <cassert> | ||||
#include <sstream> // .get_int64() | #include <sstream> // .get_int64() | ||||
#include <utility> // std::pair | #include <utility> // std::pair | ||||
▲ Show 20 Lines • Show All 46 Lines • ▼ Show 20 Lines | public: | ||||
enum VType getType() const { return typ; } | enum VType getType() const { return typ; } | ||||
const std::string& getValStr() const { return val; } | const std::string& getValStr() const { return val; } | ||||
bool empty() const { return (values.size() == 0); } | bool empty() const { return (values.size() == 0); } | ||||
size_t size() const { return values.size(); } | size_t size() const { return values.size(); } | ||||
bool getBool() const { return isTrue(); } | bool getBool() const { return isTrue(); } | ||||
void getObjMap(std::map<std::string,UniValue>& kv) const; | bool checkObject(const std::map<std::string,UniValue::VType>& memberTypes); | ||||
bool checkObject(const std::map<std::string,UniValue::VType>& memberTypes) const; | |||||
const UniValue& operator[](const std::string& key) const; | const UniValue& operator[](const std::string& key) const; | ||||
const UniValue& operator[](size_t index) const; | const UniValue& operator[](unsigned int index) const; | ||||
bool exists(const std::string& key) const { size_t i; return findKey(key, i); } | bool exists(const std::string& key) const { return (findKey(key) >= 0); } | ||||
bool isNull() const { return (typ == VNULL); } | bool isNull() const { return (typ == VNULL); } | ||||
bool isTrue() const { return (typ == VBOOL) && (val == "1"); } | bool isTrue() const { return (typ == VBOOL) && (val == "1"); } | ||||
bool isFalse() const { return (typ == VBOOL) && (val != "1"); } | bool isFalse() const { return (typ == VBOOL) && (val != "1"); } | ||||
bool isBool() const { return (typ == VBOOL); } | bool isBool() const { return (typ == VBOOL); } | ||||
bool isStr() const { return (typ == VSTR); } | bool isStr() const { return (typ == VSTR); } | ||||
bool isNum() const { return (typ == VNUM); } | bool isNum() const { return (typ == VNUM); } | ||||
bool isArray() const { return (typ == VARR); } | bool isArray() const { return (typ == VARR); } | ||||
bool isObject() const { return (typ == VOBJ); } | bool isObject() const { return (typ == VOBJ); } | ||||
bool push_back(const UniValue& val); | bool push_back(const UniValue& val); | ||||
bool push_back(const std::string& val_) { | bool push_back(const std::string& val_) { | ||||
UniValue tmpVal(VSTR, val_); | UniValue tmpVal(VSTR, val_); | ||||
return push_back(tmpVal); | return push_back(tmpVal); | ||||
} | } | ||||
bool push_back(const char *val_) { | bool push_back(const char *val_) { | ||||
std::string s(val_); | std::string s(val_); | ||||
return push_back(s); | return push_back(s); | ||||
} | } | ||||
bool push_back(uint64_t val_) { | |||||
UniValue tmpVal(val_); | |||||
return push_back(tmpVal); | |||||
} | |||||
bool push_back(int64_t val_) { | |||||
UniValue tmpVal(val_); | |||||
return push_back(tmpVal); | |||||
} | |||||
bool push_back(int val_) { | |||||
UniValue tmpVal(val_); | |||||
return push_back(tmpVal); | |||||
} | |||||
bool push_back(double val_) { | |||||
UniValue tmpVal(val_); | |||||
return push_back(tmpVal); | |||||
} | |||||
bool push_backV(const std::vector<UniValue>& vec); | bool push_backV(const std::vector<UniValue>& vec); | ||||
void __pushKV(const std::string& key, const UniValue& val); | |||||
bool pushKV(const std::string& key, const UniValue& val); | bool pushKV(const std::string& key, const UniValue& val); | ||||
bool pushKV(const std::string& key, const std::string& val_) { | bool pushKV(const std::string& key, const std::string& val_) { | ||||
UniValue tmpVal(VSTR, val_); | UniValue tmpVal(VSTR, val_); | ||||
return pushKV(key, tmpVal); | return pushKV(key, tmpVal); | ||||
} | } | ||||
bool pushKV(const std::string& key, const char *val_) { | bool pushKV(const std::string& key, const char *val_) { | ||||
std::string _val(val_); | std::string _val(val_); | ||||
return pushKV(key, _val); | return pushKV(key, _val); | ||||
Show All 14 Lines | bool pushKV(const std::string& key, double val_) { | ||||
UniValue tmpVal(val_); | UniValue tmpVal(val_); | ||||
return pushKV(key, tmpVal); | return pushKV(key, tmpVal); | ||||
} | } | ||||
bool pushKVs(const UniValue& obj); | bool pushKVs(const UniValue& obj); | ||||
std::string write(unsigned int prettyIndent = 0, | std::string write(unsigned int prettyIndent = 0, | ||||
unsigned int indentLevel = 0) const; | unsigned int indentLevel = 0) const; | ||||
bool read(const char *raw, size_t len); | bool read(const char *raw); | ||||
bool read(const char *raw) { return read(raw, strlen(raw)); } | |||||
bool read(const std::string& rawStr) { | bool read(const std::string& rawStr) { | ||||
return read(rawStr.data(), rawStr.size()); | return read(rawStr.c_str()); | ||||
} | } | ||||
private: | private: | ||||
UniValue::VType typ; | UniValue::VType typ; | ||||
std::string val; // numbers are stored as C++ strings | std::string val; // numbers are stored as C++ strings | ||||
std::vector<std::string> keys; | std::vector<std::string> keys; | ||||
std::vector<UniValue> values; | std::vector<UniValue> values; | ||||
bool findKey(const std::string& key, size_t& retIdx) const; | int findKey(const std::string& key) const; | ||||
void writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const; | void writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const; | ||||
void writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const; | void writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const; | ||||
public: | public: | ||||
// Strict type-specific getters, these throw std::runtime_error if the | // Strict type-specific getters, these throw std::runtime_error if the | ||||
// value is of unexpected type | // value is of unexpected type | ||||
const std::vector<std::string>& getKeys() const; | const std::vector<std::string>& getKeys() const; | ||||
const std::vector<UniValue>& getValues() const; | const std::vector<UniValue>& getValues() const; | ||||
▲ Show 20 Lines • Show All 88 Lines • ▼ Show 20 Lines | enum jtokentype { | ||||
JTOK_KW_NULL, | JTOK_KW_NULL, | ||||
JTOK_KW_TRUE, | JTOK_KW_TRUE, | ||||
JTOK_KW_FALSE, | JTOK_KW_FALSE, | ||||
JTOK_NUMBER, | JTOK_NUMBER, | ||||
JTOK_STRING, | JTOK_STRING, | ||||
}; | }; | ||||
extern enum jtokentype getJsonToken(std::string& tokenVal, | extern enum jtokentype getJsonToken(std::string& tokenVal, | ||||
unsigned int& consumed, const char *raw, const char *end); | unsigned int& consumed, const char *raw); | ||||
extern const char *uvTypeName(UniValue::VType t); | extern const char *uvTypeName(UniValue::VType t); | ||||
static inline bool jsonTokenIsValue(enum jtokentype jtt) | static inline bool jsonTokenIsValue(enum jtokentype jtt) | ||||
{ | { | ||||
switch (jtt) { | switch (jtt) { | ||||
case JTOK_KW_NULL: | case JTOK_KW_NULL: | ||||
case JTOK_KW_TRUE: | case JTOK_KW_TRUE: | ||||
case JTOK_KW_FALSE: | case JTOK_KW_FALSE: | ||||
Show All 32 Lines |