Problem with (json-parse) false/nil

Q&A's, tips, howto's

Problem with (json-parse) false/nil

Postby kanen » Mon Dec 29, 2014 11:40 pm

JSON understands only "false", but Lisp understands only "nil" and they are not the same.

false should translate from JSON to nil in Lisp...

Create a file called test.json:
Code: Select all
{
"name": false,
"age": 32,
"employed": true,
"address": {
"street": "701 First Ave.",
"city": "Sunnyvale, CA 95125",
"country": "United States"
} }


Code: Select all
(setf json_text (read-file "test.json"))
setf json_lisp (json-parse json_text))

; the result below should be (name nil) and not (name false)

(("name" false) ("age" 32) ("employed" true) ("address"
  (("street" "701 First Ave.")
   ("city" "Sunnyvale, CA 95125")
   ("country" "United States"))) )


In other words, false is not nil!
Code: Select all
(lookup "name" json_lisp)
; false
(nil? (lookup "name" json_lisp))
; nil


From the newLisp manual:
In newLISP, nil and true represent both the symbols and the Boolean values false and true. Depending on their context, nil and true are treated differently. The following examples use nil, but they can be applied to true by simply reversing the logic.

Evaluation of nil yields a Boolean false and is treated as such inside flow control expressions such as if, unless, while, until, and not. Likewise, evaluating true yields true.
. Kanen Flowers http://kanen.me .
kanen
 
Posts: 145
Joined: Thu Mar 25, 2010 6:24 pm

Re: Problem with (json-parse) false/nil

Postby Lutz » Tue Dec 30, 2014 3:39 pm

You need to reserve nil for newLISP when lookup fails:
Code: Select all
> (lookup "name" '(("name" false) ("age" 32) ("employed" true)))
false
> (lookup "foo" '(("name" false) ("age" 32) ("employed" true)))
nil

“foo” is not a valid object in this case - not part of the JSON data. If you translate JSON "false" to nil, that distinction gets lost and you might think “foo” is part of the data objects with attribute nil.
Lutz
 
Posts: 5279
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California

Re: Problem with (json-parse) false/nil

Postby kanen » Tue Dec 30, 2014 8:31 pm

The problem with this approach is that you now have to have an edge-case for JSON importing, which you'd not have to have in an S-Expression. Which makes JSON a different animal when converted to Lisp. If this is the case, the manual should be very clear about it.
. Kanen Flowers http://kanen.me .
kanen
 
Posts: 145
Joined: Thu Mar 25, 2010 6:24 pm

Re: Problem with (json-parse) false/nil

Postby rickyboy » Mon Jan 05, 2015 4:18 pm

Lutz wrote:You need to reserve nil for newLISP when lookup fails

I thought that's what exp-default was for. From http://www.newlisp.org/downloads/newlis ... tml#lookup:
syntax: (lookup exp-key list-assoc [int-index [exp-default]])

...

Optionally, exp-default can be specified, which is returned if an association matching exp-key cannot be found. If the exp-default is absent and no association has been found, nil is returned.
(λx. x x) (λx. x x)
rickyboy
 
Posts: 594
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Re: Problem with (json-parse) false/nil

Postby rickyboy » Mon Jan 05, 2015 4:31 pm

Also I see Kanen's point about what is said in the manual about it. From http://www.newlisp.org/downloads/newlis ... json-parse:

All data types conforming to the ECMA-262 standard are translated.

In turn, the ECMA-262 standard here says

There are only two Boolean values, true and false.

The newLISP manual says:

nil and true are Boolean data types that evaluate to themselves

And later

In newLISP, nil and true represent both the symbols and the Boolean values false and true

So, if the manual says that "all data types conforming to the ECMA-262 standard are translated," shouldn't the boolean types, in particular, be translated too? Or alternatively, as Kanen said, the manual should say something like "all data types are translated except for ECMAScipt false; so be careful."

I don't know which should prevail. That's why I'm not a language designer :) But it seems that one of the two should prevail. Thoughts?
(λx. x x) (λx. x x)
rickyboy
 
Posts: 594
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Re: Problem with (json-parse) false/nil

Postby Lutz » Mon Jan 12, 2015 4:10 pm

Thanks for the comments Kanen and Rick.

JSON files come from very different domains and the symbolic attribute values true, false and null have different significance and interpretation in all of these domains and in newLISP.

The translation into newLISP s-expressions basically consists of replicating structured JSON data into newLISP nested lists, but the data itself should be taken as close as possible to the source. Strings as strings, numbers as numbers, symbols as symbols.

Going through the code again, I found JSON null being translated into nil and this has been corrected for 10.6.2 now translating JSON null to a LISP symbol null. Note that both null have a different interpretation in the JSON data set and in newLISP. There is a predicate null? in newLISP, but that has a different interpretation. Just like there are true? and nil? predicates but not working as expected on JSON data object values.

Most JSON translators in other programming languages offer functions to deal with the symbolic JSON values true, false, null. E.g: isTrue(), isFalse(), isNull(). In newLISP you could compare to the symbols:
Code: Select all
(if (= 'true json-value) …)
(if (= 'false json-value) …)
(if (= 'null json-value) …)

or define functions or better reader expansion macros:
Code: Select all
(define (is-json-true val) (= 'true val))

;or using macro, which would result in faster performance avoiding call overhead:

(macro (is-json-true Val) (= 'true Val))

value would contain one of the symbols: true, false or null.

Note that true? nil? and null? would not work because true? would also trigger also on strings and numbers, nil? only on nil and null? on 0,0.0,”” and ().

The documentaion is updated in 10.6.2.
Lutz
 
Posts: 5279
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California


Return to newLISP in the real world

Who is online

Users browsing this forum: No registered users and 3 guests

cron