diff --git a/modules/juce_core/javascript/juce_JSON.cpp b/modules/juce_core/javascript/juce_JSON.cpp index 8aa819212a..ebf2e470f4 100644 --- a/modules/juce_core/javascript/juce_JSON.cpp +++ b/modules/juce_core/javascript/juce_JSON.cpp @@ -100,7 +100,6 @@ public: return Result::ok(); } -private: static Result parseAny (String::CharPointerType& t, var& result) { t = t.findEndOfWhitespace(); @@ -160,6 +159,7 @@ private: return createFail ("Syntax error", &t); } +private: static Result createFail (const char* const message, const String::CharPointerType* location = nullptr) { String m (message); @@ -460,51 +460,6 @@ public: out << ']'; } -/* static void writeObject (OutputStream& out, DynamicObject& object, - const int indentLevel, const bool allOnOneLine) - { - NamedValueSet& props = object.getProperties(); - - out << '{'; - if (! allOnOneLine) - out << newLine; - - LinkedListPointer* i = &(props.values); - - for (;;) - { - NamedValueSet::NamedValue* const v = i->get(); - - if (v == nullptr) - break; - - if (! allOnOneLine) - writeSpaces (out, indentLevel + indentSize); - - out << '"'; - writeString (out, v->name); - out << "\": "; - write (out, v->value, indentLevel + indentSize, allOnOneLine); - - if (v->nextListItem.get() != nullptr) - { - if (allOnOneLine) - out << ", "; - else - out << ',' << newLine; - } - else if (! allOnOneLine) - out << newLine; - - i = &(v->nextListItem); - } - - if (! allOnOneLine) - writeSpaces (out, indentLevel); - - out << '}'; - }*/ - enum { indentSize = 2 }; }; @@ -513,7 +468,17 @@ var JSON::parse (const String& text) { var result; - if (! JSONParser::parseObjectOrArray (text.getCharPointer(), result)) + if (! parse (text, result)) + result = var(); + + return result; +} + +var JSON::fromString (StringRef text) +{ + var result; + + if (! JSONParser::parseAny (text.text, result)) result = var(); return result; diff --git a/modules/juce_core/javascript/juce_JSON.h b/modules/juce_core/javascript/juce_JSON.h index c0dc5055bf..7fb17ee209 100644 --- a/modules/juce_core/javascript/juce_JSON.h +++ b/modules/juce_core/javascript/juce_JSON.h @@ -53,6 +53,10 @@ public: If you're not interested in the error message, you can use one of the other shortcut parse methods, which simply return a var::null if the parsing fails. + + Note that this will only parse valid JSON, which means that the item given must + be either an object or an array definition. If you want to also be able to parse + any kind of primitive JSON object, use the fromString() method. */ static Result parse (const String& text, var& parsedResult); @@ -60,6 +64,10 @@ public: If the parsing fails, this simply returns var::null - if you need to find out more detail about the parse error, use the alternative parse() method which returns a Result. + + Note that this will only parse valid JSON, which means that the item given must + be either an object or an array definition. If you want to also be able to parse + any kind of primitive JSON object, use the fromString() method. */ static var parse (const String& text); @@ -94,6 +102,13 @@ public: static String toString (const var& objectToFormat, bool allOnOneLine = false); + /** Parses a string that was created with the toString() method. + This is slightly different to the parse() methods because they will reject primitive + values and only accept array or object definitions, whereas this method will handle + either. + */ + static var fromString (StringRef); + /** Writes a JSON-formatted representation of the var object to the given stream. If allOnOneLine is true, the result will be compacted into a single line of text with no carriage-returns. If false, it will be laid-out in a more human-readable format. @@ -106,7 +121,7 @@ public: /** Returns a version of a string with any extended characters escaped. */ static String escapeString (StringRef); - /** Parses a quoted string in JSON format, returning the un-escaped result in the + /** Parses a quoted string-literal in JSON format, returning the un-escaped result in the result parameter, and an error message in case the content was illegal. This advances the text parameter, leaving it positioned after the closing quote. */