Skip to content

Commit a9e53b3

Browse files
committed
Merge #4: Pull upstream
ba341a2 Add getObjMap() helper method. Also, constify checkObject(). (Jeff Garzik) ceb1194 Handle .pushKV() and .checkObject() edge cases. (Jeff Garzik) 107db98 Add ::push_back(double) method for feature parity. (Jeff Garzik) d415300 Move one-line implementation of UniValue::read() to header. (Jeff Garzik) 52e85b3 Move exception-throwing get_* methods into separate implementation module. (Jeff Garzik) dac5296 README.md: update code quotes (Jeff Garzik) 3e31dcf README.md: close code quote (Jeff Garzik) d09b842 Update README.md (Jeff Garzik) f1b86ed Convert README to markdown style. (Jeff Garzik) 1dfe464 Import UniValue class unit tests from bitcoin project. (Jeff Garzik) 0d3e74d operator[] takes size_t index parameter (versus unsigned int) (kozyilmaz) 640158f Private findKey() method becomes size_t clean, and returns bool on failure. (kozyilmaz) 4fd5444 Reject unterminated strings (Russell Yanofsky) a31231b Version 1.0.3 (Jeff Garzik) cfa0384 Convenience wrappers for push_back-ing integer types (isle2983) fd32d1a Don't require nul-terminated string inputs (Russell Yanofsky) 0bb1439 Support parsing raw literals in UniValue (Russell Yanofsky) 839ccd7 Add test driver for JSONTestSuite (Russell Yanofsky) a38fcd3 Do not shadow member variable codepoint. (Pavel Janík) 26ef3ff Remove trailing whitespace from JSON export (BtcDrak) Pull request description: No rush, feel free to keep this open until upstream merged the outstanding pulls. Tree-SHA512: f25fefc0319d8a02802fc802d75bada17ad583c81d92005947d93fb16ff7cff03b5fd92d986dccfec24cbfd13d87e08afef1efd6bac42ec0d57c6901930a5f50
2 parents 16a1f7f + 8a2d6f1 commit a9e53b3

23 files changed

+782
-228
lines changed

Makefile.am

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pkgconfig_DATA = pc/libunivalue.pc
1212

1313
libunivalue_la_SOURCES = \
1414
lib/univalue.cpp \
15+
lib/univalue_get.cpp \
1516
lib/univalue_read.cpp \
1617
lib/univalue_write.cpp
1718

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

23-
TESTS = test/unitester
24+
TESTS = test/object test/unitester test/no_nul
2425

2526
GENBIN = gen/gen$(BUILD_EXEEXT)
2627
GEN_SRCS = gen/gen.cpp
@@ -33,7 +34,7 @@ gen: lib/univalue_escapes.h $(GENBIN)
3334
@echo Updating $<
3435
$(AM_V_at)$(GENBIN) > lib/univalue_escapes.h
3536

36-
noinst_PROGRAMS = $(TESTS)
37+
noinst_PROGRAMS = $(TESTS) test/test_json
3738

3839
TEST_DATA_DIR=test
3940

@@ -42,6 +43,21 @@ test_unitester_LDADD = libunivalue.la
4243
test_unitester_CXXFLAGS = -I$(top_srcdir)/include -DJSON_TEST_SRC=\"$(srcdir)/$(TEST_DATA_DIR)\"
4344
test_unitester_LDFLAGS = -static $(LIBTOOL_APP_LDFLAGS)
4445

46+
test_test_json_SOURCES = test/test_json.cpp
47+
test_test_json_LDADD = libunivalue.la
48+
test_test_json_CXXFLAGS = -I$(top_srcdir)/include
49+
test_test_json_LDFLAGS = -static $(LIBTOOL_APP_LDFLAGS)
50+
51+
test_no_nul_SOURCES = test/no_nul.cpp
52+
test_no_nul_LDADD = libunivalue.la
53+
test_no_nul_CXXFLAGS = -I$(top_srcdir)/include
54+
test_no_nul_LDFLAGS = -static $(LIBTOOL_APP_LDFLAGS)
55+
56+
test_object_SOURCES = test/object.cpp
57+
test_object_LDADD = libunivalue.la
58+
test_object_CXXFLAGS = -I$(top_srcdir)/include
59+
test_object_LDFLAGS = -static $(LIBTOOL_APP_LDFLAGS)
60+
4561
TEST_FILES = \
4662
$(TEST_DATA_DIR)/fail10.json \
4763
$(TEST_DATA_DIR)/fail11.json \
@@ -77,6 +93,8 @@ TEST_FILES = \
7793
$(TEST_DATA_DIR)/fail39.json \
7894
$(TEST_DATA_DIR)/fail40.json \
7995
$(TEST_DATA_DIR)/fail41.json \
96+
$(TEST_DATA_DIR)/fail42.json \
97+
$(TEST_DATA_DIR)/fail44.json \
8098
$(TEST_DATA_DIR)/fail3.json \
8199
$(TEST_DATA_DIR)/fail4.json \
82100
$(TEST_DATA_DIR)/fail5.json \
@@ -88,6 +106,11 @@ TEST_FILES = \
88106
$(TEST_DATA_DIR)/pass2.json \
89107
$(TEST_DATA_DIR)/pass3.json \
90108
$(TEST_DATA_DIR)/round1.json \
91-
$(TEST_DATA_DIR)/round2.json
109+
$(TEST_DATA_DIR)/round2.json \
110+
$(TEST_DATA_DIR)/round3.json \
111+
$(TEST_DATA_DIR)/round4.json \
112+
$(TEST_DATA_DIR)/round5.json \
113+
$(TEST_DATA_DIR)/round6.json \
114+
$(TEST_DATA_DIR)/round7.json
92115

93116
EXTRA_DIST=$(TEST_FILES) $(GEN_SRCS)

README

Lines changed: 0 additions & 7 deletions
This file was deleted.

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
# UniValue
3+
4+
## Summary
5+
6+
A universal value class, with JSON encoding and decoding.
7+
8+
UniValue is an abstract data type that may be a null, boolean, string,
9+
number, array container, or a key/value dictionary container, nested to
10+
an arbitrary depth.
11+
12+
This class is aligned with the JSON standard, [RFC
13+
7159](https://tools.ietf.org/html/rfc7159.html).
14+
15+
## Installation
16+
17+
This project is a standard GNU
18+
[autotools](https://www.gnu.org/software/automake/manual/html_node/Autotools-Introduction.html)
19+
project. Build and install instructions are available in the `INSTALL`
20+
file provided with GNU autotools.
21+
22+
```
23+
$ ./autogen.sh
24+
$ ./configure
25+
$ make
26+
```
27+
28+
## Design
29+
30+
UniValue provides a single dynamic RAII C++ object class,
31+
and minimizes template use (contra json_spirit).
32+

configure.ac

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
m4_define([libunivalue_major_version], [1])
22
m4_define([libunivalue_minor_version], [1])
3-
m4_define([libunivalue_micro_version], [2])
4-
m4_define([libunivalue_interface_age], [2])
3+
m4_define([libunivalue_micro_version], [3])
4+
m4_define([libunivalue_interface_age], [3])
55
# If you need a modifier for the version number.
66
# Normally empty, but can be used to make "fixup" releases.
77
m4_define([libunivalue_extraversion], [])
@@ -14,7 +14,7 @@ m4_define([libunivalue_age], [m4_eval(libunivalue_binary_age - libunivalue_inter
1414
m4_define([libunivalue_version], [libunivalue_major_version().libunivalue_minor_version().libunivalue_micro_version()libunivalue_extraversion()])
1515

1616

17-
AC_INIT([univalue], [1.0.2],
17+
AC_INIT([univalue], [1.0.3],
1818
[http://github.com/jgarzik/univalue/])
1919

2020
dnl make the compilation flags quiet unless V=1 is used

include/univalue.h

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#define __UNIVALUE_H__
88

99
#include <stdint.h>
10+
#include <string.h>
1011

1112
#include <string>
1213
#include <vector>
@@ -69,10 +70,11 @@ class UniValue {
6970
size_t size() const { return values.size(); }
7071

7172
bool getBool() const { return isTrue(); }
72-
bool checkObject(const std::map<std::string,UniValue::VType>& memberTypes);
73+
void getObjMap(std::map<std::string,UniValue>& kv) const;
74+
bool checkObject(const std::map<std::string,UniValue::VType>& memberTypes) const;
7375
const UniValue& operator[](const std::string& key) const;
74-
const UniValue& operator[](unsigned int index) const;
75-
bool exists(const std::string& key) const { return (findKey(key) >= 0); }
76+
const UniValue& operator[](size_t index) const;
77+
bool exists(const std::string& key) const { size_t i; return findKey(key, i); }
7678

7779
bool isNull() const { return (typ == VNULL); }
7880
bool isTrue() const { return (typ == VBOOL) && (val == "1"); }
@@ -92,8 +94,25 @@ class UniValue {
9294
std::string s(val_);
9395
return push_back(s);
9496
}
97+
bool push_back(uint64_t val_) {
98+
UniValue tmpVal(val_);
99+
return push_back(tmpVal);
100+
}
101+
bool push_back(int64_t val_) {
102+
UniValue tmpVal(val_);
103+
return push_back(tmpVal);
104+
}
105+
bool push_back(int val_) {
106+
UniValue tmpVal(val_);
107+
return push_back(tmpVal);
108+
}
109+
bool push_back(double val_) {
110+
UniValue tmpVal(val_);
111+
return push_back(tmpVal);
112+
}
95113
bool push_backV(const std::vector<UniValue>& vec);
96114

115+
void __pushKV(const std::string& key, const UniValue& val);
97116
bool pushKV(const std::string& key, const UniValue& val);
98117
bool pushKV(const std::string& key, const std::string& val_) {
99118
UniValue tmpVal(VSTR, val_);
@@ -124,9 +143,10 @@ class UniValue {
124143
std::string write(unsigned int prettyIndent = 0,
125144
unsigned int indentLevel = 0) const;
126145

127-
bool read(const char *raw);
146+
bool read(const char *raw, size_t len);
147+
bool read(const char *raw) { return read(raw, strlen(raw)); }
128148
bool read(const std::string& rawStr) {
129-
return read(rawStr.c_str());
149+
return read(rawStr.data(), rawStr.size());
130150
}
131151

132152
private:
@@ -135,7 +155,7 @@ class UniValue {
135155
std::vector<std::string> keys;
136156
std::vector<UniValue> values;
137157

138-
int findKey(const std::string& key) const;
158+
bool findKey(const std::string& key, size_t& retIdx) const;
139159
void writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const;
140160
void writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const;
141161

@@ -240,7 +260,7 @@ enum jtokentype {
240260
};
241261

242262
extern enum jtokentype getJsonToken(std::string& tokenVal,
243-
unsigned int& consumed, const char *raw);
263+
unsigned int& consumed, const char *raw, const char *end);
244264
extern const char *uvTypeName(UniValue::VType t);
245265

246266
static inline bool jsonTokenIsValue(enum jtokentype jtt)

0 commit comments

Comments
 (0)