Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JSON中序列化和反序列化Clojure::命名空间关键字_Json_Authentication_Serialization_Clojure - Fatal编程技术网

在JSON中序列化和反序列化Clojure::命名空间关键字

在JSON中序列化和反序列化Clojure::命名空间关键字,json,authentication,serialization,clojure,Json,Authentication,Serialization,Clojure,我正在尝试将一些Clojure数据结构序列化到一个持久性数据库中,我目前正用于此目的 假设我有一个包含名称空间关键字的映射,如下所示: {:cemerick.friend/identity {:current friend, :authentications {friend {:identity friend, :roles #{:clojure-cms.handler/user}}}}} {:cemerick.friend/identity {:current friend, :authen

我正在尝试将一些Clojure数据结构序列化到一个持久性数据库中,我目前正用于此目的

假设我有一个包含名称空间关键字的映射,如下所示:

{:cemerick.friend/identity {:current friend, :authentications {friend {:identity friend, :roles #{:clojure-cms.handler/user}}}}}
{:cemerick.friend/identity {:current friend, :authentications {:friend {:identity friend, :roles [clojure-cms.handler/user]}}}}
它被序列化为JSON,如下所示:

{"cemerick.friend/identity":{"current":"friend","authentications":{"friend":{"identity":"friend","roles":["clojure-cms.handler/user"]}}}}
当读回并序列化(使用关键字化
(解析字符串数据true)
)时,我得到以下信息:

{:cemerick.friend/identity {:current friend, :authentications {friend {:identity friend, :roles #{:clojure-cms.handler/user}}}}}
{:cemerick.friend/identity {:current friend, :authentications {:friend {:identity friend, :roles [clojure-cms.handler/user]}}}}
如何使用解析此JSON并获得与原始JSON相同的数据

注意:为我试图实现的目标提供了一些上下文。

查看,很明显,
解析字符串的可选关键字参数
将影响JSON对象中的所有名称属性,示例中的名称空间关键字等值属性不受影响。实际上,您的问题有两个方面:原始集也没有正确转换回

对于set问题,您可以做的是编写一个自定义解码器,如中所述

对于最初的问题,除了对返回的映射进行后期处理、找到
:roles
的值并将该值转换为关键字之外,可能没有其他直接方法,如(未测试):


我希望也许能用点别的东西(
edn
也许,但我一点也不知道)。我现在接受你的答案。是的,你可以使用edn,它将正确地反序列化集合和命名空间关键字。试试:
(clojure.edn/read-string(prstr{::namespaced{:non-ns::ns}}))
尽管如此,我认为@schaueho是你问题的正确答案:)@nberger更有趣。在我的例子中,我担心JSON的兼容性(请参阅我原始帖子中的链接问题),你认为这可能是一个问题吗?是的,如果你使用edn,它将不兼容JSON。对不起,我没有得到完整的上下文。。。如果这个序列化映射必须作为更大会话的一部分进入redis,这也必须由node应用程序反序列化,我看到两个选项:在整个会话中使用edn(因此node也应该使用它),或者如果这个映射只在clojure端需要,那么您仍然可以使用edn,但必须对其进行编码(b64?)才能将其放入json映射中。有关如何进行编码的示例,请参见。