Added support for integers and double parsing.
authorethereal <ethereal@ethv.net>
Mon, 27 Jan 2014 08:31:20 +0000 (01:31 -0700)
committerethereal <ethereal@ethv.net>
Mon, 27 Jan 2014 08:31:20 +0000 (01:31 -0700)
src/main.c
src/yacjs.c
test/test1.json [new file with mode: 0644]
test/test2.json [new file with mode: 0644]

index 5cf0aab..100e03b 100644 (file)
@@ -5,12 +5,16 @@
 int main() {
     struct yacjs_node *root = yacjs_parse("{\"foo\": \"bar\", \"baz\": [\"a\", \"b\", \"c\"]}");
 
-    printf("Result: %p\n", root);
-    printf("foo: %p\n", yacjs_node_dict_get(root, "foo"));
-    printf("foo string: %s\n", yacjs_node_str(yacjs_node_dict_get(root, "foo")));
+    FILE *fp = fopen("test/test1.json", "r");
+    char buffer[8192];
+    int ret = fread(buffer, 1, 8192, fp);
+    buffer[ret] = 0;
+    fclose(fp);
+
+    root = yacjs_parse(buffer);
+    printf("root: %p\n", root);
+
+    //printf("%f\n", yacjs_node_float(yacjs_node_array_elem(root, 2)));
 
-    struct yacjs_node *baz = yacjs_node_dict_get(root, "baz");
-    printf("baz: %p\n", baz);
-    printf("\tfirst element string: %s\n", yacjs_node_str(yacjs_node_array_elem(baz, 0)));
     return 0;
 }
index f99a38f..7697a2d 100644 (file)
@@ -1,5 +1,7 @@
+#include <stdbool.h>
 #include <stdio.h> // for debugging
 #include <stdlib.h>
+#include <ctype.h>
 
 #include "yacjs.h"
 #include "yacjs_dict.h"
@@ -23,6 +25,8 @@ struct yacjs_node {
 enum token_type {
     TOKEN_STRING,
     TOKEN_NUMBER,
+    TOKEN_FLOAT,
+    TOKEN_FPNUMBER,
     TOKEN_OPENDICT,
     TOKEN_CLOSEDICT,
     TOKEN_OPENARRAY,
@@ -144,7 +148,7 @@ struct yacjs_node *yacjs_node_dict_get(struct yacjs_node *node,
 }
 
 static void skip_whitespace(const char ** const ptr) {
-    while((**ptr == ' ' || **ptr == '\t') && **ptr != 0) {
+    while((**ptr == ' ' || **ptr == '\t' || **ptr == '\n') && **ptr != 0) {
         *ptr = yacjs_u8s_next(*ptr);
     }
 }
@@ -174,6 +178,21 @@ static const char *next_token(const char ** const ptr, int *length,
         *type = TOKEN_STRING;
         return start;
     }
+    else if(isdigit(**ptr)) {
+        // TODO: support floating-point etc.
+        const char *start = (*ptr);
+        *length = 0;
+        bool is_fp = false;
+        while((isdigit(**ptr) || **ptr == '.' || **ptr == 'e') && **ptr != 0) {
+            if(**ptr == '.' || **ptr == 'e') is_fp = true;
+            (*ptr) ++, (*length) ++;
+        }
+        if(*ptr == 0) return NULL;
+        if(is_fp) *type = TOKEN_FLOAT;
+        else *type = TOKEN_NUMBER;
+
+        return start;
+    }
     else if(**ptr == '{') {
         *type = TOKEN_OPENDICT;
         return (*ptr)++;
@@ -199,6 +218,8 @@ static const char *next_token(const char ** const ptr, int *length,
         return (*ptr)++;
     }
 
+    printf("Don't know what to do with a '%c'\n", **ptr);
+
     *type = TOKEN_ERROR;
 
     return NULL;
@@ -232,6 +253,18 @@ static struct yacjs_node *parse_any(const char **string) {
         build->data.number = strtoll(s, NULL, 0);
         return build;
     }
+    else if(type == TOKEN_FLOAT) {
+        struct yacjs_node *build = malloc(sizeof(*build));
+        build->type = YACJS_NODE_FLOAT;
+        char *e;
+        build->data.fp = strtod(s, &e);
+        if(e != *string) {
+            free(build);
+            // error parsing number, not everything was used
+            return NULL;
+        }
+        return build;
+    }
 
     printf("Unknown token type %i\n", type);
 
@@ -316,7 +349,8 @@ static struct yacjs_node *parse_array_contents(const char **string) {
             == result->data.array.entries_count) {
 
             void *nmem = realloc(result->data.array.entries,
-                result->data.array.entries_size * 2 + 1);
+                sizeof(struct yacjs_node)
+                    * (result->data.array.entries_size * 2 + 1));
             if(nmem == NULL) {
                 last_error = YACJS_ERROR_MEMORY;
                 return NULL;
diff --git a/test/test1.json b/test/test1.json
new file mode 100644 (file)
index 0000000..32b04ed
--- /dev/null
@@ -0,0 +1,254 @@
+[
+    {
+        "id": 0,
+        "guid": "9439da93-c91d-461c-bb19-9b4e187aa15c",
+        "isActive": true,
+        "balance": "$3,074.00",
+        "picture": "http://placehold.it/32x32",
+        "age": 30,
+        "name": "Moses Branch",
+        "gender": "male",
+        "company": "Gaptec",
+        "email": "mosesbranch@gaptec.com",
+        "phone": "+1 (894) 513-3977",
+        "address": "907 Linwood Street, Ilchester, Maryland, 5171",
+        "about": "Velit nisi non ad eu deserunt. Velit commodo duis enim aute ut ipsum nostrud elit nisi sunt exercitation. Excepteur officia ullamco aute fugiat fugiat sint dolore irure non. Deserunt tempor qui veniam fugiat non in dolore qui laboris deserunt proident labore cillum. Exercitation enim qui nostrud exercitation.\r\n",
+        "registered": "1989-06-15T21:59:17 +06:00",
+        "latitude": -5.545433,
+        "longitude": 81.457256,
+        "tags": [
+            "cillum",
+            "dolor",
+            "aliquip",
+            "commodo",
+            "enim",
+            "dolore",
+            "reprehenderit"
+        ],
+        "friends": [
+            {
+                "id": 0,
+                "name": "Weiss Blackwell"
+            },
+            {
+                "id": 1,
+                "name": "Cristina Booker"
+            },
+            {
+                "id": 2,
+                "name": "Prince Merrill"
+            }
+        ],
+        "randomArrayItem": "lemon"
+    },
+    {
+        "id": 1,
+        "guid": "2a427dbf-49b4-4815-b508-a942999ac0c0",
+        "isActive": true,
+        "balance": "$3,734.00",
+        "picture": "http://placehold.it/32x32",
+        "age": 32,
+        "name": "Taylor Nielsen",
+        "gender": "female",
+        "company": "Opportech",
+        "email": "taylornielsen@opportech.com",
+        "phone": "+1 (826) 406-2121",
+        "address": "404 Fairview Place, Crawfordsville, Nevada, 4342",
+        "about": "Consequat fugiat qui excepteur in elit enim. Dolore est consectetur ipsum anim duis. Eu irure ex culpa quis sint id qui sint in nisi est cupidatat ut. Esse aliqua ut culpa ut proident ex sit sunt nostrud commodo et. Magna non aute adipisicing dolor.\r\n",
+        "registered": "1992-11-13T18:23:54 +07:00",
+        "latitude": -16.373351,
+        "longitude": 88.319025,
+        "tags": [
+            "velit",
+            "nulla",
+            "sunt",
+            "eu",
+            "sunt",
+            "amet",
+            "non"
+        ],
+        "friends": [
+            {
+                "id": 0,
+                "name": "Lucille Beard"
+            },
+            {
+                "id": 1,
+                "name": "Juana Baird"
+            },
+            {
+                "id": 2,
+                "name": "Fulton Jennings"
+            }
+        ],
+        "randomArrayItem": "lemon"
+    },
+    {
+        "id": 2,
+        "guid": "28019898-5043-4715-ad1f-a20fecc0206d",
+        "isActive": false,
+        "balance": "$3,802.00",
+        "picture": "http://placehold.it/32x32",
+        "age": 37,
+        "name": "Jarvis Pugh",
+        "gender": "male",
+        "company": "Vortexaco",
+        "email": "jarvispugh@vortexaco.com",
+        "phone": "+1 (988) 599-3106",
+        "address": "135 Hull Street, Warsaw, Arkansas, 306",
+        "about": "In labore pariatur do sit ipsum magna ex magna laborum proident do deserunt sunt irure. Eiusmod proident adipisicing consequat elit ex consequat quis aute officia mollit eu consequat fugiat. Magna fugiat ullamco sit reprehenderit in id irure.\r\n",
+        "registered": "2002-02-09T18:15:18 +07:00",
+        "latitude": 72.13127,
+        "longitude": -109.31755,
+        "tags": [
+            "amet",
+            "labore",
+            "nostrud",
+            "eu",
+            "reprehenderit",
+            "id",
+            "aute"
+        ],
+        "friends": [
+            {
+                "id": 0,
+                "name": "Bryant Lara"
+            },
+            {
+                "id": 1,
+                "name": "Helen Valentine"
+            },
+            {
+                "id": 2,
+                "name": "Krystal Wagner"
+            }
+        ],
+        "randomArrayItem": "cherry"
+    },
+    {
+        "id": 3,
+        "guid": "34c03f4f-bcb9-4a3c-b8af-0adc5ae3baa3",
+        "isActive": true,
+        "balance": "$3,367.00",
+        "picture": "http://placehold.it/32x32",
+        "age": 40,
+        "name": "Wood Burt",
+        "gender": "male",
+        "company": "Zentia",
+        "email": "woodburt@zentia.com",
+        "phone": "+1 (968) 452-3004",
+        "address": "716 Raleigh Place, Ripley, Missouri, 4702",
+        "about": "Ea sint elit magna consectetur tempor occaecat minim est tempor culpa aliqua dolore duis. Labore aute laboris veniam occaecat mollit amet ullamco velit labore sunt aliquip nostrud officia consectetur. Nostrud ex aliqua sint ullamco do in proident. Eu eiusmod veniam ipsum exercitation nulla proident ex consectetur minim id pariatur sit. In ut consequat cupidatat consectetur consectetur mollit consectetur ut aliquip sit amet irure. Esse laboris eu occaecat cillum esse.\r\n",
+        "registered": "2001-03-22T20:48:01 +07:00",
+        "latitude": -35.081133,
+        "longitude": -36.077238,
+        "tags": [
+            "ut",
+            "fugiat",
+            "officia",
+            "enim",
+            "sint",
+            "esse",
+            "minim"
+        ],
+        "friends": [
+            {
+                "id": 0,
+                "name": "Delia Hamilton"
+            },
+            {
+                "id": 1,
+                "name": "Courtney Sanford"
+            },
+            {
+                "id": 2,
+                "name": "Brandy Boyle"
+            }
+        ],
+        "randomArrayItem": "lemon"
+    },
+    {
+        "id": 4,
+        "guid": "9ed933e9-0dec-43b2-a641-c68c09f48b7b",
+        "isActive": false,
+        "balance": "$1,039.00",
+        "picture": "http://placehold.it/32x32",
+        "age": 27,
+        "name": "Santos Sargent",
+        "gender": "male",
+        "company": "Zilphur",
+        "email": "santossargent@zilphur.com",
+        "phone": "+1 (904) 558-2248",
+        "address": "189 Coleridge Street, Stevens, South Dakota, 4912",
+        "about": "Reprehenderit ea duis veniam eu ullamco nulla enim nisi in elit sunt ex nisi commodo. Pariatur laborum excepteur excepteur consectetur occaecat ea quis id adipisicing magna. Dolore duis velit cupidatat dolor deserunt labore labore Lorem. Eiusmod cupidatat proident aliqua laboris.\r\n",
+        "registered": "1992-03-24T08:27:58 +07:00",
+        "latitude": 57.371592,
+        "longitude": 87.292071,
+        "tags": [
+            "adipisicing",
+            "ipsum",
+            "veniam",
+            "occaecat",
+            "nostrud",
+            "cillum",
+            "culpa"
+        ],
+        "friends": [
+            {
+                "id": 0,
+                "name": "Monique Alford"
+            },
+            {
+                "id": 1,
+                "name": "Andrea Mckee"
+            },
+            {
+                "id": 2,
+                "name": "Bernadette Kane"
+            }
+        ],
+        "randomArrayItem": "cherry"
+    },
+    {
+        "id": 5,
+        "guid": "a253bc99-dadc-4f25-a0e0-350641e744b2",
+        "isActive": false,
+        "balance": "$3,147.00",
+        "picture": "http://placehold.it/32x32",
+        "age": 32,
+        "name": "Parrish Daniel",
+        "gender": "male",
+        "company": "Pyramax",
+        "email": "parrishdaniel@pyramax.com",
+        "phone": "+1 (802) 465-2535",
+        "address": "426 Nelson Street, Cliff, Florida, 919",
+        "about": "Eu elit nisi reprehenderit nostrud adipisicing laborum consequat dolor ullamco qui pariatur dolore laborum. Consequat aliqua est ad exercitation enim. Tempor anim ex consequat laboris. Officia labore excepteur quis quis. Minim velit ad do aute culpa reprehenderit adipisicing nisi sit qui incididunt non ut. Deserunt irure voluptate cupidatat voluptate sunt laborum laborum magna.\r\n",
+        "registered": "1989-11-21T07:35:52 +07:00",
+        "latitude": -1.823997,
+        "longitude": -73.392845,
+        "tags": [
+            "elit",
+            "ad",
+            "amet",
+            "aliqua",
+            "nisi",
+            "non",
+            "excepteur"
+        ],
+        "friends": [
+            {
+                "id": 0,
+                "name": "Fox West"
+            },
+            {
+                "id": 1,
+                "name": "Torres Chambers"
+            },
+            {
+                "id": 2,
+                "name": "Rios Mcclure"
+            }
+        ],
+        "randomArrayItem": "cherry"
+    }
+]
diff --git a/test/test2.json b/test/test2.json
new file mode 100644 (file)
index 0000000..7f918be
--- /dev/null
@@ -0,0 +1,8 @@
+[
+    32,
+    64,
+    128.64,
+    {
+        "test": 128
+    }
+]