From 9915229c03541128c9b715b4a274681026681d36 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 29 Aug 2020 20:00:36 +0100 Subject: Fix handling of empty object and object with nothing after a comma --- libjsonpp/json.ll | 8 +++++--- libjsonpp/testParse.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/libjsonpp/json.ll b/libjsonpp/json.ll index 12e46a9..71f509f 100644 --- a/libjsonpp/json.ll +++ b/libjsonpp/json.ll @@ -33,6 +33,7 @@ escape "\\" text [^\\\"]* %x OBJECT_ITEM +%x OBJECT_ITEM_OR_END %x OBJECT_NEXT %x ARRAY_ITEM %x ARRAY_NEXT @@ -63,13 +64,13 @@ text [^\\\"]* yy_pop_state(); } -{beginstr} { +{beginstr} { yy_push_state(STRING); } {beginobj} { BeginObject(); - BEGIN(OBJECT_ITEM); + BEGIN(OBJECT_ITEM_OR_END); } {beginarray} { @@ -87,6 +88,7 @@ text [^\\\"]* yy_pop_state(); break; case OBJECT_ITEM: + case OBJECT_ITEM_OR_END: PushKey(encodeBuf()); BEGIN(COLON); break; @@ -94,7 +96,7 @@ text [^\\\"]* buf.clear(); } -{endobj} { +{endobj} { EndObject(); yy_pop_state(); } diff --git a/libjsonpp/testParse.cpp b/libjsonpp/testParse.cpp index f9eaf92..714cad4 100644 --- a/libjsonpp/testParse.cpp +++ b/libjsonpp/testParse.cpp @@ -78,6 +78,54 @@ BOOST_AUTO_TEST_CASE(parse_null) std::get(json::parseValue(val)); } +BOOST_AUTO_TEST_CASE(parse_empty_object) +{ + const Glib::ustring val(" { } "); + BOOST_REQUIRE(std::get(json::parseValue(val)).empty()); +} + +BOOST_AUTO_TEST_CASE(parse_broken_object_lone_number) +{ + const Glib::ustring val(" { 1 } "); + BOOST_REQUIRE_THROW(json::parseValue(val), json::ParseError); +} + +BOOST_AUTO_TEST_CASE(parse_broken_object_lone_string) +{ + const Glib::ustring val(" { \"string\" } "); + BOOST_REQUIRE_THROW(json::parseValue(val), json::ParseError); +} + +BOOST_AUTO_TEST_CASE(parse_broken_object_missing_value) +{ + const Glib::ustring val(" { \"string\": } "); + BOOST_REQUIRE_THROW(json::parseValue(val), json::ParseError); +} + +BOOST_AUTO_TEST_CASE(parse_broken_object_missing_second_name) +{ + const Glib::ustring val(" { \"string\": 1, } "); + BOOST_REQUIRE_THROW(json::parseValue(val), json::ParseError); +} + +BOOST_AUTO_TEST_CASE(parse_broken_object_missing_second_colon) +{ + const Glib::ustring val(" { \"string\": 1, \"string2\" } "); + BOOST_REQUIRE_THROW(json::parseValue(val), json::ParseError); +} + +BOOST_AUTO_TEST_CASE(parse_broken_object_missing_second_value) +{ + const Glib::ustring val(" { \"string\": 1, \"string2\": } "); + BOOST_REQUIRE_THROW(json::parseValue(val), json::ParseError); +} + +BOOST_AUTO_TEST_CASE(parse_broken_object_lone_bool) +{ + const Glib::ustring val(" { true } "); + BOOST_REQUIRE_THROW(json::parseValue(val), json::ParseError); +} + BOOST_AUTO_TEST_CASE(parse_empty_array) { const Glib::ustring val(" [ ] "); -- cgit v1.2.3