From a34e72528224fe2a97fa89d8115fedc870df6fd3 Mon Sep 17 00:00:00 2001 From: Krzysztof Gabis Date: Sun, 6 Mar 2022 21:27:20 +0100 Subject: [PATCH] 1.4.0: Accepting trailing commas in objects and arrays Issue #178 --- CMakeLists.txt | 2 +- package.json | 2 +- parson.c | 12 +++++++++--- parson.h | 8 ++++---- tests.c | 6 ++++-- tests/test_2.txt | 2 +- 6 files changed, 20 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4e1cf6d..5d188fb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ project(parson C) include (GNUInstallDirs) -set(PARSON_VERSION 1.3.1) +set(PARSON_VERSION 1.4.0) add_library(parson parson.c) target_include_directories(parson PUBLIC $) diff --git a/package.json b/package.json index 62fd31e..4a6879f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "parson", - "version": "1.3.1", + "version": "1.4.0", "repo": "kgabis/parson", "description": "Small json parser and reader", "keywords": [ "json", "parser" ], diff --git a/parson.c b/parson.c index 5a78118..6f7e94f 100644 --- a/parson.c +++ b/parson.c @@ -1,7 +1,7 @@ /* SPDX-License-Identifier: MIT - Parson 1.3.1 (https://github.com/kgabis/parson) + Parson 1.4.0 (https://github.com/kgabis/parson) Copyright (c) 2012 - 2022 Krzysztof Gabis Permission is hereby granted, free of charge, to any person obtaining a copy @@ -31,8 +31,8 @@ #include "parson.h" #define PARSON_IMPL_VERSION_MAJOR 1 -#define PARSON_IMPL_VERSION_MINOR 3 -#define PARSON_IMPL_VERSION_PATCH 1 +#define PARSON_IMPL_VERSION_MINOR 4 +#define PARSON_IMPL_VERSION_PATCH 0 #if (PARSON_VERSION_MAJOR != PARSON_IMPL_VERSION_MAJOR)\ || (PARSON_VERSION_MINOR != PARSON_IMPL_VERSION_MINOR)\ @@ -980,6 +980,9 @@ static JSON_Value * parse_object_value(const char **string, size_t nesting) { } SKIP_CHAR(string); SKIP_WHITESPACES(string); + if (**string == '}') { + break; + } } SKIP_WHITESPACES(string); if (**string != '}') { @@ -1025,6 +1028,9 @@ static JSON_Value * parse_array_value(const char **string, size_t nesting) { } SKIP_CHAR(string); SKIP_WHITESPACES(string); + if (**string == ']') { + break; + } } SKIP_WHITESPACES(string); if (**string != ']' || /* Trim array after parsing is over */ diff --git a/parson.h b/parson.h index bba4659..fa431f9 100644 --- a/parson.h +++ b/parson.h @@ -1,7 +1,7 @@ /* SPDX-License-Identifier: MIT - Parson 1.3.1 (https://github.com/kgabis/parson) + Parson 1.4.0 (https://github.com/kgabis/parson) Copyright (c) 2012 - 2022 Krzysztof Gabis Permission is hereby granted, free of charge, to any person obtaining a copy @@ -35,10 +35,10 @@ extern "C" #endif #define PARSON_VERSION_MAJOR 1 -#define PARSON_VERSION_MINOR 3 -#define PARSON_VERSION_PATCH 1 +#define PARSON_VERSION_MINOR 4 +#define PARSON_VERSION_PATCH 0 -#define PARSON_VERSION_STRING "1.3.1" +#define PARSON_VERSION_STRING "1.4.0" #include /* size_t */ diff --git a/tests.c b/tests.c index 1e5083d..895986a 100644 --- a/tests.c +++ b/tests.c @@ -307,6 +307,8 @@ void test_suite_3(void) { TEST(json_parse_string("false") != NULL); TEST(json_parse_string("\"string\"") != NULL); TEST(json_parse_string("123") != NULL); + TEST(json_parse_string("[\"lorem\",]") != NULL); + TEST(json_parse_string("{\"lorem\":\"ipsum\",}") != NULL); /* Test UTF-16 parsing */ TEST(STREQ(json_string(json_parse_string("\"\\u0024x\"")), "$x")); @@ -318,9 +320,9 @@ void test_suite_3(void) { g_malloc_count = 0; TEST(json_parse_string(NULL) == NULL); TEST(json_parse_string("") == NULL); /* empty string */ - TEST(json_parse_string("[\"lorem\",]") == NULL); - TEST(json_parse_string("{\"lorem\":\"ipsum\",}") == NULL); TEST(json_parse_string("{lorem:ipsum}") == NULL); + TEST(json_parse_string("{\"lorem\":\"ipsum\",]") == NULL); + TEST(json_parse_string("{\"lorem\":\"ipsum\",,}") == NULL); TEST(json_parse_string("[,]") == NULL); TEST(json_parse_string("[,") == NULL); TEST(json_parse_string("[") == NULL); diff --git a/tests/test_2.txt b/tests/test_2.txt index 9231027..c59ff44 100644 --- a/tests/test_2.txt +++ b/tests/test_2.txt @@ -31,5 +31,5 @@ "url" : "https:\/\/www.example.com\/search?q=12345", "escaped chars" : "\" \\ \/", "empty object" : {}, - "empty array" : [] + "empty array" : [], }