]> thyrsus.com Git - microjson.git/commitdiff
Improvements to the first worked example, and a typo fix.
authorEric S. Raymond <esr@thyrsus.com>
Wed, 17 Sep 2014 19:44:59 +0000 (15:44 -0400)
committerEric S. Raymond <esr@thyrsus.com>
Wed, 17 Sep 2014 19:44:59 +0000 (15:44 -0400)
example1.c
microjson.asc
microjson.c

index 4dbd94563ae01f73af315f658f355ae6e9646c40..96aa855609c50291d5add940363beb90edc7fb6b 100644 (file)
@@ -10,8 +10,6 @@
 
 #include "microjson.h"
 
-static const char *simple = "{\"flag1\":true,\"flag2\":false,\"count\":42}";
-
 static bool flag1, flag2;
 static int count;
 
@@ -26,10 +24,11 @@ int main(int argc, char *argv[])
 {
     int status = 0;
 
-    puts(simple);
-    json_read_object(simple, json_attrs, NULL);
-    printf("count = %d, flag1 = %d, flag2 = %d\n",
-          count, flag1, flag2);
+    status = json_read_object(argv[1], json_attrs, NULL);
+    printf("status = %d, count = %d, flag1 = %d, flag2 = %d\n",
+          status, count, flag1, flag2);
+    if (status != 0)
+       puts(json_error_string(status));
 }
 
 /* end */
index 89913f4184ff1e0364c845f623eb26aa3e3655d9..1ba5d9e03cf3579e82b29f4d563aa74a91b2df5f 100644 (file)
@@ -31,8 +31,6 @@ Here is nearly the simplest possible example:
 
 #include "microjson.h"
 
-static const char *simple = "{\"flag1\":true,\"flag2\":false,\"count\":42}";
-
 static bool flag1, flag2;
 static int count;
 
@@ -47,30 +45,64 @@ int main(int argc, char *argv[])
 {
     int status = 0;
 
-    puts(simple);
-    json_read_object(simple, json_attrs, NULL);
-    printf("count = %d, flag1 = %d, flag2 = %d\n",
-          count, flag1, flag2);
+    status = json_read_object(argv[1], json_attrs, NULL);
+    printf("status = %d, count = %d, flag1 = %d, flag2 = %d\n",
+          status, count, flag1, flag2);
+    if (status != 0)
+       puts(json_error_string(status));
 }
+---------------------------------------------------------------------
+
+And here are some invocations:
+
+---------------------------------------------------------------------
+$ example1 '{"flag1":true,"flag2":false,"count":42}'
+status = 0, count = 42, flag1 = 1, flag2 = 0
+
+$ example1 '{"flag1":true,"flag2":false,"count":23}'
+status = 0, count = 23, flag1 = 1, flag2 = 0
+
+$ example1 '{"whozis":true,"flag2":false,"count":23}'
+status = 3, count = 0, flag1 = 0, flag2 = 0
+unknown attribute name
+
+$ example1 '{"flag1":true,"flag2":false,"count":23,"whozis":"whatsis"}'
+status = 3, count = 23, flag1 = 1, flag2 = 0
+unknown attribute name
 
-/* end */
 ---------------------------------------------------------------------
 
-The +json_read_object()+ call unpacks the values in the example JSON object into
-three static variables.  In many uses the target locations would
-instead be storage in some static structure instance.
+The +json_read_object()+ call unpacks the values in the argument JSON
+object into three static variables.  In many uses the target locations
+would instead be storage in some static structure instance.
 
 In this example, the +json_attrs+ structure array associates each
 possible member name with a type and a target address.  The function
 +json_read_object()+ treats this array of constants as parsing
 instructions.
 
+When an unexpected attribute name is encountered, the parser terminates,
+returning an error status.  Attributes and values parsed before the 
+error their storage.
+
 The parser recognizes a wider range of types than this, and the
 template structures can specify defaults when an expected JSON 
 attribute is omitted. Most of the rest is details.
 
 == Theory of Operation ==
 
+The parser is a simple state machine that walks the input looking
+for syntactically well-formed attribute-value pairs.  Each time it
+finds one, it looks up the name in the template structure array 
+driving the parse. The type tells it how to interpret the 
+value; the target address tells it where to put the value.
+
+Syntax errors, or any unknown attribute name, terminate the parse.
+
+
+
+
+
 The "shape" of a JSON object in the type signature of its
 attributes (and attribute values, and so on recursively down through
 all nestings of objects and arrays).  This parser is indifferent to
@@ -91,7 +123,7 @@ objects or strings - not reals or integers or floats - as elements
 (this limitation could be easily removed if required). Third, all
 elements of an array must be of the same type.
 
-There are separata entry points for beginning a parse of either JSON
+There are separate entry points for beginning a parse of either JSON
 object or a JSON array. JSON "float" quantities are actually stored as
 doubles.
 
index 8687cda2fceab192fe173c605ce544155edf058f..7d19ccae83d592645dec4c5eb8e5653cc2add483 100644 (file)
@@ -32,7 +32,7 @@ objects or strings - not reals or integers or floats - as elements
 (this limitation could be easily removed if required). Third, all
 elements of an array must be of the same type.
 
-   There are separata entry points for beginning a parse of either
+   There are separate entry points for beginning a parse of either
 JSON object or a JSON array. JSON "float" quantities are actually
 stored as doubles.