aboutsummaryrefslogtreecommitdiffstats
path: root/json/examples/test_json.c
diff options
context:
space:
mode:
Diffstat (limited to 'json/examples/test_json.c')
-rwxr-xr-xjson/examples/test_json.c133
1 files changed, 133 insertions, 0 deletions
diff --git a/json/examples/test_json.c b/json/examples/test_json.c
new file mode 100755
index 0000000..0545c7c
--- /dev/null
+++ b/json/examples/test_json.c
@@ -0,0 +1,133 @@
+/* vim: set et ts=4
+ *
+ * Copyright (C) 2015 Mirko Pasqualetti All rights reserved.
+ * https://github.com/udp/json-parser
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+
+#include "json.h"
+
+static void process_value(json_value* value)
+{
+ if (value == NULL) {
+ return;
+ }
+ if (value->type != json_object) {
+ print_depth_shift(depth);
+ }
+ switch (value->type) {
+ case json_none:
+ printf("none\n");
+ break;
+ case json_null:
+ printf("null\n");
+ break;
+ case json_object:
+ process_object(value, depth+1);
+ break;
+ case json_array:
+ process_array(value, depth+1);
+ break;
+ case json_integer:
+ printf("int: %10" PRId64 "\n", value->u.integer);
+ break;
+ case json_double:
+ printf("double: %f\n", value->u.dbl);
+ break;
+ case json_string:
+ printf("string: %s\n", value->u.string.ptr);
+ break;
+ case json_boolean:
+ printf("bool: %d\n", value->u.boolean);
+ break;
+ }
+}
+
+int main(int argc, char** argv)
+{
+ char* filename;
+ FILE *fp;
+ struct stat filestatus;
+ int file_size;
+ char* file_contents;
+ json_char* json;
+ json_value* value;
+
+ if (argc != 2) {
+ fprintf(stderr, "%s <file_json>\n", argv[0]);
+ return 1;
+ }
+ filename = argv[1];
+
+ if ( stat(filename, &filestatus) != 0) {
+ fprintf(stderr, "File %s not found\n", filename);
+ return 1;
+ }
+ file_size = filestatus.st_size;
+ file_contents = (char*)malloc(filestatus.st_size);
+ if ( file_contents == NULL) {
+ fprintf(stderr, "Memory error: unable to allocate %d bytes\n", file_size);
+ return 1;
+ }
+
+ fp = fopen(filename, "rt");
+ if (fp == NULL) {
+ fprintf(stderr, "Unable to open %s\n", filename);
+ fclose(fp);
+ free(file_contents);
+ return 1;
+ }
+ if ( fread(file_contents, file_size, 1, fp) != 1 ) {
+ fprintf(stderr, "Unable to read content of %s\n", filename);
+ fclose(fp);
+ free(file_contents);
+ return 1;
+ }
+ fclose(fp);
+
+ printf("%s\n", file_contents);
+
+ printf("--------------------------------\n\n");
+
+ json = (json_char*)file_contents;
+
+ value = json_parse(json,file_size);
+
+ if (value == NULL) {
+ fprintf(stderr, "Unable to parse data\n");
+ free(file_contents);
+ exit(1);
+ }
+
+ process_value(value);
+
+ json_value_free(value);
+ free(file_contents);
+ return 0;
+}