Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

S-exp to JSON and back

Name: Anonymous 2012-11-10 7:02

How do you transform/encode a generic s-exp to JSON and back?

inb4 "very carefully."

Name: Anonymous 2012-11-10 7:28

You don't. You stick with bin or SQLlite.
pa tish

Name: Anonymous 2012-11-10 7:38

>>2

But how do I into Lispy webserver <-> JS in browser

Name: Anonymous 2012-11-10 7:42


>
``niggers''

Name: Anonymous 2012-11-10 7:51

>>4
da fuq? u retarded son?

Name: Anonymous 2012-11-10 7:56

>>5
But am I Abelson retarded?

Name: hhhh 2012-11-10 8:02

Vorsicht vor ERIKA TRILLER in Remscheid

Name: Anonymous 2012-11-10 8:29

>>3
Which "Lispy webserver"?

Name: Anonymous 2012-11-10 8:49

Name: Anonymous 2012-11-10 9:16

>>8
Any Lisp/Scheme; I'm asking generally. JSON hash objects can be decoded as lists in server side lisp. But what datatype in Lisp do I use to represent JSON hash objects? I'm asking this in the context of a function implementation:

(define (to-json exp) ...)


How would the above function then know when to treat an s-exp as a JSON array or hash?

Name: Anonymous 2012-11-10 10:17

>>10
hash = { "a": 1, "b": 2 }
  ((a . 1) (b . 2))
array = ['a', 'b']
  (a b)

Name: Anonymous 2012-11-10 12:14

>>11
Wouldn't that fail for:

hash = {"a": [1, 42], "b": 2}
((a 1 42) (b . 2)

Name: Anonymous 2012-11-10 12:30

>>11
Oh shit son, you just got served.

Name: Anonymous 2012-11-10 14:18

>>12
Alright, go the xml-as-sexp route and let hashes = paired lists beginning with @
hash = {"a": [1, 42], "b": 2}
  (@
    a (1 42)
    b 2)

Name: Anonymous 2012-11-10 17:13

>>12
Well, the key (car) is still a, and the value (cdr) is still '(1 42). The real issue is you can't tell the difference between [[1, 2], [3, 4]] and {1: [2], 3: [4]}.

You could just make a json-hash datatype yourself, and use regular pairs for Arrays, depending on which LISP you're using.

Name: Anonymous 2012-11-10 18:07

>>1

Sexps don't special syntax for hashes and arrays, so you'll have to introduce them to maintain the equivalence.


{
  "key1":[29,"val",2],
  "key2":51,
  "key3":{"key3.1":[{},{}], "key3.2":32}
}



(hash
  ("key1" (array 29 "val" 2))
  ("key2" 51)
  ("key3" (hash ("key3.1" (array (hash) (hash))) ("key3.2" 32)))
)


To go from sexp to json, you can just use arrays for everything.


(define (factorial n)
  (letr loop ((n n) (acc 1))
    (if (= n 0)
      acc
      (loop (- n 1) (* n acc)))))



["define",["factorial","n"],
  ["letr","loop",[["n","n"],["acc", 1]],
    ["if",["=","n",0],
      "acc",
      ["loop",["-","n",1],["*","n","acc"]]]]]

Name: Anonymous 2012-11-10 18:16

'(#hash((key . 1)) 1 2)

Name: Anonymous 2012-11-10 19:51

>>6
i fucking love you

Don't change these.
Name: Email:
Entire Thread Thread List