summaryrefslogtreecommitdiffstats
path: root/src/liblaxjson-test.c
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2015-02-10 06:58:33 (GMT)
committerAndrew Kelley <superjoe30@gmail.com>2015-02-10 09:01:19 (GMT)
commitf1424bf3264203c80b0af4a520deb82a760977df (patch)
tree8a7a40acc62698f76743e3c8559555e1e9ae8071 /src/liblaxjson-test.c
parentd9eb88bab46f8c1ecde201664075843b7f403e7c (diff)
downloadmxe-f1424bf3264203c80b0af4a520deb82a760977df.zip
mxe-f1424bf3264203c80b0af4a520deb82a760977df.tar.gz
mxe-f1424bf3264203c80b0af4a520deb82a760977df.tar.bz2
add liblaxjson package
Diffstat (limited to 'src/liblaxjson-test.c')
-rw-r--r--src/liblaxjson-test.c87
1 files changed, 87 insertions, 0 deletions
diff --git a/src/liblaxjson-test.c b/src/liblaxjson-test.c
new file mode 100644
index 0000000..dae2b9d
--- /dev/null
+++ b/src/liblaxjson-test.c
@@ -0,0 +1,87 @@
+/*
+ * This file is part of MXE.
+ * See index.html for further information.
+ */
+
+/*
+ * Copyright (c) 2013 Andrew Kelley
+ *
+ * This file is part of liblaxjson, which is MIT licensed.
+ * See http://opensource.org/licenses/MIT
+ */
+
+#include <laxjson.h>
+#include <stdio.h>
+
+static int on_string(struct LaxJsonContext *context,
+ enum LaxJsonType type, const char *value, int length)
+{
+ (void)context;
+ (void)length;
+ char *type_name = (type == LaxJsonTypeProperty) ? "property" : "string";
+ printf("%s: %s\n", type_name, value);
+ return 0;
+}
+
+static int on_number(struct LaxJsonContext *context, double x)
+{
+ (void)context;
+ printf("number: %f\n", x);
+ return 0;
+}
+
+static int on_primitive(struct LaxJsonContext *context, enum LaxJsonType type)
+{
+ (void)context;
+ char *type_name;
+ if (type == LaxJsonTypeTrue)
+ type_name = "true";
+ else if (type == LaxJsonTypeFalse)
+ type_name = "false";
+ else
+ type_name = "null";
+
+ printf("primitive: %s\n", type_name);
+ return 0;
+}
+
+static int on_begin(struct LaxJsonContext *context, enum LaxJsonType type)
+{
+ (void)context;
+ char *type_name = (type == LaxJsonTypeArray) ? "array" : "object";
+ printf("begin %s\n", type_name);
+ return 0;
+}
+
+static int on_end(struct LaxJsonContext *context, enum LaxJsonType type)
+{
+ (void)context;
+ char *type_name = (type == LaxJsonTypeArray) ? "array" : "object";
+ printf("end %s\n", type_name);
+ return 0;
+}
+
+int main() {
+ char buf[1024];
+ struct LaxJsonContext *context;
+ FILE *f;
+ int amt_read;
+
+ context = lax_json_create();
+
+ context->userdata = NULL; /* can set this to whatever you want */
+ context->string = on_string;
+ context->number = on_number;
+ context->primitive = on_primitive;
+ context->begin = on_begin;
+ context->end = on_end;
+
+ f = fopen("file.json", "rb");
+ while ((amt_read = fread(buf, 1, sizeof(buf), f))) {
+ lax_json_feed(context, amt_read, buf);
+ }
+ lax_json_destroy(context);
+
+ return 0;
+}
+