Browse Source

Added a JSON::fromString method.

tags/2021-05-28
jules 11 years ago
parent
commit
0530b6b01a
2 changed files with 28 additions and 48 deletions
  1. +12
    -47
      modules/juce_core/javascript/juce_JSON.cpp
  2. +16
    -1
      modules/juce_core/javascript/juce_JSON.h

+ 12
- 47
modules/juce_core/javascript/juce_JSON.cpp View File

@@ -100,7 +100,6 @@ public:
return Result::ok(); return Result::ok();
} }
private:
static Result parseAny (String::CharPointerType& t, var& result) static Result parseAny (String::CharPointerType& t, var& result)
{ {
t = t.findEndOfWhitespace(); t = t.findEndOfWhitespace();
@@ -160,6 +159,7 @@ private:
return createFail ("Syntax error", &t); return createFail ("Syntax error", &t);
} }
private:
static Result createFail (const char* const message, const String::CharPointerType* location = nullptr) static Result createFail (const char* const message, const String::CharPointerType* location = nullptr)
{ {
String m (message); String m (message);
@@ -460,51 +460,6 @@ public:
out << ']'; out << ']';
} }
/* static void writeObject (OutputStream& out, DynamicObject& object,
const int indentLevel, const bool allOnOneLine)
{
NamedValueSet& props = object.getProperties();
out << '{';
if (! allOnOneLine)
out << newLine;
LinkedListPointer<NamedValueSet::NamedValue>* 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 }; enum { indentSize = 2 };
}; };
@@ -513,7 +468,17 @@ var JSON::parse (const String& text)
{ {
var result; 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(); result = var();
return result; return result;


+ 16
- 1
modules/juce_core/javascript/juce_JSON.h View File

@@ -53,6 +53,10 @@ public:
If you're not interested in the error message, you can use one of the other 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. 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); 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 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. 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); static var parse (const String& text);
@@ -94,6 +102,13 @@ public:
static String toString (const var& objectToFormat, static String toString (const var& objectToFormat,
bool allOnOneLine = false); 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. /** 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 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. 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. */ /** Returns a version of a string with any extended characters escaped. */
static String escapeString (StringRef); 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. result parameter, and an error message in case the content was illegal.
This advances the text parameter, leaving it positioned after the closing quote. This advances the text parameter, leaving it positioned after the closing quote.
*/ */


Loading…
Cancel
Save