Relaxed JSON is a JSON superset that that aims to be easier to write by humans.
Unpoly often accepts JSON in HTML attributes or HTTP headers:
<span class='user' up-data='{ "name": "Bob", "age": 18 }'>Bob</span>
To be easier on the hand and eyes, Unpoly also accepts single-quoted strings and unquoted property names:
<span class="user" up-data="{ name: 'Bob', age: 18 }">Bob</span>
When Unpoly outputs HTML (e.g. for the X-Up-Context
header) it always produces regular JSON.
If you implement the optional server protocol you only need to deal with regular JSON strings.
Your frontend code can parse relaxed JSON using up.util.parseRelaxedJSON()
:
let value = up.util.parseRelaxedJSON("{ foo: 'one', bar: 'two', }")
console.log(value) // logs an object { foo: 'one', bar: 'two' }
On the backend we recommend using a JSON5 parser, which is a superset of relaxed JSON. JSON5 parsers are available in many languages.