Browse Source

Allow scalars to be root level elements

PUBLISHED_FROM=2f11c7c9fd2c500a4ecb95a855055791b083d5af
Sergey Lyubka 9 years ago
parent
commit
31165b1183
2 changed files with 10 additions and 21 deletions
  1. 1 20
      frozen.c
  2. 9 1
      unit_test.c

+ 1 - 20
frozen.c

@@ -149,14 +149,6 @@ static int test_and_skip(struct frozen *f, int expected) {
   return ch == END_OF_STRING ? JSON_STRING_INCOMPLETE : JSON_STRING_INVALID;
 }
 
-static int test_no_skip(struct frozen *f, int expected) {
-  int ch = cur(f);
-  if (ch == expected) {
-    return 0;
-  }
-  return ch == END_OF_STRING ? JSON_STRING_INCOMPLETE : JSON_STRING_INVALID;
-}
-
 static int is_alpha(int ch) {
   return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
 }
@@ -393,20 +385,9 @@ static int parse_object(struct frozen *f) {
 }
 
 static int doit(struct frozen *f) {
-  int ret = 0;
-
   if (f->cur == 0 || f->end < f->cur) return JSON_STRING_INVALID;
   if (f->end == f->cur) return JSON_STRING_INCOMPLETE;
-
-  if (0 == (ret = test_no_skip(f, '{'))) {
-    TRY(parse_object(f));
-  } else if (0 == (ret = test_no_skip(f, '['))) {
-    TRY(parse_array(f));
-  } else {
-    return ret;
-  }
-
-  return 0;
+  return parse_value(f);
 }
 
 static int json_encode_string(struct json_out *out, const char *p, size_t len) {

+ 9 - 1
unit_test.c

@@ -56,7 +56,7 @@ static int static_num_tests = 0;
 static const char *test_errors(void) {
   /* clang-format off */
   static const char *invalid_tests[] = {
-      "1",        "a:3",           "\x01",         "{:",
+      "p",        "a:3",           "\x01",         "{:",
       " { 1",     "{a:\"\n\"}",    "{a:1x}",       "{a:1e}",
       "{a:.1}",   "{a:0.}",        "{a:0.e}",      "{a:0.e1}",
       "{a:0.1e}", "{a:\"\\u\" } ", "{a:\"\\yx\"}", "{a:\"\\u111r\"}",
@@ -351,6 +351,14 @@ static const char *test_scanf(void) {
     ASSERT(s[3] == '\0');
   }
 
+  {
+    /* Test for scalar value being a root element */
+    int n = 0;
+    const char *str = " true ";
+    ASSERT(json_scanf(str, strlen(str), " %B ", &n) == 1);
+    ASSERT(n == 1);
+  }
+
   return NULL;
 }