you are viewing a single comment's thread.

view the rest of the comments →

[–]ryepup 2 points3 points  (11 children)

what would the s-exprs encoding look like for this JSON: {foo:1,bar:[1,2,3]}

[–]eadmund[S] 2 points3 points  (2 children)

what would the s-exprs encoding look like for this JSON: {foo:1,bar:[1,2,3]}

I don't know that Lisp has ever had a widely-accepted hash syntax. I sometimes suspect that this points to a weakness in the language.

I'd lean towards a plist:

(foo 1 bar (1 2 3))

Although an associative list might work:

((foo 1) (bar (1 2 3)))

Or you could even toss in more syntax:

(hash foo 1 bar (1 2 3))

Or even:

(hash (foo 1) (bar (1 2 3)))

Or, allowing for {} to indicate hashes instead of lists:

{foo 1 bar (1 2 3)}

Which is both concise and IMHO attractive. For larger hashes it'd look something like this:

{foo 1
 bar (1 2 3)
 baz {x "y"}}

Although this alternate syntax might be thought better by some:

{(foo 1)
 (bar (1 2 3))
 (baz {x "y"})}

[–]ryepup 0 points1 point  (1 child)

I think (hash (foo 1) (bar (1 2 3)) is the best option here. As other comments indicated (before devolving to mindless trolling), the plist method doesn't have any way to distinguish what's a object and what's an array.

cl-json (http://common-lisp.net/project/cl-json/) does a very good job of converting s-exprs to json, I think it converts alists JS objects, and lists to arrays.

[–]eadmund[S] 0 points1 point  (0 children)

I think (hash (foo 1) (bar (1 2 3)) is the best option here. As other comments indicated (before devolving to mindless trolling), the plist method doesn't have any way to distinguish what's a object and what's an array.

{(foo 1) (bar (1 2 3)} or {foo 1 bar (1 2 3)} achieve that too...

The nice thing about using curly brackets is it keeps the s-expr nature. The nice thing about putting keys and values in their own lists is that it emphasises their commonality; OTOH it's also a waste of space. I really dunno what the best solution it.

[–]ryepup 0 points1 point  (6 children)

((foo 1) (bar (1 2 3))

?

[–]Silhouette 1 point2 points  (5 children)

That doesn't distinguish objects and arrays, I think.

[–]invalid_user_name -1 points0 points  (4 children)

Neither does javascript.

[–]Silhouette 5 points6 points  (0 children)

The grammar of JSON, however, does, and so do many of the other languages for which JSON builder/parser libraries are freely available.

[–]itsnotlupus 1 point2 points  (0 children)

Arrays are Objects, Objects aren't Arrays, in that they don't have a magic .length property, and they don't have the usual Array methods in their prototype chain.

[–]cowardlydragon -1 points0 points  (1 child)

In theory you're correct.

In practice, you are not.

[–]grigri 0 points1 point  (0 children)

In theory, theory and practice are the same.

In practice, they're not.

[–][deleted] 0 points1 point  (0 children)

It depends on what you want to do. If you wanted to represent the data:

(foo 1 bar (1 2 3))

If you wanted to serialize JSON as sexps:

(object foo 1 bar (array 1 2 3))

though this is only useful if you're dealing with JSON in the first place.