简体   繁体   English

Erlang JSON API数据生命周期

[英]Erlang JSON API data lifecycle

What is the best practices of using Erlang in JSON API's? 在JSON API中使用Erlang的最佳实践是什么? I mean how do you deal with JSON - Logic - Database - Logic - JSON lifecycle. 我的意思是你如何处理JSON - 逻辑 - 数据库 - 逻辑 - JSON生命周期。 What data structures, tools and techniques are you using in Erlang apps? 您在Erlang应用程序中使用了哪些数据结构,工具和技术?

There are two tools I tend to use for JSON in Erlang: jsx and jiffy . 我倾向于在Erlang中使用两种工具来使用JSON: jsxjiffy Check them both as they have slightly different properties wrt flexibility and speed. 检查两者,因为它们具有略微不同的特性,具有灵活性和速度。

The major problem is that Erlang has no built-in dictionary type. 主要问题是Erlang没有内置字典类型。 Hence, JSON objects are often represented as a property list, ie, 因此,JSON对象通常表示为属性列表,即

{ a : 10,
  b : 20 }

is represented as a list of tuples: 表示为元组列表:

[{a, 10}, {b, 20}]

and this is the major thing to look out for. 这是值得关注的重点。 It also means that one should probably avoid using JSON as the internal representation format as much as possible since it is unwieldy to work with in the long run. 这也意味着应该尽可能避免使用JSON作为内部表示格式,因为从长远来看它很难处理。

For working with JSON, I use mochijson2 and Erlson . 对于使用JSON,我使用mochijson2和Erlson

mochijson2 is a part of the mochiweb project. mochijson2mochiweb项目的一部分。 It is a stable, battle-tested, pure Erlang implementation with flexible JSON parsing and generation APIs. 它是一个稳定的,经过实战考验的纯Erlang实现,具有灵活的JSON解析和生成API。

Erlson provides a nice dictionary syntax for Erlang which is especially handy when working with JSON. Erlson为Erlang提供了一个很好的字典语法,在使用JSON时特别方便。 For example: 例如:

#[
    foo = 1,
    bar = "some string",
    nested = #[i = 1, b = true]
]

In addition, Erlson comes with erlson:to_json and erlson:from_json functions for converting between Erlson and JSON using mochiweb2 . 此外,Erlson附带了erlson:to_jsonerlson:from_json函数,用于使用mochiweb2在Erlson和JSON之间进行转换。

(disclaimer: I'm the author of Erlson) (免责声明:我是Erlson的作者)

Your question is bigger than the answers received so far. 你的问题比到目前为止收到的答案还要大。 However, for Manipulating JSON, they have all told you about mochijson.erl , mochijson2.erl all of which come with mochiweb HTTP Library. 但是,对于Manipulating JSON,他们都告诉过你mochijson.erlmochijson2.erl所有这些都附带了mochiweb HTTP Library。 To use them, a JSON Object is represented as a Struct Object like this: 要使用它们,JSON对象表示为Struct对象,如下所示:

In JSON
{"FirstName": "Joshua", "Surname": "Muzaaya"}
In Erlang
{struct,[{<<"FirstName">>,<<"Joshua">>},{<<"Surname">>,<<"Muzaaya">>}]}

To Parse to and Fro: 要解析和解冻:

JSON_DATA = {"students":[
{"student_number":45,
"details":[{"FirstName": "Joshua", "Surname": "Muzaaya"} ] } ]}
From JSON to Erlang
$> mochijson2:decode(JSON_DATA). {struct,[{<<"students">>, [{struct,[{<<"student_number">>,45}, {<<"details">>, [{struct,[{<<"FirstName">>,<<"Joshua">>}, {<<"Surname">>,<<"Muzaaya">>}]}]}]}]}]}
From Erlang to JSON
$> mochijson2:encode(StructObject).
Now Databases, i dont know what you really want, but, normally, we can write data in a Key Value store , Mnesia , or CouchDB via a RESTFUL access API. 现在数据库,我不知道你真正想要什么,但是,通常,我们可以通过RESTFUL访问API在Key Value商店, MnesiaCouchDB写入数据。 I have written a Couchbase Single Server Client in Erlang. 我在Erlang中编写了一个Couchbase Single Server客户端。 Here are some of the functions inside: 以下是一些内部功能:
\ncreate_database(ServerDomain,DBName)-> create_database(SERVERDOMAIN,数据库名) - >\n    try ibrowse:send_req(ServerDomain ++ DBName,[{"Content-Type", 尝试ibrowse:send_req(ServerDomain ++ DBName,[{“Content-Type”, 
"application/json"}],put,[]) of “application / json”}],put,[])
{ok,_,_,Result} -> {ok,_,_,Result} - > \n case proplists:get_value(<<"ok">>,element(2,mochijson2:decode(Result case proplists:get_value(<<“ok”>>,element(2,mochijson2:decode(Result
))) of )))\n undefined -> {error,failed}; undefined - > {error,failed};\n _ -> ok _ - >好的\n end; 结束;\n Any -> {error,Any} 任何 - > {错误,任何}\n catch 抓住\n EE:EE2 -> {error,{EE,EE2}} EE:EE2 - > {error,{EE,EE2}}\n end. 结束。
\ndelete_database(ServerDomain,DBName)-> delete_database(SERVERDOMAIN,数据库名) - >\n try ibrowse:send_req(ServerDomain ++ DBName,[{"Content-Type","application/json"}],delete,[]) of 尝试ibrowse:send_req(ServerDomain ++ DBName,[{“Content-Type”,“application / json”}],删除,[]) \n {ok,_,_,Result} -> {ok,_,_,Result} - > \n case proplists:get_value(<<"ok">>,element(2,mochijson2:decode(Result))) of case proplists:get_value(<<“ok”>>,element(2,mochijson2:decode(Result)))\n undefined -> {error,failed}; undefined - > {error,failed};\n _ -> ok _ - >好的\n end; 结束;\n Any -> {error,Any} 任何 - > {错误,任何}\n catch 抓住\n EE:EE2 -> {error,{EE,EE2}} EE:EE2 - > {error,{EE,EE2}}\n end. 结束。
\n%% read/3 ::= Struct %% read / 3 :: = Struct
read(ServerDomain,DBName,DocId)-> 读(SERVERDOMAIN,数据库名,的DocID) - > \n try ibrowse:send_req(ServerDomain ++ DBName ++ "/" ++ DocId,[{"Content-Type","application/json"}],get,[]) of 尝试ibrowse:send_req(ServerDomain ++ DBName ++“/”++ DocId,[{“Content-Type”,“application / json”}],get,[])
\n {ok,_,_,Result} -> mochijson2:decode(Result); {ok,_,_,Result} - > mochijson2:decode(Result);
\n Any -> {error,Any} 任何 - > {错误,任何}\n catch 抓住\n EE:EE2 -> {error,{EE,EE2}} EE:EE2 - > {error,{EE,EE2}}\n end. 结束。

\n%% write/3 ::= [{id,Id::string()},{rev,Rev::string()}] %% write / 3 :: = [{id,Id :: string()},{rev,Rev :: string()}]
write(ServerDomain,DBName,DocStruct)-> 写(SERVERDOMAIN,数据库名,DocStruct) - >\n try ibrowse:send_req(ServerDomain ++ DBName,[{"Content-Type","application/json"}],post,lists:flatten(mochijson:encode(DocStruct))) of 尝试ibrowse:send_req(ServerDomain ++ DBName,[{“Content-Type”,“application / json”}],post,lists:flatten(mochijson:encode(DocStruct)))
\n {ok,_,_,Result} -> {ok,_,_,Result} - > \n E = element(2,mochijson2:decode(Result)), E =元素(2,mochijson2:decode(Result)),\n case {proplists:get_value(<<"id">>,E),proplists:get_value(<<"rev">>,E)} of case {proplists:get_value(<<“id”>>,E),proplists:get_value(<<“rev”>>,E)} of\n {undefined,_} -> {error,E}; {undefined,_} - > {error,E};\n {_,undefined} -> {error,E}; {_,undefined} - > {error,E};\n {Id,Rev} -> [{id,binary_to_list(Id)},{rev,binary_to_list(Rev)}] {Id,Rev} - > [{id,binary_to_list(Id)},{rev,binary_to_list(Rev)}]
\n end; 结束;
\n Any -> {error,Any} 任何 - > {错误,任何}\n catch 抓住\n EE:EE2 -> {error,{EE,EE2}} EE:EE2 - > {error,{EE,EE2}}\n end. 结束。

\n%% remember to specify the '_rev' of recent copy when updating %%记得在更新时指定最近副本的'_rev'\n%% update/4 ::= [{id,Id::string()},{rev,Rev::string()}] %% update / 4 :: = [{id,Id :: string()},{rev,Rev :: string()}]\nupdate(ServerDomain,DBName,DocId,DocStruct)-> 更新(SERVERDOMAIN,数据库名,文档ID,DocStruct) - >\n try ibrowse:send_req(ServerDomain ++ DBName ++ "/" ++ DocId,[{"Content-Type","application/json"}],put,lists:flatten(mochijson:encode(DocStruct))) of 尝试ibrowse:send_req(ServerDomain ++ DBName ++“/”++ DocId,[{“Content-Type”,“application / json”}],put,lists:flatten(mochijson:encode(DocStruct)))
\n {ok,_,_,Result} -> {ok,_,_,Result} - > \n E = element(2,mochijson2:decode(Result)), E =元素(2,mochijson2:decode(Result)),\n case {proplists:get_value(<<"id">>,E),proplists:get_value(<<"rev">>,E)} of case {proplists:get_value(<<“id”>>,E),proplists:get_value(<<“rev”>>,E)} of\n {undefined,_} -> {error,E}; {undefined,_} - > {error,E};\n {_,undefined} -> {error,E}; {_,undefined} - > {error,E};\n {Id,Rev} -> [{id,binary_to_list(Id)},{rev,binary_to_list(Rev)}] {Id,Rev} - > [{id,binary_to_list(Id)},{rev,binary_to_list(Rev)}]
\n end; 结束;
\n Any -> {error,Any} 任何 - > {错误,任何}\n catch 抓住\n EE:EE2 -> {error,{EE,EE2}} EE:EE2 - > {error,{EE,EE2}}\n end. 结束。

\n%% you need to provide the '_rev' of current copy %%您需要提供当前副本的'_rev' \n%% of doc to be deleted 要删除的doc%%%
delete(ServerDomain,DBName,DocId,Rev)-> 删除(SERVERDOMAIN,数据库名,文档ID,版本) - >\n try ibrowse:send_req(ServerDomain ++ DBName ++ "/" ++ DocId ++ "?rev=" ++ Rev,[{"Content-Type","application/json"}],delete,[]) of 尝试ibrowse:send_req(ServerDomain ++ DBName ++“/”++ DocId ++“?rev =”++ Rev,[{“Content-Type”,“application / json”}],删除,[])
\n {ok,_,_,Result} -> {ok,_,_,Result} - > \n E = element(2,mochijson2:decode(Result)), E =元素(2,mochijson2:decode(Result)),\n case {proplists:get_value(<<"id">>,E),proplists:get_value(<<"ok">>,E)} of case {proplists:get_value(<<“id”>>,E),proplists:get_value(<<“ok”>>,E)} of\n {undefined,_} -> {error,E}; {undefined,_} - > {error,E};\n {_,undefined} -> {error,E}; {_,undefined} - > {error,E};\n {_Id,_} -> ok {_Id,_} - >好的
\n end; 结束;
\n Any -> {error,Any} 任何 - > {错误,任何}\n catch 抓住\n EE:EE2 -> {error,{EE,EE2}} EE:EE2 - > {error,{EE,EE2}}\n end. 结束。

\nrandom(ServerDomain,Count) when is_integer(Count)-> 随机(ServerDomain,Count)当is_integer(Count) - >时\n try ibrowse:send_req(ServerDomain ++ "_uuids?count=" ++ integer_to_list(Count),[{"Content-Type","application/json"}],get,[]) of 尝试ibrowse:send_req(ServerDomain ++“_ uuids?count =”++ integer_to_list(Count),[{“Content-Type”,“application / json”}],get,[])of \n {ok,_,_,Result} -> {ok,_,_,Result} - > \n [binary_to_list(Z) || [binary_to_list(Z)|| Z <- proplists:get_value(<<"uuids">>,element(2,mochijson2:decode(Result)))]; Z < - proplists:get_value(<<“uuids”>>,element(2,mochijson2:decode(Result)))];\n Any -> {error,Any} 任何 - > {错误,任何}\n catch 抓住\n EE:EE2 -> {error,{EE,EE2}} EE:EE2 - > {error,{EE,EE2}}\n end. 结束。\n

So, perhaps try breaking down your question. 所以,也许试着打破你的问题。 Otherwise, you can see above that i am playing alot with JSON. 否则,你可以看到上面我正在玩很多JSON。 To use the methods abpve, first make user ibrowse is running. 要使用方法abpve,首先使用户ibrowse正在运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM