日期:2014-05-16 浏览次数:20391 次
上一篇实现了json数据到message记录的转换,这篇则是将message还原为json以便发送给客户端。
首先完善上篇中creationDate部分的解析:
para({"creationDate",Val},Data)-> {obj,List}=Val, Time=#time{}, NewData1=Data#message{time=Time}, NewData=paraEle(List,NewData1) ; para({"date",Val},Data) when is_integer(Val)-> io:format("para date:~p~n",[Data]), #message{time=Time}=Data, NewTime=Time#time{date=Val}, NewData=Data#message{time=NewTime} ; para({"day",Val},Data) when is_integer(Val)-> io:format("para day:~p~n",[Data]), #message{time=Time}=Data, NewTime=Time#time{day=Val}, NewData=Data#message{time=NewTime} ; para({"hours",Val},Data) when is_integer(Val)-> #message{time=Time}=Data, NewTime=Time#time{hours=Val}, NewData=Data#message{time=NewTime} ; para({"minutes",Val},Data) when is_integer(Val)-> #message{time=Time}=Data, NewTime=Time#time{minutes=Val}, NewData=Data#message{time=NewTime} ; para({"month",Val},Data) when is_integer(Val)-> #message{time=Time}=Data, NewTime=Time#time{month=Val}, NewData=Data#message{time=NewTime} ; para({"seconds",Val},Data) when is_integer(Val)-> #message{time=Time}=Data, NewTime=Time#time{seconds=Val}, NewData=Data#message{time=NewTime} ; para({"time",Val},Data) when is_integer(Val)-> #message{time=Time}=Data, NewTime=Time#time{thetime=Val}, NewData=Data#message{time=NewTime} ; para({"timezoneOffset",Val},Data) when is_integer(Val)-> #message{time=Time}=Data, NewTime=Time#time{offset=Val}, NewData=Data#message{time=NewTime} ; para({"year",Val},Data) when is_integer(Val)-> #message{time=Time}=Data, NewTime=Time#time{year=Val}, NewData=Data#message{time=NewTime} ; 再编写编码部分:
paraseEncode(Message)-> {message,Id,Type,From,To,Subject,Content,Time}=Message, {time,Date,Day,Hours,Minutes,Month,Seconds,TheTime,Offset,Year}=Time, Data={obj,[{"content",list_to_binary(Content)}, {"from",list_to_binary(From)}, {"to",list_to_binary(To)}, {"subject",list_to_binary(Subject)}, {"id",list_to_binary(Id)}, {"type",list_to_binary(Type)}, {"creationDate",{obj,[{"date",Date}, {"day",Day}, {"hours",Hours}, {"minutes",Minutes}, {"month",Month}, {"seconds",Seconds}, {"time",TheTime}, {"timezoneOffset",Offset}, {"year",Year} ] } }] }, rfc4627:encode(Data) . 测试如下:
14> util_MessageParas:paraseEncode({message,"289n-2","msg","client1",[],"chat","aaa", {time,27,1,18,8,1,26,1330337306984,-480,112}}). "{\"content\":\"aaa\",\"from\":\"client1\",\"to\":\"\",\"subject\":\"chat\",\"id\":\"289n-2\",\"type\":\"msg\",\"creationDate\":{\"date\":27,\"day\":1,\"hours\":18,\"minutes\":8,\"month\":1,\"seconds\":26,\"time\":1330337306984,\"timezoneOffset\":-480,\"year\":112}}"