Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 14 Dec 2014 17:02:56 +0100
From:      Jilles Tjoelker <jilles@stack.nl>
To:        Stefan Esser <se@freebsd.org>
Cc:        "freebsd-hackers@freebsd.org" <freebsd-hackers@freebsd.org>, Phil Shafer <phil@juniper.net>
Subject:   Re: [Patch] updated: Add JSON and XML output to pciconf (libxo support - D1206)
Message-ID:  <20141214160256.GB84077@stack.nl>
In-Reply-To: <548BF6EF.2090504@freebsd.org>
References:  <201412101931.sBAJV7uk076028@idle.juniper.net> <548BF6EF.2090504@freebsd.org>

next in thread | previous in thread | raw e-mail | index | archive | help
On Sat, Dec 13, 2014 at 09:21:03AM +0100, Stefan Esser wrote:
> Am 10.12.2014 um 20:31 schrieb Phil Shafer:
> > Stefan Esser writes:
> >> But I have to admit, that I do not really know the rules for
> >> quotes around data fields in JSON. (E.g. must I write "true"
> >> to represent a string value of "true", or could I also use
> >> true without quotes to represent a truth value? What do
> >> parsers do if there are unquoted words or words separated
> >> by blanks?)

> > JSON uses "true", "false", and "null" (without quotes)
> > as unquoted tokens.  If you want a boolean, you'll need:

> >     xo_emit("{n:valid/%s}", valid ? "true" : "false");

> Hmmm, but what will the "reader" of the JSON file expect?

> Is it good practice to have libxo output booleans instead
> of strings, if these are the only possible values?

> Will a JSON parser automatically convert them to strings,
> if an unquoted true or false is read and a strings is
> expected?

> (Sorry, I have bno experience with JSON parsers ...)

It depends on which JSON parser. Each parser and language has its own
rules about type conversion. Some examples:

When using JavaScript JSON.parse, JavaScript's type conversion rules
will apply when using the deserialized data. These will never implicitly
convert a string "true" or "false" to a boolean true or false (both of
them will convert to true because they are non-empty). A boolean will be
converted to a string "true" or "false" when appropriate.

Likewise, when using Python json module, Python's type conversion rules
will apply when using the deserialized data. These will generally allow
few implicit conversions, but as in JavaScript any non-empty string will
be considered true by control flow statements. Explicit conversion of a
boolean to a string gives "True" or "False"; something like
  "true" if v else "false"
is needed to get the lowercase values.

Jansson (C library) and Android's JsonReader do not convert any types at
all. If multiple types are acceptable, the caller will have to handle
it.

JSONObject (Java, from json.org or in Android) will convert a string
"true" or "false" from and to a boolean true or false when it is
requested as such type.

Summarizing, a boolean will be easiest to use if it is serialized as a
JSON boolean. If you do not want to use a JSON boolean for some reason,
a number 0 (false) or 1 (true) will still be easier and safer than a
string "false" or "true".

-- 
Jilles Tjoelker



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20141214160256.GB84077>