From: Eric S. Raymond Date: Wed, 17 Sep 2014 19:44:59 +0000 (-0400) Subject: Improvements to the first worked example, and a typo fix. X-Git-Tag: 1.0~26 X-Git-Url: http://thyrsus.com/gitweb/?a=commitdiff_plain;h=11b72a9bae5915ab5dfec0739213512b1aa5a592;p=microjson.git Improvements to the first worked example, and a typo fix. --- diff --git a/example1.c b/example1.c index 4dbd945..96aa855 100644 --- a/example1.c +++ b/example1.c @@ -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 */ diff --git a/microjson.asc b/microjson.asc index 89913f4..1ba5d9e 100644 --- a/microjson.asc +++ b/microjson.asc @@ -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. diff --git a/microjson.c b/microjson.c index 8687cda..7d19cca 100644 --- a/microjson.c +++ b/microjson.c @@ -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.