Skip to content

Commit

Permalink
Support parsing raw literals in UniValue
Browse files Browse the repository at this point in the history
Fixes following test failures in https://github.com/nst/JSONTestSuite:

bitcoin SHOULD_HAVE_PASSED  y_structure_lonely_negative_real.json
bitcoin SHOULD_HAVE_PASSED  y_structure_lonely_string.json
bitcoin SHOULD_HAVE_PASSED  y_structure_lonely_false.json
bitcoin SHOULD_HAVE_PASSED  y_structure_lonely_null.json
bitcoin SHOULD_HAVE_PASSED  y_string_space.json
bitcoin SHOULD_HAVE_PASSED  y_structure_string_empty.json
bitcoin SHOULD_HAVE_PASSED  y_structure_lonely_int.json
bitcoin SHOULD_HAVE_PASSED  y_structure_lonely_true.json
  • Loading branch information
ryanofsky committed Nov 8, 2016
1 parent 3f03bfd commit 0bb1439
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 14 deletions.
7 changes: 6 additions & 1 deletion Makefile.am
Expand Up @@ -88,6 +88,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)
29 changes: 17 additions & 12 deletions lib/univalue_read.cpp
Expand Up @@ -177,7 +177,7 @@ enum jtokentype getJsonToken(string& tokenVal, unsigned int& consumed,
string valStr;
JSONUTF8StringFilter writer(valStr);

while (*raw) {
while (true) {
if ((unsigned char)*raw < 0x20)
return JTOK_ERR;

Expand Down Expand Up @@ -371,9 +371,6 @@ bool UniValue::read(const char *raw)
case JTOK_KW_NULL:
case JTOK_KW_TRUE:
case JTOK_KW_FALSE: {
if (!stack.size())
return false;

UniValue tmpVal;
switch (tok) {
case JTOK_KW_NULL:
Expand All @@ -388,6 +385,11 @@ bool UniValue::read(const char *raw)
default: /* impossible */ break;
}

if (!stack.size()) {
*this = tmpVal;
break;
}

UniValue *top = stack.back();
top->values.push_back(tmpVal);

Expand All @@ -396,10 +398,12 @@ bool UniValue::read(const char *raw)
}

case JTOK_NUMBER: {
if (!stack.size())
return false;

UniValue tmpVal(VNUM, tokenVal);
if (!stack.size()) {
*this = tmpVal;
break;
}

UniValue *top = stack.back();
top->values.push_back(tmpVal);

Expand All @@ -408,17 +412,18 @@ bool UniValue::read(const char *raw)
}

case JTOK_STRING: {
if (!stack.size())
return false;

UniValue *top = stack.back();

if (expect(OBJ_NAME)) {
UniValue *top = stack.back();
top->keys.push_back(tokenVal);
clearExpect(OBJ_NAME);
setExpect(COLON);
} else {
UniValue tmpVal(VSTR, tokenVal);
if (!stack.size()) {
*this = tmpVal;
break;
}
UniValue *top = stack.back();
top->values.push_back(tmpVal);
}

Expand Down
2 changes: 1 addition & 1 deletion test/fail1.json
@@ -1 +1 @@
"A JSON payload should be an object or array, not a string."
"This is a string that never ends, yes it goes on and on, my friends.
1 change: 1 addition & 0 deletions test/round3.json
@@ -0,0 +1 @@
"abcdefghijklmnopqrstuvwxyz"
1 change: 1 addition & 0 deletions test/round4.json
@@ -0,0 +1 @@
7
1 change: 1 addition & 0 deletions test/round5.json
@@ -0,0 +1 @@
true
1 change: 1 addition & 0 deletions test/round6.json
@@ -0,0 +1 @@
false
1 change: 1 addition & 0 deletions test/round7.json
@@ -0,0 +1 @@
null
5 changes: 5 additions & 0 deletions test/unitester.cpp
Expand Up @@ -125,6 +125,11 @@ static const char *filenames[] = {
"pass3.json",
"round1.json", // round-trip test
"round2.json", // unicode
"round3.json", // bare string
"round4.json", // bare number
"round5.json", // bare true
"round6.json", // bare false
"round7.json", // bare null
};

// Test \u handling
Expand Down

0 comments on commit 0bb1439

Please sign in to comment.