Skip to content

Commit

Permalink
Merge #4: Pull upstream
Browse files Browse the repository at this point in the history
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
  • Loading branch information
laanwj committed Oct 9, 2017
2 parents 16a1f7f + 8a2d6f1 commit a9e53b3
Show file tree
Hide file tree
Showing 23 changed files with 782 additions and 228 deletions.
29 changes: 26 additions & 3 deletions 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
TESTS = test/object test/unitester test/no_nul

GENBIN = gen/gen$(BUILD_EXEEXT)
GEN_SRCS = gen/gen.cpp
Expand All @@ -33,7 +34,7 @@ gen: lib/univalue_escapes.h $(GENBIN)
@echo Updating $<
$(AM_V_at)$(GENBIN) > lib/univalue_escapes.h

noinst_PROGRAMS = $(TESTS)
noinst_PROGRAMS = $(TESTS) test/test_json

TEST_DATA_DIR=test

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

test_test_json_SOURCES = test/test_json.cpp
test_test_json_LDADD = libunivalue.la
test_test_json_CXXFLAGS = -I$(top_srcdir)/include
test_test_json_LDFLAGS = -static $(LIBTOOL_APP_LDFLAGS)

test_no_nul_SOURCES = test/no_nul.cpp
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 @@ -77,6 +93,8 @@ TEST_FILES = \
$(TEST_DATA_DIR)/fail39.json \
$(TEST_DATA_DIR)/fail40.json \
$(TEST_DATA_DIR)/fail41.json \
$(TEST_DATA_DIR)/fail42.json \
$(TEST_DATA_DIR)/fail44.json \
$(TEST_DATA_DIR)/fail3.json \
$(TEST_DATA_DIR)/fail4.json \
$(TEST_DATA_DIR)/fail5.json \
Expand All @@ -88,6 +106,11 @@ TEST_FILES = \
$(TEST_DATA_DIR)/pass2.json \
$(TEST_DATA_DIR)/pass3.json \
$(TEST_DATA_DIR)/round1.json \
$(TEST_DATA_DIR)/round2.json
$(TEST_DATA_DIR)/round2.json \
$(TEST_DATA_DIR)/round3.json \
$(TEST_DATA_DIR)/round4.json \
$(TEST_DATA_DIR)/round5.json \
$(TEST_DATA_DIR)/round6.json \
$(TEST_DATA_DIR)/round7.json

EXTRA_DIST=$(TEST_FILES) $(GEN_SRCS)
7 changes: 0 additions & 7 deletions README

This file was deleted.

32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

# 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).

## Installation

This project is a standard GNU
[autotools](https://www.gnu.org/software/automake/manual/html_node/Autotools-Introduction.html)
project. Build and install instructions are available in the `INSTALL`
file provided with GNU autotools.

```
$ ./autogen.sh
$ ./configure
$ make
```

## Design

UniValue provides a single dynamic RAII C++ object class,
and minimizes template use (contra json_spirit).

6 changes: 3 additions & 3 deletions configure.ac
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
m4_define([libunivalue_major_version], [1])
m4_define([libunivalue_minor_version], [1])
m4_define([libunivalue_micro_version], [2])
m4_define([libunivalue_interface_age], [2])
m4_define([libunivalue_micro_version], [3])
m4_define([libunivalue_interface_age], [3])
# If you need a modifier for the version number.
# Normally empty, but can be used to make "fixup" releases.
m4_define([libunivalue_extraversion], [])
Expand All @@ -14,7 +14,7 @@ m4_define([libunivalue_age], [m4_eval(libunivalue_binary_age - libunivalue_inter
m4_define([libunivalue_version], [libunivalue_major_version().libunivalue_minor_version().libunivalue_micro_version()libunivalue_extraversion()])


AC_INIT([univalue], [1.0.2],
AC_INIT([univalue], [1.0.3],
[http://github.com/jgarzik/univalue/])

dnl make the compilation flags quiet unless V=1 is used
Expand Down
34 changes: 27 additions & 7 deletions include/univalue.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define __UNIVALUE_H__

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

#include <string>
#include <vector>
Expand Down Expand Up @@ -69,10 +70,11 @@ 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[](unsigned int index) const;
bool exists(const std::string& key) const { return (findKey(key) >= 0); }
const UniValue& operator[](size_t index) const;
bool exists(const std::string& key) const { size_t i; return findKey(key, i); }

bool isNull() const { return (typ == VNULL); }
bool isTrue() const { return (typ == VBOOL) && (val == "1"); }
Expand All @@ -92,8 +94,25 @@ class UniValue {
std::string s(val_);
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);

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 Down Expand Up @@ -124,9 +143,10 @@ class UniValue {
std::string write(unsigned int prettyIndent = 0,
unsigned int indentLevel = 0) const;

bool read(const char *raw);
bool read(const char *raw, size_t len);
bool read(const char *raw) { return read(raw, strlen(raw)); }
bool read(const std::string& rawStr) {
return read(rawStr.c_str());
return read(rawStr.data(), rawStr.size());
}

private:
Expand All @@ -135,7 +155,7 @@ class UniValue {
std::vector<std::string> keys;
std::vector<UniValue> values;

int findKey(const std::string& key) 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 @@ -240,7 +260,7 @@ enum jtokentype {
};

extern enum jtokentype getJsonToken(std::string& tokenVal,
unsigned int& consumed, const char *raw);
unsigned int& consumed, const char *raw, const char *end);
extern const char *uvTypeName(UniValue::VType t);

static inline bool jsonTokenIsValue(enum jtokentype jtt)
Expand Down
Loading

0 comments on commit a9e53b3

Please sign in to comment.