Skip to content

Commit 0bb1439

Browse files
committed
Support parsing raw literals in UniValue
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
1 parent 3f03bfd commit 0bb1439

File tree

9 files changed

+34
-14
lines changed

9 files changed

+34
-14
lines changed

Makefile.am

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ TEST_FILES = \
8888
$(TEST_DATA_DIR)/pass2.json \
8989
$(TEST_DATA_DIR)/pass3.json \
9090
$(TEST_DATA_DIR)/round1.json \
91-
$(TEST_DATA_DIR)/round2.json
91+
$(TEST_DATA_DIR)/round2.json \
92+
$(TEST_DATA_DIR)/round3.json \
93+
$(TEST_DATA_DIR)/round4.json \
94+
$(TEST_DATA_DIR)/round5.json \
95+
$(TEST_DATA_DIR)/round6.json \
96+
$(TEST_DATA_DIR)/round7.json
9297

9398
EXTRA_DIST=$(TEST_FILES) $(GEN_SRCS)

lib/univalue_read.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ enum jtokentype getJsonToken(string& tokenVal, unsigned int& consumed,
177177
string valStr;
178178
JSONUTF8StringFilter writer(valStr);
179179

180-
while (*raw) {
180+
while (true) {
181181
if ((unsigned char)*raw < 0x20)
182182
return JTOK_ERR;
183183

@@ -371,9 +371,6 @@ bool UniValue::read(const char *raw)
371371
case JTOK_KW_NULL:
372372
case JTOK_KW_TRUE:
373373
case JTOK_KW_FALSE: {
374-
if (!stack.size())
375-
return false;
376-
377374
UniValue tmpVal;
378375
switch (tok) {
379376
case JTOK_KW_NULL:
@@ -388,6 +385,11 @@ bool UniValue::read(const char *raw)
388385
default: /* impossible */ break;
389386
}
390387

388+
if (!stack.size()) {
389+
*this = tmpVal;
390+
break;
391+
}
392+
391393
UniValue *top = stack.back();
392394
top->values.push_back(tmpVal);
393395

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

398400
case JTOK_NUMBER: {
399-
if (!stack.size())
400-
return false;
401-
402401
UniValue tmpVal(VNUM, tokenVal);
402+
if (!stack.size()) {
403+
*this = tmpVal;
404+
break;
405+
}
406+
403407
UniValue *top = stack.back();
404408
top->values.push_back(tmpVal);
405409

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

410414
case JTOK_STRING: {
411-
if (!stack.size())
412-
return false;
413-
414-
UniValue *top = stack.back();
415-
416415
if (expect(OBJ_NAME)) {
416+
UniValue *top = stack.back();
417417
top->keys.push_back(tokenVal);
418418
clearExpect(OBJ_NAME);
419419
setExpect(COLON);
420420
} else {
421421
UniValue tmpVal(VSTR, tokenVal);
422+
if (!stack.size()) {
423+
*this = tmpVal;
424+
break;
425+
}
426+
UniValue *top = stack.back();
422427
top->values.push_back(tmpVal);
423428
}
424429

test/fail1.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"A JSON payload should be an object or array, not a string."
1+
"This is a string that never ends, yes it goes on and on, my friends.

test/round3.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"abcdefghijklmnopqrstuvwxyz"

test/round4.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
7

test/round5.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
true

test/round6.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
false

test/round7.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
null

test/unitester.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ static const char *filenames[] = {
125125
"pass3.json",
126126
"round1.json", // round-trip test
127127
"round2.json", // unicode
128+
"round3.json", // bare string
129+
"round4.json", // bare number
130+
"round5.json", // bare true
131+
"round6.json", // bare false
132+
"round7.json", // bare null
128133
};
129134

130135
// Test \u handling

0 commit comments

Comments
 (0)