Converting from json to scheme list

21 views
Skip to first unread message

Alko YakTender

unread,
Nov 27, 2020, 11:23:40 AM11/27/20
to BiwaScheme

Hello,

I have been trying biwascheme for a while and I think it is a great project!

I have been trying to a small project in which I use biwascheme on the server side using node.js as well as on the client side. I am simply using expressjs as an API to output scheme lists to the client side. When I output a list from the server, it automatically converts it into a json object with a car and cdr properties. I was wondering if there is existed a similar function to convert this json object back to a scheme list?

For eg:
on the server side :

(js-invoke app
       "get"
       "/channels"
       (js-closure
        (lambda (req res next)
          (let1 res-text  (list (list "home" "toto") (list "meow") "travel" "dev")
            (js-invoke res "send" res-text)))))

Here is the json I receive:

{"car":{"car":"home","cdr":{"car":"toto","cdr":{}}},"cdr":{"car":{"car":"meow","cdr":{}},"cdr":{"car":"travel","cdr":{"car":"dev","cdr":{}}}}}

It's great that it can automatically convert the list to json but it would be great if I could just reconstruct the json received back to the same list structure.

Thanks,

Alok

Thomas Elam

unread,
Nov 28, 2020, 5:45:40 AM11/28/20
to biwas...@googlegroups.com
Alok,

It's not exactly what you're looking for, but perhaps dom->string or dom->sexp
in some of my code could be a template.  I'm not sure.

Where are you located?  It would be nice to meet another Biwaschemer in Karnataka, India.

Tom


--
You received this message because you are subscribed to the Google Groups "BiwaScheme" group.
To unsubscribe from this group and stop receiving emails from it, send an email to biwascheme+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/biwascheme/3aeae84e-c55a-474d-9be1-b1c5cccd7702n%40googlegroups.com.

Alko YakTender

unread,
Nov 28, 2020, 1:32:31 PM11/28/20
to BiwaScheme
Hello Tom,


On Saturday, November 28, 2020 at 5:45:40 AM UTC-5 tom...@gmail.com wrote:
Alok,

It's not exactly what you're looking for, but perhaps dom->string or dom->sexp
in some of my code could be a template.  I'm not sure.

 
Thanks for your advice. I will look into your code. I think I can already use list->js-array on the server and  js-array->list on the client to get back the same result. I was wondering if there was something built-in to convert to and from json since it already does the former automatically.
 
Where are you located?  It would be nice to meet another Biwaschemer in Karnataka, India.


I am in the East coast stateside unfortunately but glad to know there are schemers everywhere! I will keep an eye on your project as well for inspiration and encouragement!

Thanks again,

Alok

Thomas Elam

unread,
Nov 28, 2020, 1:40:53 PM11/28/20
to biwas...@googlegroups.com
On Sun, Nov 29, 2020 at 12:02 AM 'Alko YakTender' via BiwaScheme <biwas...@googlegroups.com> wrote:
I am in the East coast stateside unfortunately but glad to know there are schemers everywhere! I will keep an eye on your project as well for inspiration and encouragement!

Look for lots of changes on https://web-call.cc in the near future, mostly in the explanations.
It will be very motivational for me to have people, especially Schemers, look at it and comment.

jcubic

unread,
Nov 29, 2020, 3:17:02 AM11/29/20
to BiwaScheme
Hi,

I think that the problem you have is that you use default to JSON
serialization. You should use S-Expression as you format of exchange data.

Convert Expression to string and send that and then convert that back to list
structure.

Here are two standard scheme function to do just that:

(define (expr->string . expr)
   (let ((port (open-output-string)))
      (let iter ((expr expr))
         (if (null? expr)
             (get-output-string port)
             (begin
               (write (car expr) port)
               (display " " port)
               (iter (cdr expr)))))))

(define (string->expr str)
  (let ((port (open-input-string str)))
    (let iter ((result ()))
       (let ((str (read port)))
          (if (eof-object? str)
              (reverse result)
              (iter (cons str result)))))))


But the second function will not work, it will create infinite loop because
read don't work as it should. You may need to modify the function to read

Alko YakTender

unread,
Nov 29, 2020, 6:42:30 PM11/29/20
to BiwaScheme
Hello jcubic,

Thanks for your input. It feels like it is converting the list to a js-object when being output as a plain list.

I have come up with a function json->list that restores the list back to its original state.

(define (json->list jinput)
  (if (or (js-null? (js-ref jinput "car"))
             (js-undefined? (js-ref jinput "car")))
      (if (or (number? jinput)
          (string? jinput))
      jinput ;; primitive
      '())  ;; empty object
      ;; lets assume that car means cdr is also present
      (cons (json->list (js-ref jinput "car"))
                  (json->list (js-ref jinput "cdr")))))

This works for simple case below but is probably complete for all complex cases.

(define jsonobj (js-invoke (js-eval "JSON") "parse"
               "{\"car\":{\"car\":\"home\",\"cdr\":{\"car\":23, \"cdr\":{}}},
                  \"cdr\":{\"car\":{\"car\":\"meow\",\"cdr\":{}},\"cdr\":{\"car\":\"travel\",\"cdr\":{\"car\":\"dev\",\"cdr\":{}}}}}"))

(console-log (json->list jsonobj))
;; (("home" 23) ("meow") "travel" "dev")

Regards,

Alok

jcubic

unread,
Nov 30, 2020, 2:58:02 AM11/30/20
to BiwaScheme
If this function works for you then i'ts fine, but I would not use JSON conversion in lisp when I have S-Expressions. It don't make any sense to use JSON in lisp,
when there is native and simpler SExp data format. In my LIPS Scheme interpreter I have more literal data types because I have full Numerical tower,
it would be really hard to use JSON for that, you will need to write the parser for converting complex numbers and rationals and read is already a parser for SExpr.
Reply all
Reply to author
Forward
0 new messages