W3C home > Mailing lists > Public > public-xformsusers@w3.org > February 2016

JSON: empty number and boolean values

From: Erik Bruchez <erik@bruchez.org>
Date: Wed, 3 Feb 2016 11:58:35 -0800
Message-ID: <CAAc0PEVmymb6WMapQ901FD+UecTw-4e2zB_tPD7KkgaFAUceYg@mail.gmail.com>
To: "public-xformsusers@w3.org" <public-xformsusers@w3.org>
All,

Say we receive the following JSON:

    { "name": "Mark", "age": 21 }

which converts to:

    <json type="object">
        <name>Mark</name>
        <age type="number">21</age>
    </json>

We have an input field bound to:

    instance()/age

Then the user blanks the field, which, say, is not required. So we have:

    <json type="object">
        <name>Mark</name>
        <age type="number"></age>
    </json>

Now when we try to serialize back the XML to JSON, what should happen?
I see 3 options:

1. Generate an empty string for "age".
2. Generate `null` "age".
3. Raise an error.

I don't like #1 because that changes types in an unexpected way.

I don't like #3 because it seems like we should have the possibility
to handle optional values.

My preference would go to #2:

    { "name": "Mark", "age": null }

The rule would be: "if we have a `number` or `boolean` element whose
string value cannot be converted back to a number or a boolean JSON
value, either because it is empty or blank or because it doesn't match
the lexical value of a JSON number of boolean, then generate a
`null`".

Feedback welcome.

-Erik
Received on Wednesday, 3 February 2016 19:59:23 UTC

This archive was generated by hypermail 2.3.1 : Wednesday, 3 February 2016 19:59:23 UTC