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 8, 2020
2 parents 24015ea + 19d04b5 commit aeed05d
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 32 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ Library provides generic JSON text parser.
## Features

* Written in ANSI C99, compatible with ``size_t`` for size data types
* RFC 4627 compliant
* RFC 4627 and RFC 8259 compliant
* Based on static token allocation with optional application dynamic pre-allocation
* No recursion during parse operation
* Re-entrant functions
* Zero-copy, no ``malloc`` or ``free`` functions used
* Advanced find algorithm for tokens
* Testscoverage is available
* Test coverage is available
* User friendly MIT license

## Contribute
Expand Down
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.1
* Version: v1.0.2
*/
#ifndef LWJSON_HDR_OPTS_H
#define LWJSON_HDR_OPTS_H
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.1'
version = 'v1.0.2'

# 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 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.1
* Version: v1.0.2
*/
#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.1
* Version: v1.0.2
*/
#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.1
* Version: v1.0.2
*/
#ifndef LWJSON_HDR_OPTS_H
#define LWJSON_HDR_OPTS_H
Expand Down
69 changes: 45 additions & 24 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.1
* Version: v1.0.2
*/
#include <string.h>
#include "lwjson/lwjson.h"
Expand Down Expand Up @@ -68,7 +68,7 @@ prv_skip_blank(const char** p) {
if (s != NULL && *s != '\0') {
return lwjsonOK;
}
return lwjsonERR;
return lwjsonERRJSON;
}

/**
Expand Down Expand Up @@ -116,8 +116,8 @@ prv_parse_string(const char** p, const char** pout, size_t* poutlen) {
++s;
for (size_t i = 0; i < 4; ++i, ++len) {
if (!((*s >= '0' && *s <= '9')
|| (*s >= 'a' && *s <= 'z')
|| (*s >= 'A' && *s <= 'Z'))) {
|| (*s >= 'a' && *s <= 'f')
|| (*s >= 'A' && *s <= 'F'))) {
return lwjsonERRJSON;
}
if (i < 3) {
Expand All @@ -134,9 +134,6 @@ prv_parse_string(const char** p, const char** pout, size_t* poutlen) {
}
}
*poutlen = len;
if ((res = prv_skip_blank(&s)) != lwjsonOK) {
return res;
}
*p = s;
return res;
}
Expand All @@ -153,15 +150,23 @@ prv_parse_property_name(const char** p, lwjson_token_t* t) {
const char* s = *p;
lwjsonr_t res;

if ((res = prv_parse_string(p, &t->token_name, &t->token_name_len)) != lwjsonOK) {
/* Parse property string first */
if ((res = prv_parse_string(&s, &t->token_name, &t->token_name_len)) != lwjsonOK) {
return res;
}
s = *p;
/* Skip any spaces */
if ((res = prv_skip_blank(&s)) != lwjsonOK) {
return res;
}
/* Must continue with colon */
if (*s != ':') {
return lwjsonERRJSON;
}
++s;
prv_skip_blank(&s);
/* Skip any spaces */
if ((res = prv_skip_blank(&s)) != lwjsonOK) {
return res;
}
*p = s;
return lwjsonOK;
}
Expand Down Expand Up @@ -344,6 +349,33 @@ prv_find(const lwjson_token_t* parent, const char* path) {
return NULL;
}

/**
* \brief Check for character after opening bracket of array or object
* \param[in,out] p: JSON string
* \param[in] t: Token to check for type
* \return \ref lwjsonOK on success, member of \ref lwjsonr_t otherwise
*/
static inline lwjsonr_t
prv_check_valid_char_after_open_bracket(const char **p, lwjson_token_t* t) {
lwjsonr_t res;
const char* s = *p;

/* Check next character after object open */
if ((res = prv_skip_blank(&s)) != lwjsonOK) {
return res;
}
if (*s == '\0'
|| (t->type == LWJSON_TYPE_OBJECT
&& (*s != '"' && *s != '}'))
|| (t->type == LWJSON_TYPE_ARRAY
&& (*s != '"' && *s != ']' && *s != '[' && *s != '{' && *s != '-'
&& (*s < '0' || *s > '9') && *s != 't' && *s != 'n' && *s != 'f'))) {
res = lwjsonERRJSON;
}
*p = s;
return res;
}

/**
* \brief Setup LwJSON instance for parsing JSON strings
* \param[in,out] lw: LwJSON instance
Expand Down Expand Up @@ -373,7 +405,6 @@ lwjson_parse(lwjson_t* lw, const char* json_str) {
lwjsonr_t res = lwjsonOK;
const char* p = json_str;
lwjson_token_t* t, *to = &lw->first_token;
uint8_t first_check = 1;

/* values from very beginning */
lw->flags.parsed = 0;
Expand All @@ -398,9 +429,8 @@ lwjson_parse(lwjson_t* lw, const char* json_str) {
goto ret;
}
++p;
if (*p == '\0') {
res = lwjsonERRJSON;
goto ret;
if ((res = prv_check_valid_char_after_open_bracket(&p, to)) != lwjsonOK) {
return res;
}

/* Process all characters */
Expand Down Expand Up @@ -463,18 +493,9 @@ lwjson_parse(lwjson_t* lw, const char* json_str) {
case '[':
t->type = *p == '{' ? LWJSON_TYPE_OBJECT : LWJSON_TYPE_ARRAY;
++p;
/* Check next character after object open */
if ((res = prv_skip_blank(&p)) != lwjsonOK) {
if ((res = prv_check_valid_char_after_open_bracket(&p, t)) != 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;
break;
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.1
* Version: v1.0.2
*/
#include <string.h>
#include <stdio.h>
Expand Down
11 changes: 11 additions & 0 deletions test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ test_run(void) {
/* Run JSON parse tests that must succeed */
test_parse(lwjsonOK, "{}");
test_parse(lwjsonOK, "{ }");
test_parse(lwjsonOK, "{}\r\n");
test_parse(lwjsonOK, "{ }\r\n");
test_parse(lwjsonOK, "{\t}\r\n");
test_parse(lwjsonOK, "{\t }\r\n");
test_parse(lwjsonOK, "[1,2,3,4]");
test_parse(lwjsonOK, "{\"k\":[]}");
test_parse(lwjsonOK, "{\"k\":[1]}");
Expand All @@ -179,6 +183,10 @@ test_run(void) {
test_parse(lwjsonOK, "{\"k\":false}");
test_parse(lwjsonOK, "{\"k\":true}");
test_parse(lwjsonOK, "{\"k\":null}");
test_parse(lwjsonOK, "{\"k\" :null}");
test_parse(lwjsonOK, "{\"k\" : null}");
test_parse(lwjsonOK, "{ \"k\": null }");
test_parse(lwjsonOK, "{ \"k\": null }");
test_parse(lwjsonOK, "{\"k\":\"Stringgg\"}");
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}");
Expand All @@ -192,6 +200,7 @@ test_run(void) {
test_parse(lwjsonERRJSON, "[\"abc\":\"test\"]");
test_parse(lwjsonERRJSON, "]");
test_parse(lwjsonERRJSON, "[[,[]]");
test_parse(lwjsonERRJSON, "[,[]]");
test_parse(lwjsonERRJSON, "[[],[,{}]");

/* Check specials */
Expand All @@ -204,6 +213,8 @@ test_run(void) {
test_parse(lwjsonOK, "{\"k\":\"\\u1234\"}");
test_parse(lwjsonOK, "{\"k\":\"\\uabcd\"}");
test_parse(lwjsonOK, "{\"k\":\"\\uAbCd\"}");
test_parse(lwjsonOK, "{\"k\":\"\\u1abc\"}");
test_parse(lwjsonERRJSON, "{\"k\":\"\\u1aGc\"}");
test_parse(lwjsonERRJSON, "{\"k\":\"\\u\t\n\n\n\"}");
test_parse(lwjsonERRJSON, "{\"k\":\"\\u\"}");
test_parse(lwjsonERRJSON, "{\"k\":\"\\u1\"}");
Expand Down

0 comments on commit aeed05d

Please sign in to comment.