您的位置:首页 > Web前端 > JavaScript

mochiweb 、mochijson

2016-05-23 17:21 501 查看
Mochiweb是Erlang实现的一个开源Web服务器,它设计的一个亮点就是他本身的Http请求的参数化模型。因此我们可以用OO的方式来理解它的相关用法。
它的实现在mochiweb_request模块.在mochiweb中,每个client请求其构造一个 Req 对象(注:这个“对象“只是便于理解的提法), Req 可以理解成 mochiweb_request 的一个参数化或实例化.
http://www.360doc.com/content/12/1003/15/10834595_239290379.shtml http://blog.csdn.net/dp0304/article/details/6994435
mochijson2  :The mochijson2 API essentially consists of just two functions, which (if you've downloaded mochiweb) can be used directly from the Erlang shell as follows:

erl -pa path/to/mochiweb/ebin
...
1> mochijson2:decode(<<"[1,2,3]">>).
[1,2,3]
2> iolist_to_binary(mochijson2:encode([1,2,3])).
<<"[1,2,3]">>

ERL  :: {struct, [{strKey, <<"strVal">>}, {intKey, 10}, {arrayKey, [1, 2, 3]}]}
JSON :: {strKey:"strVal", intKey:10, arrayKey:[1, 2, 3]}

http://stackoverflow.com/questions/1000046/mochijson2-examples
部分说明

%% @author Bob Ippolito <bob@mochimedia.com>

%% @copyright 2007 Mochi Media, Inc.

%% @doc Yet another JSON (RFC 4627) library for Erlang. mochijson2 works

%%      with binaries as strings, arrays as lists (without an {array, _})

%%      wrapper and it only knows how to decode UTF-8 (and ASCII).

%%

%%      JSON terms are decoded as follows (javascript -> erlang):

%%      <ul>

%%          <li>{"key": "value"} ->

%%              {struct, [{<<"key">>, <<"value">>}]}</li>

%%          <li>["array", 123, 12.34, true, false, null] ->

%%              [<<"array">>, 123, 12.34, true, false, null]

%%          </li>

%%      </ul>

%%      <ul>

%%          <li>Strings in JSON decode to UTF-8 binaries in Erlang</li>

%%          <li>Objects decode to {struct, PropList}</li>

%%          <li>Numbers decode to integer or float</li>

%%          <li>true, false, null decode to their respective terms.</li>

%%      </ul>

%%      The encoder will accept the same format that the decoder will produce,

%%      but will also allow additional cases for leniency:

%%      <ul>

%%          <li>atoms other than true, false, null will be considered UTF-8

%%              strings (even as a proplist key)

%%          </li>

%%          <li>{json, IoList} will insert IoList directly into the output

%%              with no validation

%%          </li>

%%          <li>{array, Array} will be encoded as Array

%%              (legacy mochijson style)

%%          </li>

%%          <li>A non-empty raw proplist will be encoded as an object as long

%%              as the first pair does not have an atom key of json, struct,

%%              or array

%%          </li>

%%      </ul>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: