#include "microjson.h"
-static const char *simple = "{\"flag1\":true,\"flag2\":false,\"count\":42}";
-
static bool flag1, flag2;
static int count;
{
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 */
#include "microjson.h"
-static const char *simple = "{\"flag1\":true,\"flag2\":false,\"count\":42}";
-
static bool flag1, flag2;
static int count;
{
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
(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.