Date: Sun, 14 Dec 2014 20:12:52 +0100 From: Stefan Esser <se@freebsd.org> To: Jilles Tjoelker <jilles@stack.nl>, Stefan Esser <se@localhost.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: <548DE134.8040501@freebsd.org> In-Reply-To: <20141214160256.GB84077@stack.nl> References: <201412101931.sBAJV7uk076028@idle.juniper.net> <548BF6EF.2090504@freebsd.org> <20141214160256.GB84077@stack.nl>
next in thread | previous in thread | raw e-mail | index | archive | help
Am 14.12.2014 um 17:02 schrieb Jilles Tjoelker: > 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". Hi Jilles, thanks a lot for the explanation! I'll change all pciconf output to use true/false without quotes, then. This seems to be the best compromise (looks good in XML and JSON when directly reading the output, can be parsed as XML and JSON). @Phil: I think this information makes a good candidate for the libxo documentation (at least I did not see, that it is already mentioned). Best regards, Stefan
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?548DE134.8040501>