From c45134d1f2c9c44defbc2c8df4e6c82c6eeec817 Mon Sep 17 00:00:00 2001 From: cxl Date: Mon, 7 Nov 2011 12:56:38 +0000 Subject: [PATCH] Core: ParseJSON(const char *) now returns ErrorValue if input is invalid git-svn-id: svn://ultimatepp.org/upp/trunk@4144 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- uppsrc/Core/JSON.cpp | 9 +++++++-- uppsrc/Core/src.tpp/JSON$en-us.tpp | 7 ++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/uppsrc/Core/JSON.cpp b/uppsrc/Core/JSON.cpp index 93b5a2485..bb2258f6b 100644 --- a/uppsrc/Core/JSON.cpp +++ b/uppsrc/Core/JSON.cpp @@ -41,8 +41,13 @@ Value ParseJSON(CParser& p) Value ParseJSON(const char *s) { - CParser p(s); - return ParseJSON(p); + try { + CParser p(s); + return ParseJSON(p); + } + catch(CParser::Error) { + return ErrorValue(); + } } String AsJSON(const Value& v, const String& sep, bool pretty) diff --git a/uppsrc/Core/src.tpp/JSON$en-us.tpp b/uppsrc/Core/src.tpp/JSON$en-us.tpp index 4a0b4159c..29e0a15b8 100644 --- a/uppsrc/Core/src.tpp/JSON$en-us.tpp +++ b/uppsrc/Core/src.tpp/JSON$en-us.tpp @@ -16,16 +16,17 @@ topic "JSON support"; ]_[*@3 p])&] [s2;%% Parses JSON represented from [%-*@3 p]. It is possible to parse only part of whole text (e.g. when to parse just single element -of array `- parser the stops at the end of element. Elements +of array `- parser then stops at the end of element. Elements of JSON are parsed into corresponding Value types, JSON objects -are represented by ValueMap, JSON arrays by ValueArray.&] +are represented by ValueMap, JSON arrays by ValueArray. If input +JSON is invalid throws CParser`::Error.&] [s3;%% &] [s4; &] [s5;:ParseJSON`(const char`*`): [_^Value^ Value]_[* ParseJSON]([@(0.0.255) const]_[@(0.0.255) c har]_`*[*@3 s])&] [s2;%% Parses JSON text [%-*@3 s]. Elements of JSON are parsed into corresponding Value types, JSON objects are represented by ValueMap, -JSON arrays by ValueArray.&] +JSON arrays by ValueArray. If input JSON is invalid returns ErrorValue.&] [s3;%% &] [s4; &] [s5;:AsJSON`(int`): [_^String^ String]_[* AsJSON]([@(0.0.255) int]_[*@3 i])&]