Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update univalue #248

Merged
merged 2 commits into from
Dec 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/univalue/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pkgconfig_DATA = pc/libunivalue.pc

libunivalue_la_SOURCES = \
lib/univalue.cpp \
lib/univalue_get.cpp \
lib/univalue_read.cpp \
lib/univalue_write.cpp

Expand All @@ -20,7 +21,7 @@ libunivalue_la_LDFLAGS = \
-no-undefined
libunivalue_la_CXXFLAGS = -I$(top_srcdir)/include

TESTS = test/unitester test/no_nul
TESTS = test/object test/unitester test/no_nul

GENBIN = gen/gen$(BUILD_EXEEXT)
GEN_SRCS = gen/gen.cpp
Expand Down Expand Up @@ -52,6 +53,11 @@ test_no_nul_LDADD = libunivalue.la
test_no_nul_CXXFLAGS = -I$(top_srcdir)/include
test_no_nul_LDFLAGS = -static $(LIBTOOL_APP_LDFLAGS)

test_object_SOURCES = test/object.cpp
test_object_LDADD = libunivalue.la
test_object_CXXFLAGS = -I$(top_srcdir)/include
test_object_LDFLAGS = -static $(LIBTOOL_APP_LDFLAGS)

TEST_FILES = \
$(TEST_DATA_DIR)/fail10.json \
$(TEST_DATA_DIR)/fail11.json \
Expand Down Expand Up @@ -88,6 +94,8 @@ TEST_FILES = \
$(TEST_DATA_DIR)/fail40.json \
$(TEST_DATA_DIR)/fail41.json \
$(TEST_DATA_DIR)/fail42.json \
$(TEST_DATA_DIR)/fail44.json \
$(TEST_DATA_DIR)/fail45.json \
$(TEST_DATA_DIR)/fail3.json \
$(TEST_DATA_DIR)/fail4.json \
$(TEST_DATA_DIR)/fail5.json \
Expand All @@ -98,6 +106,7 @@ TEST_FILES = \
$(TEST_DATA_DIR)/pass1.json \
$(TEST_DATA_DIR)/pass2.json \
$(TEST_DATA_DIR)/pass3.json \
$(TEST_DATA_DIR)/pass4.json \
$(TEST_DATA_DIR)/round1.json \
$(TEST_DATA_DIR)/round2.json \
$(TEST_DATA_DIR)/round3.json \
Expand Down
21 changes: 21 additions & 0 deletions src/univalue/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

# UniValue

## Summary

A universal value class, with JSON encoding and decoding.

UniValue is an abstract data type that may be a null, boolean, string,
number, array container, or a key/value dictionary container, nested to
an arbitrary depth.

This class is aligned with the JSON standard, [RFC
7159](https://tools.ietf.org/html/rfc7159.html).

## Library usage

This is a fork of univalue used by Bitcoin Core. It is not maintained for usage
by other projects. Notably, the API may break in non-backward-compatible ways.

Other projects looking for a maintained library should use the upstream
univalue at https://github.com/jgarzik/univalue.
2 changes: 0 additions & 2 deletions src/univalue/gen/gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
#include <string.h>
#include "univalue.h"

using namespace std;

static bool initEscapes;
static std::string escapes[256];

Expand Down
23 changes: 17 additions & 6 deletions src/univalue/include/univalue.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef UNIVALUE_H__
#define UNIVALUE_H__
#ifndef __UNIVALUE_H__
#define __UNIVALUE_H__

#include <stdint.h>
#include <string.h>

#include <string>
#include <vector>
Expand Down Expand Up @@ -69,7 +70,8 @@ class UniValue {
size_t size() const { return values.size(); }

bool getBool() const { return isTrue(); }
bool checkObject(const std::map<std::string,UniValue::VType>& memberTypes);
void getObjMap(std::map<std::string,UniValue>& kv) const;
bool checkObject(const std::map<std::string,UniValue::VType>& memberTypes) const;
const UniValue& operator[](const std::string& key) const;
const UniValue& operator[](size_t index) const;
bool exists(const std::string& key) const { size_t i; return findKey(key, i); }
Expand Down Expand Up @@ -104,8 +106,13 @@ class UniValue {
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);

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 std::string& val_) {
UniValue tmpVal(VSTR, val_);
Expand All @@ -123,6 +130,10 @@ class UniValue {
UniValue tmpVal(val_);
return pushKV(key, tmpVal);
}
bool pushKV(const std::string& key, bool val_) {
UniValue tmpVal((bool)val_);
return pushKV(key, tmpVal);
}
bool pushKV(const std::string& key, int val_) {
UniValue tmpVal((int64_t)val_);
return pushKV(key, tmpVal);
Expand All @@ -137,7 +148,7 @@ class UniValue {
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) {
return read(rawStr.data(), rawStr.size());
}
Expand All @@ -148,7 +159,7 @@ class UniValue {
std::vector<std::string> keys;
std::vector<UniValue> values;

bool findKey(const std::string& key, size_t& ret) const;
bool findKey(const std::string& key, size_t& retIdx) 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;

Expand Down Expand Up @@ -293,4 +304,4 @@ extern const UniValue NullUniValue;

const UniValue& find_value( const UniValue& obj, const std::string& name);

#endif // UNIVALUE_H__
#endif // __UNIVALUE_H__
59 changes: 38 additions & 21 deletions src/univalue/lib/univalue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ bool ParseDouble(const std::string& str, double *out)
}
}

using namespace std;

const UniValue NullUniValue;

void UniValue::clear()
Expand All @@ -102,15 +100,15 @@ bool UniValue::setBool(bool val_)
return true;
}

static bool validNumStr(const string& s)
static bool validNumStr(const std::string& s)
{
string tokenVal;
std::string tokenVal;
unsigned int consumed;
enum jtokentype tt = getJsonToken(tokenVal, consumed, s.data(), s.data() + s.size());
return (tt == JTOK_NUMBER);
}

bool UniValue::setNumStr(const string& val_)
bool UniValue::setNumStr(const std::string& val_)
{
if (!validNumStr(val_))
return false;
Expand All @@ -123,7 +121,7 @@ bool UniValue::setNumStr(const string& val_)

bool UniValue::setInt(uint64_t val_)
{
ostringstream oss;
std::ostringstream oss;

oss << val_;

Expand All @@ -132,7 +130,7 @@ bool UniValue::setInt(uint64_t val_)

bool UniValue::setInt(int64_t val_)
{
ostringstream oss;
std::ostringstream oss;

oss << val_;

Expand All @@ -141,7 +139,7 @@ bool UniValue::setInt(int64_t val_)

bool UniValue::setFloat(double val_)
{
ostringstream oss;
std::ostringstream oss;

oss << std::setprecision(16) << val_;

Expand All @@ -150,7 +148,7 @@ bool UniValue::setFloat(double val_)
return ret;
}

bool UniValue::setStr(const string& val_)
bool UniValue::setStr(const std::string& val_)
{
clear();
typ = VSTR;
Expand Down Expand Up @@ -191,13 +189,22 @@ bool UniValue::push_backV(const std::vector<UniValue>& vec)
return true;
}

void UniValue::__pushKV(const std::string& key, const UniValue& val_)
{
keys.push_back(key);
values.push_back(val_);
}

bool UniValue::pushKV(const std::string& key, const UniValue& val_)
{
if (typ != VOBJ)
return false;

keys.push_back(key);
values.push_back(val_);
size_t idx;
if (findKey(key, idx))
values[idx] = val_;
else
__pushKV(key, val_);
return true;
}

Expand All @@ -206,31 +213,42 @@ bool UniValue::pushKVs(const UniValue& obj)
if (typ != VOBJ || obj.typ != VOBJ)
return false;

for (unsigned int i = 0; i < obj.keys.size(); i++) {
keys.push_back(obj.keys[i]);
values.push_back(obj.values.at(i));
}
for (size_t i = 0; i < obj.keys.size(); i++)
__pushKV(obj.keys[i], obj.values.at(i));

return true;
}

bool UniValue::findKey(const std::string& key, size_t& ret) const
void UniValue::getObjMap(std::map<std::string,UniValue>& kv) const
{
if (typ != VOBJ)
return;

kv.clear();
for (size_t i = 0; i < keys.size(); i++)
kv[keys[i]] = values[i];
}

bool UniValue::findKey(const std::string& key, size_t& retIdx) const
{
for (size_t i = 0; i < keys.size(); i++) {
if (keys[i] == key) {
ret = i;
retIdx = i;
return true;
}
}

return false;
}

bool UniValue::checkObject(const std::map<std::string,UniValue::VType>& t)
bool UniValue::checkObject(const std::map<std::string,UniValue::VType>& t) const
{
if (typ != VOBJ)
return false;

for (std::map<std::string,UniValue::VType>::const_iterator it = t.begin();
it != t.end(); ++it) {
size_t idx;
size_t idx = 0;
if (!findKey(it->first, idx))
return false;

Expand All @@ -246,7 +264,7 @@ const UniValue& UniValue::operator[](const std::string& key) const
if (typ != VOBJ)
return NullUniValue;

size_t index;
size_t index = 0;
if (!findKey(key, index))
return NullUniValue;

Expand Down Expand Up @@ -361,4 +379,3 @@ const UniValue& UniValue::get_array() const
throw std::runtime_error("JSON value is not an array as expected");
return *this;
}

Loading