Skip to content

Commit

Permalink
Merge pull request #31 from ryanofsky/raw-literals
Browse files Browse the repository at this point in the history
Support parsing raw literals in UniValue
  • Loading branch information
jgarzik authored Jan 29, 2017
2 parents 511008c + 0bb1439 commit 89bb073
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 14 deletions.
7 changes: 6 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"abcdefghijklmnopqrstuvwxyz"
1 change: 1 addition & 0 deletions test/round4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7
1 change: 1 addition & 0 deletions test/round5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true
1 change: 1 addition & 0 deletions test/round6.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
false
1 change: 1 addition & 0 deletions test/round7.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
null
5 changes: 5 additions & 0 deletions test/unitester.cpp
Original file line number Diff line number Diff line change
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 89bb073

Please sign in to comment.