49 if (*
i ==
'\n') {
e.column = 1;
e.line++; }
106 case 'a':
c =
'\a';
break;
107 case 'b':
c =
'\b';
break;
108 case 'f':
c =
'\f';
break;
109 case 'n':
c =
'\n';
break;
110 case 'r':
c =
'\r';
break;
111 case 't':
c =
'\t';
break;
117 for (
int i = 4; --
i >= 0;)
122 throwError (
"Syntax error in unicode escape sequence", errorLocation);
137 buffer.appendUTF8Char (
c);
140 return buffer.toUTF8();
159 case '0':
case '1':
case '2':
case '3':
case '4':
160 case '5':
case '6':
case '7':
case '8':
case '9':
186 throwError (
"Syntax error", originalLocation);
194 jassert (intValue >= 0 && intValue < 10);
200 auto digit = ((
int)
c) -
'0';
204 intValue = intValue * 10 + digit;
208 if (
c ==
'e' ||
c ==
'E' ||
c ==
'.')
212 return var (isNegative ? -asDouble : asDouble);
216 ||
c ==
',' ||
c ==
'}' ||
c ==
']' ||
c == 0)
222 throwError (
"Syntax error in number", lastPos);
225 auto correctedValue = isNegative ? -intValue : intValue;
227 return (intValue >> 31) != 0 ?
var (correctedValue)
228 :
var ((
int) correctedValue);
235 auto& resultProperties = resultObject->getProperties();
248 throwError (
"Unexpected EOF in object declaration", startOfObjectDecl);
251 throwError (
"Expected a property name in double-quotes", errorLocation);
257 throwError (
"Invalid property name", errorLocation);
265 resultProperties.set (propertyName,
parseAny());
280 auto destArray =
result.getArray();
291 throwError (
"Unexpected EOF in array declaration", startOfArrayDecl);
310 int indentLevel,
bool allOnOneLine,
int maximumDecimalPlaces)
322 else if (
v.isUndefined())
330 else if (
v.isDouble())
332 auto d =
static_cast<double> (
v);
343 else if (
v.isArray())
345 writeArray (
out, *
v.getArray(), indentLevel, allOnOneLine, maximumDecimalPlaces);
347 else if (
v.isObject())
349 if (
auto*
object =
v.getDynamicObject())
350 object->writeAsJSON (
out, indentLevel, allOnOneLine, maximumDecimalPlaces);
357 jassert (! (
v.isMethod() ||
v.isBinaryData()));
372 auto c =
t.getAndAdvance();
378 case '\"':
out <<
"\\\"";
break;
379 case '\\':
out <<
"\\\\";
break;
380 case '\a':
out <<
"\\a";
break;
381 case '\b':
out <<
"\\b";
break;
382 case '\f':
out <<
"\\f";
break;
383 case '\t':
out <<
"\\t";
break;
384 case '\r':
out <<
"\\r";
break;
385 case '\n':
out <<
"\\n";
break;
388 if (
c >= 32 &&
c < 127)
400 for (
int i = 0;
i < 2; ++
i)
416 out.writeRepeatedByte (
' ', (
size_t) numSpaces);
420 int indentLevel,
bool allOnOneLine,
int maximumDecimalPlaces)
429 for (
int i = 0;
i < array.
size(); ++
i)
436 if (
i < array.
size() - 1)
443 else if (! allOnOneLine)
497 return error.getResult();
519 return mo.toString();
529 if (quote !=
'"' && quote !=
'\'')
537 return error.getResult();
555 static String createRandomWideCharString (Random&
r)
557 juce_wchar buffer[40] = { 0 };
567 while (! CharPointer_UTF16::canRepresent (buffer[
i]));
573 return CharPointer_UTF32 (buffer);
576 static String createRandomIdentifier (Random&
r)
582 static const char chars[] =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-:";
583 buffer[
i] = chars [
r.nextInt (
sizeof (chars) - 1)];
586 return CharPointer_ASCII (buffer);
591 static var createRandomDouble (Random&
r)
593 return var ((
r.nextDouble() * 1000.0) + 0.1);
596 static var createRandomVar (Random&
r,
int depth)
598 switch (
r.nextInt (depth > 3 ? 6 : 8))
601 case 1:
return r.nextInt();
602 case 2:
return r.nextInt64();
603 case 3:
return r.nextBool();
604 case 4:
return createRandomDouble (
r);
605 case 5:
return createRandomWideCharString (
r);
609 var
v (createRandomVar (
r, depth + 1));
611 for (
int i = 1 +
r.nextInt (30); --
i >= 0;)
612 v.append (createRandomVar (
r, depth + 1));
619 auto o =
new DynamicObject();
621 for (
int i =
r.nextInt (30); --
i >= 0;)
622 o->setProperty (createRandomIdentifier (
r), createRandomVar (
r, depth + 1));
632 void runTest()
override
637 auto r = getRandom();
639 expect (JSON::parse (String()) == var());
640 expect (JSON::parse (
"{}").isObject());
641 expect (JSON::parse (
"[]").isArray());
642 expect (JSON::parse (
"[ 1234 ]")[0].isInt());
643 expect (JSON::parse (
"[ 12345678901234 ]")[0].isInt64());
644 expect (JSON::parse (
"[ 1.123e3 ]")[0].isDouble());
645 expect (JSON::parse (
"[ -1234]")[0].isInt());
646 expect (JSON::parse (
"[-12345678901234]")[0].isInt64());
647 expect (JSON::parse (
"[-1.123e3]")[0].isDouble());
649 for (
int i = 100; --
i >= 0;)
654 v = createRandomVar (
r, 0);
656 const bool oneLine =
r.nextBool();
657 String asString (JSON::toString (
v, oneLine));
658 var parsed = JSON::parse (
"[" + asString +
"]")[0];
659 String parsedString (JSON::toString (parsed, oneLine));
660 expect (asString.isNotEmpty() && parsedString == asString);
665 beginTest (
"Float formatting");
667 std::map<double, String>
tests;
670 tests[1.01] =
"1.01";
671 tests[0.76378] =
"0.76378";
672 tests[-10] =
"-10.0";
673 tests[10.01] =
"10.01";
674 tests[0.0123] =
"0.0123";
675 tests[-3.7e-27] =
"-3.7e-27";
677 tests[-12345678901234567.0] =
"-1.234567890123457e16";
678 tests[192000] =
"192000.0";
679 tests[1234567] =
"1.234567e6";
680 tests[0.00006] =
"0.00006";
681 tests[0.000006] =
"6.0e-6";
684 expectEquals (JSON::toString (
test.first),
test.second);
689static JSONTests JSONUnitTests;
static void message(int level, const char *fmt,...)
Definition adplugdb.cpp:120
static Result ok() noexcept
Definition Result.h:68
static Result fail(const std::string &errorMessage) noexcept
Definition Result.cpp:58
static String toHexString(int number)
Definition String.cpp:1830
Definition juce_Array.h:56
bool isEmpty() const noexcept
Definition juce_Array.h:222
int size() const noexcept
Definition juce_Array.h:215
ElementType & getReference(int index) noexcept
Definition juce_Array.h:267
Definition juce_CharPointer_UTF16.h:35
static size_t getBytesRequiredFor(juce_wchar charToWrite) noexcept
Definition juce_CharPointer_UTF16.h:248
void write(juce_wchar charToWrite) noexcept
Definition juce_CharPointer_UTF16.h:181
int16 CharType
Definition juce_CharPointer_UTF16.h:40
static bool isWhitespace(char character) noexcept
Definition juce_CharacterFunctions.cpp:59
static double readDoubleValue(CharPointerType &text) noexcept
Definition juce_CharacterFunctions.h:147
static int getHexDigitValue(juce_wchar digit) noexcept
Definition juce_CharacterFunctions.cpp:112
Definition juce_DynamicObject.h:40
Definition juce_File.h:45
Definition juce_Identifier.h:39
bool isValid() const noexcept
Definition juce_Identifier.h:114
static var fromString(StringRef)
Definition juce_JSON.cpp:468
static Result parse(const String &text, var &parsedResult)
Definition juce_JSON.cpp:489
static String escapeString(StringRef)
Definition juce_JSON.cpp:515
static String toString(const var &objectToFormat, bool allOnOneLine=false, int maximumDecimalPlaces=15)
Definition juce_JSON.cpp:503
static void writeToStream(OutputStream &output, const var &objectToFormat, bool allOnOneLine=false, int maximumDecimalPlaces=15)
Definition juce_JSON.cpp:510
static Result parseQuotedString(String::CharPointerType &text, var &result)
Definition juce_JSON.cpp:522
Definition juce_MemoryOutputStream.h:36
Definition juce_OutputStream.h:38
Definition juce_Result.h:57
Definition juce_String.h:53
Definition juce_StringRef.h:62
Definition juce_UnitTest.h:70
Definition juce_Variant.h:42
String paddedLeft(water_uchar padCharacter, int minimumLength) const
Definition String.cpp:1042
* e
Definition inflate.c:1404
struct huft * t
Definition inflate.c:943
unsigned v[N_MAX]
Definition inflate.c:1584
unsigned d
Definition inflate.c:940
register unsigned i
Definition inflate.c:1575
unsigned s
Definition inflate.c:1555
static PuglViewHint int value
Definition pugl.h:1708
JSAMPIMAGE data
Definition jpeglib.h:945
static struct TestCase tests[]
Definition lilv_test.c:2218
float out
Definition lilv_test.c:1461
Definition juce_UnitTestCategories.h:27
JOCTET * buffer
Definition juce_JPEGLoader.cpp:302
Definition carla_juce.cpp:31
NewLine newLine
Definition juce_String.cpp:28
bool juce_isfinite(NumericType) noexcept
Definition juce_MathsFunctions.h:421
static String serialiseDouble(double input)
Definition juce_String.cpp:2260
long long int64
Definition juce_MathsFunctions.h:54
wchar_t juce_wchar
Definition juce_CharacterFunctions.h:42
bool isPositiveAndBelow(Type1 valueToTest, Type2 upperLimit) noexcept
Definition juce_MathsFunctions.h:279
constexpr int numElementsInArray(Type(&)[N]) noexcept
Definition juce_MathsFunctions.h:344
static int test(SerdEnv *env, bool top_level, bool pretty_numbers)
Definition sratom_test.c:79
Definition juce_JSON.cpp:33
int column
Definition juce_JSON.cpp:35
Result getResult() const
Definition juce_JSON.cpp:38
String getDescription() const
Definition juce_JSON.cpp:37
int line
Definition juce_JSON.cpp:35
String message
Definition juce_JSON.cpp:34
Definition juce_JSON.cpp:27
var parseNumber(bool isNegative)
Definition juce_JSON.cpp:189
bool matchIf(char c)
Definition juce_JSON.cpp:58
bool isEOF() const
Definition juce_JSON.cpp:59
void throwError(juce::String message, String::CharPointerType location)
Definition juce_JSON.cpp:41
String parseString(const juce_wchar quoteChar)
Definition juce_JSON.cpp:83
JSONParser(String::CharPointerType text)
Definition juce_JSON.cpp:28
var parseArray()
Definition juce_JSON.cpp:277
juce_wchar peekChar() const
Definition juce_JSON.cpp:57
var parseObjectOrArray()
Definition juce_JSON.cpp:70
String::CharPointerType startLocation
Definition juce_JSON.cpp:30
bool matchString(const char *t)
Definition juce_JSON.cpp:61
void skipWhitespace()
Definition juce_JSON.cpp:55
juce_wchar readChar()
Definition juce_JSON.cpp:56
var parseObject()
Definition juce_JSON.cpp:231
String::CharPointerType currentLocation
Definition juce_JSON.cpp:30
var parseAny()
Definition juce_JSON.cpp:143
const char * text
Definition swell-functions.h:167
return c
Definition crypt.c:175
int r
Definition crypt.c:458
int result
Definition process.c:1455
typedef int(UZ_EXP MsgFn)()
struct zdirent * file
Definition win32.c:1500
mo
Definition zipinfo.c:2287