Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
MaJerle committed Dec 7, 2020
2 parents 4e70a6f + 6c8a6ba commit 24015ea
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 41 deletions.
2 changes: 1 addition & 1 deletion dev/VisualStudio/lwjson_opts.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* This file is part of LwJSON - Lightweight JSON format parser.
*
* Author: Tilen MAJERLE <tilen@majerle.eu>
* Version: v1.0.0
* Version: v1.0.1
*/
#ifndef LWJSON_HDR_OPTS_H
#define LWJSON_HDR_OPTS_H
Expand Down
4 changes: 2 additions & 2 deletions dev/VisualStudio/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ main() {
char* json_text = NULL;
const lwjson_token_t* tkn;

//test_run();
test_run();
//example_minimal_run();
example_traverse_run();
//example_traverse_run();
return 0;

printf("\n---\n");
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
author = 'Tilen MAJERLE'

# The full version, including alpha/beta/rc tags
version = 'v1.0.0'
version = 'v1.0.1'

# Try to get branch at which this is running
# and try to determine which version to display in sphinx
Expand Down
2 changes: 1 addition & 1 deletion examples/example_traverse.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ example_traverse_run(void) {
}

/* Now print all keys in the object */
for (lwjson_token_t* tkn = lwjson_get_first_child(t); tkn != NULL; tkn = tkn->next) {
for (const lwjson_token_t* tkn = lwjson_get_first_child(t); tkn != NULL; tkn = tkn->next) {
printf("Token: %.*s", (int)tkn->token_name_len, tkn->token_name);
if (tkn->type == LWJSON_TYPE_ARRAY || tkn->type == LWJSON_TYPE_OBJECT) {
printf(": Token is array or object...check children tokens if any, in recursive mode..");
Expand Down
2 changes: 1 addition & 1 deletion lwjson/src/include/lwjson/lwjson.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* This file is part of LwJSON - Lightweight JSON format parser.
*
* Author: Tilen MAJERLE <tilen@majerle.eu>
* Version: v1.0.0
* Version: v1.0.1
*/
#ifndef LWJSON_HDR_H
#define LWJSON_HDR_H
Expand Down
2 changes: 1 addition & 1 deletion lwjson/src/include/lwjson/lwjson_opt.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* This file is part of LwJSON - Lightweight JSON format parser.
*
* Author: Tilen MAJERLE <tilen@majerle.eu>
* Version: v1.0.0
* Version: v1.0.1
*/
#ifndef LWJSON_HDR_OPT_H
#define LWJSON_HDR_OPT_H
Expand Down
2 changes: 1 addition & 1 deletion lwjson/src/include/lwjson/lwjson_opts_template.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* This file is part of LwJSON - Lightweight JSON format parser.
*
* Author: Tilen MAJERLE <tilen@majerle.eu>
* Version: v1.0.0
* Version: v1.0.1
*/
#ifndef LWJSON_HDR_OPTS_H
#define LWJSON_HDR_OPTS_H
Expand Down
108 changes: 78 additions & 30 deletions lwjson/src/lwjson/lwjson.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* This file is part of LwJSON - Lightweight JSON format parser.
*
* Author: Tilen MAJERLE <tilen@majerle.eu>
* Version: v1.0.0
* Version: v1.0.1
*/
#include <string.h>
#include "lwjson/lwjson.h"
Expand All @@ -55,16 +55,17 @@ prv_alloc_token(lwjson_t* lw) {
*/
static lwjsonr_t
prv_skip_blank(const char** p) {
lwjsonr_t res = lwjsonOK;
const char* s = *p;
while (s != NULL && *s != '\0') {
if (*s == ' ' || *s == '\t' || *s == '\r' || *s == '\n' || *s == '\f') {
++s;
continue;
}
*p = s;
return lwjsonOK;
break;
}
if (s != NULL && *s == '\0') {
*p = s;
if (s != NULL && *s != '\0') {
return lwjsonOK;
}
return lwjsonERR;
Expand Down Expand Up @@ -93,23 +94,46 @@ prv_parse_string(const char** p, const char** pout, size_t* poutlen) {
}
*pout = s;
/* Parse string but take care of escape characters */
for (char prev_ch = '\0';; ++s, ++len) {
for (;; ++s, ++len) {
if (s == NULL || *s == '\0') {
return lwjsonERRJSON;
}
/* Check end of string */
if (*s == '"') {
if (prev_ch != '\\') {
++s;
break;
/* Check special characters */
if (*s == '\\') {
++s;
++len;
switch (*s) {
case '"':
case '\\':
case '/':
case 'b':
case 'f':
case 'n':
case 'r':
case 't':
break;
case 'u':
++s;
for (size_t i = 0; i < 4; ++i, ++len) {
if (!((*s >= '0' && *s <= '9')
|| (*s >= 'a' && *s <= 'z')
|| (*s >= 'A' && *s <= 'Z'))) {
return lwjsonERRJSON;
}
if (i < 3) {
++s;
}
}
break;
default:
return lwjsonERRJSON;
}
} else if (*s == '"') {
++s;
break;
}
prev_ch = *s;
}
*poutlen = len;
if (*s == '"') {
++s;
}
if ((res = prv_skip_blank(&s)) != lwjsonOK) {
return res;
}
Expand Down Expand Up @@ -214,7 +238,7 @@ prv_parse_number(const char** p, lwjson_type_t* tout, lwjson_real_t* fout, lwjso
for (; exp_cnt > 0; num *= 10, --exp_cnt) {}
}
}
if (is_minus) {
if (is_minus) {
num = -num;
}
*p = s;
Expand All @@ -228,7 +252,7 @@ prv_parse_number(const char** p, lwjson_type_t* tout, lwjson_real_t* fout, lwjso
} else {
*fout = num;
}
return lwjsonOK;
return lwjsonOK;
}

/**
Expand Down Expand Up @@ -361,25 +385,30 @@ lwjson_parse(lwjson_t* lw, const char* json_str) {
return lwjsonERRJSON;
}

/* First parse */
if ((res = prv_skip_blank(&p)) != lwjsonOK) {
goto ret;
}
if (*p == '{') {
to->type = LWJSON_TYPE_OBJECT;
} else if (*p == '[') {
to->type = LWJSON_TYPE_ARRAY;
} else {
res = lwjsonERRJSON;
goto ret;
}
++p;
if (*p == '\0') {
res = lwjsonERRJSON;
goto ret;
}

/* Process all characters */
while (p != NULL && *p != '\0') {
/* Filter out blanks */
if ((res = prv_skip_blank(&p)) != lwjsonOK) {
goto ret;
}
if (first_check) {
first_check = 0;
if (*p == '{') {
to->type = LWJSON_TYPE_OBJECT;
} else if (*p == '[') {
to->type = LWJSON_TYPE_ARRAY;
} else {
res = lwjsonERRMEM;
goto ret;
}
++p;
continue;
}
if (*p == ',') {
++p;
continue;
Expand Down Expand Up @@ -433,9 +462,21 @@ lwjson_parse(lwjson_t* lw, const char* json_str) {
case '{':
case '[':
t->type = *p == '{' ? LWJSON_TYPE_OBJECT : LWJSON_TYPE_ARRAY;
++p;
/* Check next character after object open */
if ((res = prv_skip_blank(&p)) != lwjsonOK) {
return res;
}
if (*p == '\0'
|| (t->type == LWJSON_TYPE_OBJECT
&& (*p != '\0' && *p != '"' && *p != '}')
|| (t->type == LWJSON_TYPE_ARRAY
&& (*p != '"' && *p != ']' && *p != '{' && *p != '-' && *p < '0' && *p > '9' && *p != 't' && *p != 'n' && *p != 'f')))) {
res = lwjsonERRJSON;
goto ret;
}
t->next = to; /* Temporary saved as parent object */
to = t;
++p;
break;
case '"':
if ((res = prv_parse_string(&p, &t->u.str.token_value, &t->u.str.token_value_len)) == lwjsonOK) {
Expand Down Expand Up @@ -512,7 +553,14 @@ lwjson_parse(lwjson_t* lw, const char* json_str) {
++p;
}
}
if (to != &lw->first_token || (to != NULL && to->next != NULL)) {
res = lwjsonERRJSON;
to = NULL;
}
if (to != NULL) {
if (to->type != LWJSON_TYPE_ARRAY || to->type != LWJSON_TYPE_OBJECT) {
res = lwjsonERRJSON;
}
to->token_name = NULL;
to->token_name_len = 0;
}
Expand Down
2 changes: 1 addition & 1 deletion lwjson/src/lwjson/lwjson_debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* This file is part of LwJSON - Lightweight JSON format parser.
*
* Author: Tilen MAJERLE <tilen@majerle.eu>
* Version: v1.0.0
* Version: v1.0.1
*/
#include <string.h>
#include <stdio.h>
Expand Down
34 changes: 32 additions & 2 deletions test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ test_parse(lwjsonr_t exp_result, const char* json_str) {
if (lwjson_parse(&lwjson, json_str) == exp_result) {
printf("Parse test passed..\r\n");
} else {
printf("Parse test passed failed..\r\n");
printf("Parse test passed failed.. %s\r\n", json_str);
}
}

Expand Down Expand Up @@ -169,6 +169,8 @@ test_run(void) {

/* Run JSON parse tests that must succeed */
test_parse(lwjsonOK, "{}");
test_parse(lwjsonOK, "{ }");
test_parse(lwjsonOK, "[1,2,3,4]");
test_parse(lwjsonOK, "{\"k\":[]}");
test_parse(lwjsonOK, "{\"k\":[1]}");
test_parse(lwjsonOK, "{\"k\":[1,2]}");
Expand All @@ -181,6 +183,34 @@ test_run(void) {
test_parse(lwjsonOK, "{\"k\":\"Stri\\\"nggg with quote inside\"}");
test_parse(lwjsonOK, "{\"k\":{\"b\":1E5,\t\r\n\"c\":1.3E5\r\n}\r\n}");

/* Arrays */
test_parse(lwjsonOK, "[]");
test_parse(lwjsonOK, "[ ]");
test_parse(lwjsonOK, "[[],[]]");
test_parse(lwjsonOK, "[[],[],{}]");
test_parse(lwjsonERRJSON, "[");
test_parse(lwjsonERRJSON, "[\"abc\":\"test\"]");
test_parse(lwjsonERRJSON, "]");
test_parse(lwjsonERRJSON, "[[,[]]");
test_parse(lwjsonERRJSON, "[[],[,{}]");

/* Check specials */
test_parse(lwjsonOK, "{\"k\":\"\\t\"}");
test_parse(lwjsonOK, "{\"k\":\"\\b\"}");
test_parse(lwjsonOK, "{\"k\":\"\\r\"}");
test_parse(lwjsonOK, "{\"k\":\"\\n\"}");
test_parse(lwjsonOK, "{\"k\":\"\\f\"}");
test_parse(lwjsonOK, "{\"k\":\"\\\\\"}");
test_parse(lwjsonOK, "{\"k\":\"\\u1234\"}");
test_parse(lwjsonOK, "{\"k\":\"\\uabcd\"}");
test_parse(lwjsonOK, "{\"k\":\"\\uAbCd\"}");
test_parse(lwjsonERRJSON, "{\"k\":\"\\u\t\n\n\n\"}");
test_parse(lwjsonERRJSON, "{\"k\":\"\\u\"}");
test_parse(lwjsonERRJSON, "{\"k\":\"\\u1\"}");
test_parse(lwjsonERRJSON, "{\"k\":\"\\u12\"}");
test_parse(lwjsonERRJSON, "{\"k\":\"\\u123\"}");
test_parse(lwjsonERRJSON, "{\"k\":\"\\a\"}");

/* Run JSON tests to fail */
test_parse(lwjsonERRJSON, "");
test_parse(lwjsonERRJSON, "{[]}"); /* Array without key inside object */
Expand All @@ -205,4 +235,4 @@ test_run(void) {

/* Parse input text and compare against expected data types */
test_json_data_types();
}
}

0 comments on commit 24015ea

Please sign in to comment.