明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 641|回复: 7

请问,Autolisp/Visuallisp有读写Json格式的API吗?

[复制链接]
发表于 2022-4-20 11:37 | 显示全部楼层 |阅读模式
请问,Autolisp/Visuallisp有读写Json格式的API吗?


alisp的中间数据文件,存成什么格式合适?直接append->write-line为lisp表,还是什么?

如能存json当然最好,直观查看,也与其他语言工具通用
发表于 2022-4-21 10:20 | 显示全部楼层
本帖最后由 e2002 于 2022-4-21 10:26 编辑

如果需要和其他语言共同使用这些中间数据,用json格式确实比较合适,但 AutoLISP没有现成的读写json的函数,只能自己写一个专门的函数库,当然,如果你能在github上搞个这样的开源项目就更好了,大家一起打造这个库。
 楼主| 发表于 2022-4-21 11:15 | 显示全部楼层
e2002 发表于 2022-4-21 10:20
如果需要和其他语言共同使用这些中间数据,用json格式确实比较合适,但 AutoLISP没有现成的读写json的函数 ...

感谢版主提供信息。

Lisp的Json读写API,,感觉这基础功能很重要,通用数据交流的桥梁。

如果写个小Arx会方便很多。给lisp用,属性数据的CRUD都归lisp的表,Arx纯转换<表--Json>不涉及acad对象,那么版本兼容性也会不错,不挑cad版本.
发表于 2022-4-21 12:10 | 显示全部楼层
本帖最后由 e2002 于 2022-4-21 12:11 编辑

如果数据的创建和处理都在 AutoCAD内部,直接使用list即可,毕竟 LISP处理 list 数据太方便了。


只是数据的对外保存用一个 list2json函数,读取再写一个 json2list。


 楼主| 发表于 2022-4-21 12:28 | 显示全部楼层
本帖最后由 landsat99 于 2022-4-21 12:35 编辑
e2002 发表于 2022-4-21 12:10
如果数据的创建和处理都在 AutoCAD内部,直接使用list即可,毕竟 LISP处理 list 数据太方便了。

嗯呢。赞同您的意见!

我对lisp不熟,刚接触。感觉Lisp作为dwg图形数据的api可能很合适,,作通用处理 计算 分析可能比较勉强。

不太确定这种以正则为主的操作,适不适合lisp写;不合适的话就作个arx小函数。
 楼主| 发表于 2022-4-24 10:52 | 显示全部楼层
Github有个俄罗斯小哥发布过Autolisp-Json的 读取函数。供大家参考

代码如下
  1. ; AutoLISP JSON parser and formatter
  2. ; (c) 2015, Michel Beloshitsky <mbeloshitsky@gmail.com>
  3. ;
  4. ; TODO:
  5. ;   - Correct numbers handling
  6. ;   - Write formatter
  7. ;   - Remove parasite (list) == nil equivalence

  8. (setq json:debug F)

  9. (defun debug-print (text)
  10.   (if (= T json:debug)
  11.     (princ text)))

  12. (defun string2list (string)
  13.   (setq output (list))
  14.   (foreach charcode (vl-string->list string)
  15.     (setq output (cons (chr charcode) output)))
  16.   (reverse output))

  17. (defun starts-with (pfx stringlist)
  18.   (setq pfxlist (string2list pfx))
  19.   (setq result T)
  20.   (while (not (= pfxlist (list)))
  21.     (setq x (car pfxlist))
  22.     (setq y (car stringlist))
  23.     (setq pfxlist (cdr pfxlist))
  24.     (setq stringlist (cdr stringlist))
  25.     (if (not (= x y))
  26.       (setq result F)))
  27.   (setq result result))

  28. (defun drop (n lst)
  29.   (while (< 0 n)
  30.     (setq lst (cdr lst))
  31.     (setq n (- n 1)))
  32.   (setq lst lst))

  33. (defun take (n lst)
  34.   (setq output (list))
  35.   (while (< 0 n)
  36.     (setq output (cons (car lst) output))
  37.     (setq lst (cdr lst))
  38.     (setq n (- n 1)))
  39.   (reverse output))

  40. (defun readhex (hexlist)
  41.   (defun hex2num (hex) (cond
  42.     ((= hex "0") 0)  ((= hex "1") 1)  ((= hex "2") 2)  ((= hex "3") 3)  ((= hex "4") 4)  ((= hex "5") 5)  ((= hex "6") 6)
  43.     ((= hex "7") 7) ((= hex "8") 8)  ((= hex "9") 9)

  44.     ((= hex "a") 10) ((= hex "b") 11) ((= hex "c") 12) ((= hex "d") 13)  ((= hex "e") 14) ((= hex "f") 15)
  45.     ((= hex "A") 10) ((= hex "B") 11) ((= hex "C") 12) ((= hex "D") 13) ((= hex "E") 14) ((= hex "F") 15)
  46.     ))
  47.   (setq output 0)
  48.   (while (not (= hexlist (list)))
  49.     (setq output (+ (* output 16) (hex2num (car hexlist))))
  50.     (setq hexlist (cdr hexlist))
  51.     )
  52.   (setq output output))

  53. (defun json:read (jsonText)
  54.   ; Так как а автолиспе ниче нет, то разбор производим стейт-машиной c магазином
  55.   (setq curr-atom "")
  56.   (setq curr-key "")
  57.   
  58.   (setq curr-dict (list))
  59.   (setq dict-history (list))

  60.   (defun isdigit (ch)
  61.     (or (= ch "0") (= ch "1") (= ch "2") (= ch "3") (= ch "4") (= ch "5") (= ch "6") (= ch "7") (= ch "8") (= ch "9")))

  62.   (defun isspace (ch)
  63.     (or (= ch " ") (= ch "\t") (= ch "\n") (= ch "\r")))

  64.   (defun push-kv ()
  65.     (if (not (= curr-key ""))
  66.       (setq curr-dict (cons (cons curr-key curr-atom) curr-dict)))
  67.     (setq curr-key "")
  68.     (setq curr-atom ""))

  69.   (setq curr-array (list))
  70.   (setq array-history (list))

  71.   (setq string-delim "")
  72.   
  73.   (setq state "object")
  74.   (setq state-history (list))

  75.   (setq error-message "")
  76.   (defun report-error (message)
  77.     (push-state "error")
  78.     (setq error-message message)
  79.     (setq jsonList (list)))
  80.   
  81.   (defun pop-state ()
  82.     (debug-print (strcat "\n-" state))
  83.     (setq state (car state-history))
  84.     (setq state-history (cdr state-history)))
  85.   (defun push-state (new-state)
  86.     (debug-print (strcat "\n+" new-state))
  87.     (setq state-history (cons state state-history))
  88.     (setq state new-state))

  89.   (defun skip ()
  90.     (setq a ""))

  91.   (setq escaping F)

  92.   (setq jsonList (string2list jsonText))
  93.   (while (not (= jsonList (list)))
  94.     (setq ch (car jsonList))
  95.     (setq jsonList (cdr jsonList))
  96.     (cond
  97.       ((= state "up") (progn
  98.         (setq jsonList (cons ch jsonList))
  99.         (pop-state)
  100.         (pop-state)))

  101.       ((= state "object") (cond
  102.         ((isspace ch) (skip))
  103.         ((= ch "{") (progn
  104.           (setq dict-history (cons (list curr-key curr-dict) dict-history))
  105.           (setq curr-key "")
  106.           (setq curr-dict (list))
  107.           (push-state "object-next")
  108.           ))
  109.         (T (report-error (strcat "Unexpected " ch " instead of { or space")))
  110.         ))

  111.        ((= state "object-semi") (cond
  112.         ((isspace ch) (skip))
  113.         ((= ch ":") (progn
  114.           (setq curr-key curr-atom)
  115.           (pop-state)
  116.           (push-state "object-next")
  117.           (push-state "value")))
  118.         (T (report-error (strcat "Unexpected " ch " instead of : or space")))
  119.          ))

  120.        ((= state "object-next") (cond
  121.         ((isspace ch) (skip))
  122.         ((= ch """) (progn
  123.           (setq jsonList (cons ch jsonList))
  124.           (pop-state)
  125.           (push-state "object-semi")
  126.           (push-state "string")))
  127.         ((= ch ",") (progn
  128.           (push-kv)
  129.           (pop-state)
  130.           (push-state "object-semi")
  131.           (push-state "string")))
  132.         ((= ch "}") (progn
  133.           (push-kv)
  134.           (setq curr-atom curr-dict)
  135.           (setq dh (car dict-history))
  136.           (setq dict-history (cdr dict-history))
  137.           (setq curr-key   (nth 0 dh))
  138.           (setq curr-dict (nth 1 dh))
  139.           (pop-state)
  140.           (pop-state)))
  141.         (T (report-error (strcat "Unexpected " ch " instead of , or } or space")))
  142.          ))

  143.       ((= state "array") (cond
  144.         ((isspace ch) (skip))
  145.         ((= ch "[") (progn
  146.           (setq array-history (cons curr-array array-history))
  147.           (setq curr-array (list))
  148.           (push-state "array-next")
  149.           ))
  150.         (T (report-error (strcat "Unexpected " ch " instead of [ or space")))
  151.         ))

  152.       ((= state "array-next") (cond
  153.         ((isspace ch) (skip))
  154.         ((= ch ",") (progn
  155.            (setq curr-array (cons curr-atom curr-array))
  156.            (pop-state)
  157.           (push-state "array-next")
  158.           (push-state "value")))
  159.         ((= ch "]") (progn
  160.           (setq curr-array (cons curr-atom curr-array))
  161.           (setq curr-atom (reverse curr-array))
  162.           (setq curr-array (car array-history))
  163.           (setq array-history (cdr array-history))
  164.           (pop-state)
  165.           (pop-state)
  166.           ))
  167.         (T (progn
  168.           (setq jsonList (cons ch jsonList))
  169.           (push-state "value")))
  170.         ))
  171.       
  172.       ((= state "value") (cond
  173.         ((isspace ch) (skip))
  174.         ((= ch """)
  175.           (progn
  176.             (setq jsonList (cons ch jsonList))
  177.             (push-state "up")
  178.             (push-state "string")))
  179.         ((= ch "'")
  180.           (progn
  181.             (setq jsonList (cons ch jsonList))
  182.             (push-state "up")
  183.             (push-state "string")))
  184.         ((= ch "{")
  185.           (progn
  186.             (setq jsonList (cons ch jsonList))
  187.             (push-state "up")
  188.             (push-state "object")))
  189.         ((= ch "[") (progn
  190.             (setq jsonList (cons ch jsonList))
  191.             (push-state "up")
  192.             (push-state "array")))
  193.         ((or (= ch "-") (isdigit ch))
  194.           (progn
  195.             (setq jsonList (cons ch jsonList))
  196.             (setq curr-atom "")
  197.             (push-state "up")
  198.             (push-state "number")))

  199.         ((starts-with "true" (cons ch jsonList))
  200.           (progn
  201.             (setq jsonList (drop 3 jsonList))
  202.             (setq curr-atom 'true)
  203.             (pop-state)))
  204.         ((starts-with "false" (cons ch jsonList))
  205.           (progn
  206.             (setq jsonList (drop 4 jsonList))
  207.             (setq curr-atom 'false)
  208.             (pop-state)))
  209.         ((starts-with "null" (cons ch jsonList))
  210.           (progn
  211.             (setq curr-atom 'null)
  212.             (setq jsonList (drop 3 jsonList))
  213.             (pop-state)))
  214.         (T (report-error (strcat "Unexpected " ch " instead of JSON value")))
  215.         ))

  216.       ((= state "string") (cond
  217.         ((isspace ch) (skip))
  218.         ((or (= ch "'") (= ch """))
  219.           (progn
  220.             (setq curr-atom "")
  221.             (setq string-delim ch)
  222.             (push-state "instring")
  223.           ))
  224.         (T (report-error (strcat "Unexpected " ch " instead of " or space")))
  225.         ))

  226.       ((= state "instring") (cond
  227.           (escaping     (progn
  228.             (setq curr-atom (strcat curr-atom ch))
  229.             (setq escaping F)))
  230.           ((= ch "\")  
  231.             (progn
  232.               (setq ext (car jsonList))
  233.               (setq jsonList (cdr jsonList))
  234.               (cond
  235.                 ((= ext "\") (setq curr-atom (strcat curr-atom "\")))
  236.                 ((= ext """) (setq curr-atom (strcat curr-atom """)))
  237.                 ((= ext "/")  (setq curr-atom (strcat curr-atom "/")))
  238.                 ((= ext "b")  (setq curr-atom (strcat curr-atom (chr 8))))
  239.                 ((= ext "f")  (setq curr-atom (strcat curr-atom (chr 12))))
  240.                 ((= ext "n")  (setq curr-atom (strcat curr-atom (chr 10))))
  241.                 ((= ext "r")  (setq curr-atom (strcat curr-atom (chr 13))))
  242.                 ((= ext "t")  (setq curr-atom (strcat curr-atom (chr 9))))
  243.                 ((= ext "u")  (progn
  244.                   (setq curr-atom (strcat curr-atom (chr (readhex (take 4 jsonList)))))
  245.                   (setq jsonList (drop 4 jsonList))
  246.                   )))
  247.                 ))
  248.           ((= ch string-delim)
  249.             (progn
  250.               (pop-state)
  251.               (pop-state)))
  252.           (T (setq curr-atom (strcat curr-atom ch)))
  253.         ))
  254.       
  255.       ((= state "number") (cond
  256.         ((or (isdigit ch) (= ch ".") (= ch "e") (= ch "E") (= ch "-") (= ch "+"))
  257.           (setq curr-atom (strcat curr-atom ch)))
  258.         (T (progn
  259.           (setq curr-atom (atof curr-atom))
  260.           (setq jsonList (cons ch jsonList))
  261.           (pop-state)
  262.           ))
  263.         ))

  264.       ((= state "error") (skip))

  265.       (T (report-error (strcat "Unexpected  JSON parser state")))
  266.       ))
  267.   (if (not (= state "error"))
  268.     (setq curr-atom curr-atom)
  269.     (progn
  270.       (princ (strcat error-message "\n"))
  271.       (setq curr-atom nil))
  272. ))


  273. ;(defun json:write (obj)
  274. ;  ...)


 楼主| 发表于 2022-4-24 11:03 | 显示全部楼层
本帖最后由 landsat99 于 2022-4-24 11:04 编辑

Github另一位小哥的 Alisp-Json的读写函数。

The functions included in json_util.lsp:
_ json_to_list: Return a list with the elements of a string in json format
_ list_to_json: Return a string in json format from a list



  1. ;*************************************************************************************************
  2. ;* INICIO json_util.lsp
  3. ;*************************************************************************************************

  4. ;;-----------------------------------------------------------------------------------------
  5. ;;str_replace function.
  6. ;;Replace all aparisions of "patt" by "repl_to"
  7. ;;
  8. ;;Params:
  9. ;;  -str: The original string
  10. ;;  -patt: The pattern which will be replaced
  11. ;;  -repl_to: The string which will replace the pattern
  12. ;;
  13. ;;Original Code by: diegomcas, 2020/03/25
  14. ;-----------------------------------------------------------------------------------------
  15. (defun dmc:json:str_replace (str patt repl_to / pos inc init_search)

  16.   (if (not str) (setq str ""))
  17.   (if (and (not patt) (< (strlen patt) 1)) (setq patt " "))
  18.   (if (and (not repl_to) (< (strlen repl_to) 1)) (setq repl_to " "))

  19.   (setq pos (vl-string-search patt str))
  20.   (setq inc (1+ (- (strlen repl_to) (strlen patt))))
  21.   ; (princ "pos= ") (princ pos) (princ "\n")
  22.   ; (princ "inc= ") (princ inc) (princ "\n")
  23.   (while pos
  24.     (setq str (vl-string-subst repl_to patt str pos))
  25.     (setq init_search (+ pos inc))
  26.     (if (< init_search 0)
  27.       (setq init_search 0)
  28.     )
  29.     (setq pos (vl-string-search patt str init_search))
  30.   )
  31.   str
  32. )

  33. ;;-----------------------------------------------------------------------------------------
  34. ;;json_parser function (To be executed by json_to_list)
  35. ;;Make a list of list with all data of the json string.
  36. ;;One list for each of:
  37. ;;  -pairs name:value
  38. ;;  -arrays
  39. ;;  -objets
  40. ;;
  41. ;;Params:
  42. ;;  -lst_json: The list prepared by (json_to_list) function
  43. ;;  -state:  for the use of the States Machine
  44. ;;
  45. ;;Original Code by: diegomcas, 2020/03/25
  46. ;;Modified 2021/03/09
  47. ;;-----------------------------------------------------------------------------------------
  48. (defun dmc:json:json_parser (lst_json state array_lvl quote_array / lst str_name res)

  49.   (setq lst '())
  50.   (setq lst_pair '())

  51.   (if (not state)
  52.     (setq state 'Obj)
  53.   )
  54.   
  55.   (if (not array_lvl)
  56.     (setq array_lvl 0)
  57.   )

  58.   (foreach res lst_json
  59.     ; (princ "res= ") (princ res) (princ "\n")
  60.     ; (princ "state= ") (princ state) (princ "\n")
  61.     ; (princ "str_name= ") (princ str_name) (princ "\n")
  62.     ; (princ "array_lvl= ") (princ array_lvl) (princ "\n")
  63.     ; (princ "--------------------------------------------") (princ "\n")

  64.     (cond
  65.       ;Case -> reading '<ARRAY>
  66.       ((eq res (quote <ARRAY>))
  67.         (setq array_lvl (1+ array_lvl))
  68.         (setq state 'Array)
  69.         (if quote_array
  70.           (setq lst (append lst (list res)))
  71.         )
  72.       )
  73.       
  74.       ;Case -> reading '</ARRAY>
  75.       ((eq res (quote </ARRAY>))
  76.         (setq array_lvl (1- array_lvl))
  77.         (setq state 'Obj)
  78.         (if quote_array
  79.           (setq lst (append lst (list res)))
  80.         )
  81.       )

  82.       ;Case -> { null/true/false }
  83.       ((and (eq state 'Obj) (or (eq res 'null) (eq res 'false) (eq res 'true)))
  84.         (setq lst (append lst (list res)))
  85.       )

  86.       ;Case -> {name : value} / value = 'null or 'false or 'true or 'STR or 'INT or 'REAL
  87.       ((and (eq state 'Obj_Value) (or (eq res 'null) (eq res 'false) (eq res 'true) (eq (type res) 'STR) (eq (type res) 'INT) (eq (type res) 'REAL)))
  88.         (if (eq 'STR (type res))
  89.           (progn
  90.             (setq res (dmc:json:str_replace res " , " "," ))
  91.             (setq res (dmc:json:str_replace res " : " ":" ))
  92.             (setq res (dmc:json:str_replace res "( <ARRAY> "  "["))
  93.             (setq res (dmc:json:str_replace res " </ARRAY> )"  "]"))
  94.           )
  95.         )
  96.         (setq lst_pair (cons str_name res))
  97.         (setq lst (append lst (list lst_pair)))
  98.         (setq str_name nil)
  99.       )
  100.       
  101.       ;Case -> finish reading VALUE
  102.       ((and (eq state 'Obj_Value) (eq res ',))
  103.         (setq state 'Obj)
  104.       )
  105.       
  106.       ;Case -> Reading a name {NAME : value}
  107.       ((and (eq state 'Obj) (eq (type res) 'STR))
  108.         (setq res (dmc:json:str_replace res " , " "," ))
  109.         (setq res (dmc:json:str_replace res " : " ":" ))
  110.         (setq res (dmc:json:str_replace res "( <ARRAY> "  "["))
  111.         (setq res (dmc:json:str_replace res " </ARRAY> )"  "]"))

  112.         (setq str_name res)
  113.         (setq state 'Obj_Name)
  114.       )
  115.       
  116.       ;Case -> Reading a new Object
  117.       ((and (eq state 'Obj) (eq (type res) 'LIST))
  118.         (setq lst_temp (dmc:json:json_parser res state array_lvl quote_array))
  119.         (setq lst_pair (cons str_name lst_temp))
  120.         (setq lst (append lst (list lst_pair)))
  121.       )
  122.       
  123.       ;Case -> Reading a value = {Object}
  124.       ((and (eq state 'Obj_Value) (eq (type res) 'LIST))
  125.         (setq lst_temp (dmc:json:json_parser res 'Obj array_lvl quote_array))
  126.         (if str_name
  127.           (setq lst_pair (cons str_name (list lst_temp)))
  128.           (setq lst_pair lst_temp)
  129.         )
  130.         
  131.         (setq lst (append lst (list lst_pair)))
  132.       )
  133.       
  134.       ;Case -> Reading a complete name {NAME : value} -> Preparing to read value
  135.       ((and (eq state 'Obj_Name) (eq res ':))
  136.         (setq state 'Obj_Value)
  137.       )
  138.       
  139.       ;Case -> Reading an Array
  140.       ((and (eq state 'Array) (eq (type res) 'LIST))
  141.         (setq lst_temp (dmc:json:json_parser res nil array_lvl quote_array))
  142.         (if str_name
  143.           (setq lst_pair (cons str_name (list lst_temp)))
  144.           (setq lst_pair lst_temp)
  145.         )
  146.         (setq lst (append lst (list lst_pair)))
  147.       )

  148.       ;Case -> ARRAY value = 'null or 'false or 'true or 'STR or 'INT or 'REAL without name
  149.       ((and (and (> array_lvl 0) (eq state 'Array)) (or (eq res 'null) (eq res 'false) (eq res 'true) (eq (type res) 'STR) (eq (type res) 'INT) (eq (type res) 'REAL)))
  150.         ; (princ res) (princ "\n")
  151.         (setq lst (append lst (list res)))
  152.       )

  153.     )
  154.   )
  155.   lst
  156. )

  157. ;;-----------------------------------------------------------------------------------------
  158. ;;json_to_list function
  159. ;;Make a list of list with all data of the json string.
  160. ;;One list for each of:
  161. ;;  -pairs name:value
  162. ;;  -arrays
  163. ;;  -objets
  164. ;;
  165. ;;Params:
  166. ;;  -json: The json string (Strings values must not contain "(" ")")
  167. ;;
  168. ;;Original Code by: diegomcas, 2020/03/25
  169. ;;-----------------------------------------------------------------------------------------
  170. (defun dmc:json:json_to_list (json quote_array / strtransf stread)
  171.   (if (not (eq 'STR (type json)))
  172.     (setq json "{}")
  173.   )

  174.   ;Lists of lists for registers/arrays/objects of the json
  175.   (setq strtransf (vl-string-translate "{}" "()" json))
  176.   (setq strtransf (dmc:json:str_replace strtransf "[" "( <ARRAY> "))
  177.   (setq strtransf (dmc:json:str_replace strtransf "]" " </ARRAY> )"))
  178.   ;add spaces before "," / Repairing bad read of numbers
  179.   (setq strtransf (dmc:json:str_replace strtransf "," " , "))
  180.   (setq strtransf (dmc:json:str_replace strtransf ":" " : "))

  181.   (setq stread (read strtransf))
  182.   ;(princ "json -> ")(princ json) (princ "\n")
  183.   ;(princ "stread -> ")(princ stread) (princ "\n")

  184.   (dmc:json:json_parser stread nil 0 quote_array)
  185. )

  186. ;;-----------------------------------------------------------------------------------------
  187. ;;list_to_json function
  188. ;;Make a json string with all data of the json list.
  189. ;;
  190. ;;Not complete testing!!! Testing for use it!!!
  191. ;;Params:
  192. ;;  -lst: The list of which will be written the json
  193. ;;
  194. ;;Autolisp don't have Arrays, so it is impossible to rebuild a json that contains them.
  195. ;;This function assume the list is Array Quote
  196. ;;
  197. ;;If you run (list_to_json (json_to_list "{your_json}"))
  198. ;;you probably lose data (type of data)
  199. ;;
  200. ;;Original Code by: diegomcas, 2020/03/25
  201. ;;Modified 2021/03/09
  202. ;;-----------------------------------------------------------------------------------------
  203. (defun dmc:json:list_to_json (lst / lst_element json reading_val init_state value_to_string is_value is_attribute_simple is_attribute_complex is_array)

  204.   ; (defun is_object (lst / )
  205.     ; (and (eq (length lst) 2) (eq 'STR (type (car lst))) (not (is_array lst)))
  206.   ; )
  207.   
  208.   (defun value_to_string (element / )
  209.     (cond
  210.       ((eq 'STR (type element))
  211.         (strcat """ element """)
  212.       )
  213.       ((eq 'REAL (type element))
  214.         (rtos element 2 8)
  215.       )
  216.       ((eq 'INT (type element))
  217.         (itoa element)
  218.       )
  219.       ((eq 'NULL element)
  220.         "null"
  221.       )
  222.       ((eq 'FALSE element)
  223.         "false"
  224.       )
  225.       ((eq 'TRUE element)
  226.         "true"
  227.       )
  228.     )
  229.   )
  230.   
  231.   (defun is_value (val / )
  232.     (or
  233.       (= (type val) 'STR)
  234.       (= (type val) 'REAL)
  235.       (= (type val) 'INT)
  236.       (= 'NULL val)
  237.       (= 'FALSE val)
  238.       (= 'TRUE val)
  239.     )
  240.   )
  241.   
  242.   (defun is_attribute_simple (lst / )
  243.     (and (is_value (cdr lst)) (eq 'STR (type (car lst))) (not (is_array lst)))
  244.   )

  245.   (defun is_attribute_complex (lst / )
  246.     (and (= 'LIST (type (cdr lst))) (eq 'STR (type (car lst))) (not (is_array lst)))
  247.   )
  248.   
  249.   (defun is_array (lst / res)
  250.     (eq (QUOTE <ARRAY>) (car lst))
  251.   )

  252.   (defun read_list (lst json state / )
  253.    
  254.     (foreach lst_element lst
  255.       ; (princ "---------------------------------------------------------\n")
  256.       ; (princ "IN state    -> ") (princ state) (princ "\n")
  257.       ; (princ "lst_element -> ") (princ lst_element) (princ "\n")
  258.       
  259.       (if (eq 'LIST (type lst_element)) ; Set of data
  260.         (progn
  261.           ; (princ "Is LIST")(princ "\n")
  262.           (cond
  263.             ; ARRAY -> Read any value (Objects, Arrays, strings, numbers, 'true, 'false, 'null)
  264.             ((is_array lst_element)
  265.               ; (princ "-> INIT ARRAY.") (princ "\n")
  266.               (setq reading_val nil)
  267.               (setq json (strcat json "["))
  268.               (setq json (read_list lst_element json 'ARRAY))
  269.               (setq json (strcat json "],"))
  270.             )

  271.             ; ATTRIBUTES IN AN OBJECT
  272.             ((is_attribute_complex lst_element)
  273.               ; (princ "-> ATTRIBUTE COMPLEX IN AN OBJECT.") (princ "\n")
  274.               (setq reading_val nil)
  275.               ; (princ "-> ATTRIBUTE complex.") (princ "\n")
  276.               (setq json (strcat json (value_to_string (car lst_element)) ":"))
  277.               (setq json (read_list (cdr lst_element) json 'OBJECT))
  278.             )
  279.             
  280.             ((is_attribute_simple lst_element)
  281.               (setq reading_val nil)
  282.               ; (princ "-> ATTRIBUTE simple.") (princ "\n")
  283.               (setq json
  284.                 (strcat
  285.                   json
  286.                   (value_to_string (car lst_element)) ":"
  287.                   (value_to_string (cdr lst_element)) ","
  288.                 )
  289.               )
  290.             )
  291.             
  292.             ; NO ARRAY / NO ATTRIBUTE -> Read Object
  293.             (T
  294.               ; (princ "-> INIT OBJECT.") (princ "\n")
  295.               (setq reading_val nil)
  296.               (setq json (strcat json "{"))
  297.               (setq json (read_list lst_element json 'OBJECT))
  298.               (setq json (strcat json "},"))
  299.             )
  300.           )
  301.         )
  302.         (progn ;not is list / is name or value
  303.           ; (princ "not is list / is name or value.") (princ "\n")
  304.           (if (not (or (eq lst_element '<ARRAY>) (eq lst_element '</ARRAY>)))
  305.             (cond
  306.               ((eq state 'ARRAY)
  307.                 (setq json (strcat json (value_to_string lst_element) ","))
  308.               )
  309.               ((eq state 'ATTRIB)
  310.                 (if reading_val
  311.                   (progn
  312.                     (setq json (strcat json (value_to_string lst_element) ","))
  313.                     (setq reading_val nil)
  314.                   )
  315.                   (progn
  316.                     (setq json (strcat json (value_to_string lst_element) ":"))
  317.                     (setq reading_val T)
  318.                   )
  319.                 )
  320.               )
  321.               ((eq state 'OBJECT)
  322.               )
  323.             )
  324.           )
  325.         )
  326.       )
  327.       ; (princ "OUT state   -> ") (princ state) (princ "\n")
  328.       ; (princ "JSON        -> ") (princ json) (princ "\n")
  329.       ; (princ "---------------------------------------------------------\n")
  330.     )

  331.     json
  332.   )
  333.   
  334.   (if (is_array lst)
  335.     (progn
  336.       (setq init_state 'ARRAY)
  337.       (setq init_string "[")
  338.     )
  339.     (progn
  340.       (setq init_state 'OBJECT)
  341.       (setq init_string "{")
  342.     )
  343.   )
  344.   
  345.   (setq json (read_list lst init_string init_state))

  346.   (if (eq "[" init_string)
  347.     (setq json (strcat json "]"))
  348.     (setq json (strcat json "}"))
  349.   )
  350.   
  351.   ;Clean the string
  352.   (setq json (dmc:json:str_replace json ",}" "}"))
  353.   (setq json (dmc:json:str_replace json ",]" "]"))
  354. )

  355. ;*************************************************************************************************
  356. ;* FIN json_util.lsp
  357. ;*************************************************************************************************
发表于 2022-11-2 10:05 | 显示全部楼层
landsat99 发表于 2022-4-24 11:03
Github另一位小哥的 Alisp-Json的读写函数。

The functions included in json_util.lsp:

这个list转json的程序,出来的结果不对啊。
例如((1)(2)(3)),转出来是{{}{}{}},
字符都没有了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-4-26 22:53 , Processed in 0.627694 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表