Erlang 聊天室程序(四) 数据交换格式—json的encode

上一篇实现了json数据到message记录的转换,这篇则是将message还原为json以便发送给客户端。

首先完善上篇中creationDate部分的解析:

  1. para({“creationDate”,Val},Data)->
  2.     {obj,List}=Val,
  3.     Time=#time{},
  4.     NewData1=Data#message{time=Time},
  5.     NewData=paraEle(List,NewData1)
  6. ;
  7. para({“date”,Val},Data) when is_integer(Val)->
  8.     io:format(“para date:~p~n”,[Data]),
  9.     #message{time=Time}=Data,
  10.     NewTime=Time#time{date=Val},
  11.     NewData=Data#message{time=NewTime}
  12. ;
  13. para({“day”,Val},Data) when is_integer(Val)->
  14.     io:format(“para day:~p~n”,[Data]),
  15.     #message{time=Time}=Data,
  16.     NewTime=Time#time{day=Val},
  17.     NewData=Data#message{time=NewTime}
  18. ;
  19. para({“hours”,Val},Data) when is_integer(Val)->
  20.     #message{time=Time}=Data,
  21.     NewTime=Time#time{hours=Val},
  22.     NewData=Data#message{time=NewTime}
  23. ;
  24. para({“minutes”,Val},Data) when is_integer(Val)->
  25.     #message{time=Time}=Data,
  26.     NewTime=Time#time{minutes=Val},
  27.     NewData=Data#message{time=NewTime}
  28. ;
  29. para({“month”,Val},Data) when is_integer(Val)->
  30.     #message{time=Time}=Data,
  31.     NewTime=Time#time{month=Val},
  32.     NewData=Data#message{time=NewTime}
  33. ;
  34. para({“seconds”,Val},Data) when is_integer(Val)->
  35.     #message{time=Time}=Data,
  36.     NewTime=Time#time{seconds=Val},
  37.     NewData=Data#message{time=NewTime}
  38. ;
  39. para({“time”,Val},Data) when is_integer(Val)->
  40.     #message{time=Time}=Data,
  41.     NewTime=Time#time{thetime=Val},
  42.     NewData=Data#message{time=NewTime}
  43. ;
  44. para({“timezoneOffset”,Val},Data) when is_integer(Val)->
  45.     #message{time=Time}=Data,
  46.     NewTime=Time#time{offset=Val},
  47.     NewData=Data#message{time=NewTime}
  48. ;
  49. para({“year”,Val},Data) when is_integer(Val)->
  50.     #message{time=Time}=Data,
  51.     NewTime=Time#time{year=Val},
  52.     NewData=Data#message{time=NewTime}
  53. ;

再编写编码部分:

  1. paraseEncode(Message)->
  2.     {message,Id,Type,From,To,Subject,Content,Time}=Message,
  3.     {time,Date,Day,Hours,Minutes,Month,Seconds,TheTime,Offset,Year}=Time,
  4.     Data={obj,[{"content",list_to_binary(Content)},
  5.               {"from",list_to_binary(From)},
  6.               {"to",list_to_binary(To)},
  7.               {"subject",list_to_binary(Subject)},
  8.               {"id",list_to_binary(Id)},
  9.               {"type",list_to_binary(Type)},
  10.               {"creationDate",{obj,[{"date",Date},
  11.                                {"day",Day},
  12.                                {"hours",Hours},
  13.                                {"minutes",Minutes},
  14.                                {"month",Month},
  15.                                {"seconds",Seconds},
  16.                                {"time",TheTime},
  17.                                {"timezoneOffset",Offset},
  18.                                {"year",Year}
  19.                                 ]
  20.                               }
  21.               }]
  22.           },
  23.    rfc4627:encode(Data)
  24. .

测试如下:

  1. 14> util_MessageParas:paraseEncode({message,”289n-2″,”msg”,”client1″,[],”chat”,”aaa”,
  2.          {time,27,1,18,8,1,26,1330337306984,-480,112}}).
  3. “{\”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}}”


发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

(Spamcheck Enabled)