Relaxed JSON parser
In OpenAF, when using the jsonParser function, the parsing sticks to the strict JSON definition.
For example the following behaves as expected:
> jsonParser("{ \"a\": 1 }");
{
"a": 1
}
> JSON.parse("{ \"a\": 1 }");
{
"a": 1
}
But using a more “relaxed” JSON definition, the same functions will fail:
> jsonParser("{ a: 1 }");
{ a: 1 }
> JSON.parse("{ a: 1 }");
-- SyntaxError: Unexpected token in object literal
The jsonParser function will return the text string representation as it’s unable to parse the JSON string. The native JSON.parse will actually throw an execption.
Using GSON
OpenAF includes the GSON library which can parse more “relaxed” JSON definitions. Since OpenAF version 20200108, the OpenAF jsonParser function can also use the GSON library. There is a new second boolean argument that if true alternates to GSON for parsing the string provided on the first argument:
> jsonParse("{ a: 1 }", true);
{
"a": 1
}