From owner-freebsd-hackers@FreeBSD.ORG Sun Dec 14 19:13:06 2014 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 5EF06842; Sun, 14 Dec 2014 19:13:06 +0000 (UTC) Received: from mailout01.t-online.de (mailout01.t-online.de [194.25.134.80]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mailout00.t-online.de", Issuer "TeleSec ServerPass DE-1" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id E4B8C25A; Sun, 14 Dec 2014 19:13:05 +0000 (UTC) Received: from fwd16.aul.t-online.de (fwd16.aul.t-online.de [172.20.26.243]) by mailout01.t-online.de (Postfix) with SMTP id 2857D1728C4; Sun, 14 Dec 2014 20:12:57 +0100 (CET) Received: from [192.168.119.26] (XjoVo0Z68h2UYTIRtz+2kcX5R979xjCQ71FsMNmArv97RdCeGIl3Mnlf1OWiHRNgYp@[84.154.99.91]) by fwd16.t-online.de with (TLSv1.2:ECDHE-RSA-AES256-SHA encrypted) esmtp id 1Y0Eb9-1KhJvU0; Sun, 14 Dec 2014 20:12:55 +0100 Message-ID: <548DE134.8040501@freebsd.org> Date: Sun, 14 Dec 2014 20:12:52 +0100 From: Stefan Esser User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0 MIME-Version: 1.0 To: Jilles Tjoelker , Stefan Esser Subject: Re: [Patch] updated: Add JSON and XML output to pciconf (libxo support - D1206) References: <201412101931.sBAJV7uk076028@idle.juniper.net> <548BF6EF.2090504@freebsd.org> <20141214160256.GB84077@stack.nl> In-Reply-To: <20141214160256.GB84077@stack.nl> Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit X-ID: XjoVo0Z68h2UYTIRtz+2kcX5R979xjCQ71FsMNmArv97RdCeGIl3Mnlf1OWiHRNgYp X-TOI-MSGID: fd8699b8-e9a6-4c85-913c-2787072bc14d Cc: "freebsd-hackers@freebsd.org" , Phil Shafer X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 14 Dec 2014 19:13:06 -0000 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