如何映射clojure代码到和从JSON? [英] How to map clojure code to and from JSON?

查看:183
本文介绍了如何映射clojure代码到和从JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个疯狂的想法,它涉及到一些clojure代码到CouchDB和编写查询它的视图。我不想将clojure代码存储为纯文本,因为那时我不得不担心在视图中解析它。格式化和注释不需要保留,但是代码应该能够在不改变结构的情况下进入和退出数据库。关键字,符号和字符串应该都保持原生类型。另外,我希望代码看起来很优雅,高效。



我想的代表事情如下:




  • 以'
  • 开头的字符符号

  • 开头的字符串为关键字
  • (括号)作为数组

  • [方括号]作为第一个元素的数组

  • 将({})作为对象

  • 设置为包含值设置为1和_#{}的对象。



赞赏批评,经验和想法。 / p>

编辑:如果我尝试使用clojure.contrib的json函数读写JSON代码,会发生什么:

  user>代码
((ns bz.json.app(:use(ring.middleware file)))(defn hello [req] {:status 200,:headers {Content-Typetext / plain :bodyHello World!})(def app(wrap-file hellopublic)))
user> (json-str code)
[[nsbz.json.app[use[ring.middlewarefile]]] [defnhello [req] {bodyHello World!,headers{Content-Typetext / plain},status hellopublic]]]

有一个公平的位需要做第4行看起来这是一个图书馆项目,除非有一个我不知道的地方的函数。



有了这样的库,这里的调用它可能看起来像:

  user> (=(json-to-code(read-json(json-str(code-to-json code))))code bb $ b true 


解决方案

我认为你的想法是合理的,但我会使用标签数组简化集合的处理( [list,...] [vector,...] )。除此之外,我不会改变实现策略。



我喜欢你的想法和代码在Clojure,所以我采取了一个刺激实现你的 code-to-json (包含上述建议),请访问 https://gist.github.com/ 3219854



这是它生成的输出:

 (code-to-json示例代码)
; => [list[list[list]] 'defnhello[vector'req] {:status200,:headers{Content-Typetext / plain},:bodyHello World! ] [list'def'app'[list'wrap-filehellopublic]]]

json-to-code 留作读者的练习。 ;)


I have a crazy idea, which involves putting some clojure code into CouchDB and writing views that query it. I don't want to store the clojure code as plain text, because then I would have to worry about parsing it in the views. Formatting and comments don't need to be preserved, but the code should be able to go in and out of the database without changing in structure. Keywords, symbols, and strings should all stay in their native type. Additionally, I want the code to look elegant and be efficient.

I'm thinking of representing things as follows:

  • Symbols as strings that start with '
  • Keywords as strings that start with :
  • Strings unmodified, except when they start with ' or :, in which case they're escaped with a backslash.
  • (parens) as an array
  • [brackets] as an array with "_[]" as the first element
  • maps ({}) as an object
  • sets (#{}) as an object with the values set to 1 and "_#{}" included.

Critiques, experiences, and ideas are appreciated.

Edit: Here's what happens if I try reading and writing JSON code using json functions from clojure.contrib:

user> code
((ns bz.json.app (:use (ring.middleware file))) (defn hello [req] {:status 200, :headers {"Content-Type" "text/plain"}, :body "Hello World!"}) (def app (wrap-file hello "public")))
user> (read-json (json-str code))
[["ns" "bz.json.app" ["use" ["ring.middleware" "file"]]] ["defn" "hello" ["req"] {"body" "Hello World!", "headers" {"Content-Type" "text/plain"}, "status" 200}] ["def" "app" ["wrap-file" "hello" "public"]]]

There's a fair bit that needs to be done for line 4 of the above to be exactly like line 2. It appears that it's a library project, unless there's a function somewhere that does it that I don't know about.

With such a library, here's what calling it might look like:

user> (= (json-to-code (read-json (json-str (code-to-json code)))) code)
true

解决方案

I think your idea is sound, but I'd simplify the handling of collections by using tagged arrays (["list", …], ["vector", …]) instead. Apart from that, I wouldn't change the implementation strategy.

I like your idea and to code in Clojure, so I took a stab at implementing your code-to-json (with the above suggestion incorporated) at https://gist.github.com/3219854.

This is the output it generates:

(code-to-json example-code)
; => ["list" ["list" "'ns" "'bz.json.app" ["list" ":use" ["list" "'ring.middleware" "'file"]]] ["list" "'defn" "'hello" ["vector" "'req"] {":status" 200, ":headers" {"Content-Type" "text/plain"}, ":body" "Hello World!"}] ["list" "'def" "'app" ["list" "'wrap-file" "'hello" "public"]]]

json-to-code is left as an exercise for the reader. ;)

这篇关于如何映射clojure代码到和从JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆