Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #6 from chemist/master

Here macros for translate list of records to json.
  • Loading branch information...
commit 9720df66052dfc66c9935d954061eda56f81a6f2 2 parents 855f301 + c42b94c
@lambder authored
Showing with 32 additions and 0 deletions.
  1. +18 −0 README
  2. +14 −0 src/jsonerl.hrl
View
18 README
@@ -73,7 +73,25 @@ the resulting json will be:
}
+-record(man,{name,age}).
+Man1 = #man{name = "Petr", age = 45},
+Man2 = #man{name = "Elton", age = 27 },
+ListOfMans = [Man1,Man2],
+ListJson = ?list_record_to_json(man,ListOfMans)
+
+the resulting json will be:
+
+[
+ {
+ name:"Petr",
+ age:45
+ },
+ {
+ name:"Elton",
+ age:27
+ }
+]
View
14 src/jsonerl.hrl
@@ -52,6 +52,20 @@
jsonerl:encode(?record_to_struct(RecordName, Record))
).
+-define(list_records_to_json(RecordName, List),
+ L___ = length(List),
+ Zipped___ = lists:zip(lists:seq(1,L___),List),
+ Quotes___ = lists:map(
+ fun({N___,X___}) ->
+ case L___ == N___ of
+ false ->
+ Json___ = jsonerl:encode(?record_to_struct(RecordName,X___)),
+ Json___ ++ ",";
+ true -> jsonerl:encode(?record_to_struct(RecordName,X___))
+ end
+ end, Zipped___),
+ lists:flatten(io_lib:format("~s",["["++Quotes___++"]"]))).
+
-define(json_to_record(RecordName, Json),
% decode json text to erlang struct
?struct_to_record(RecordName, jsonerl:decode(Json))
Please sign in to comment.
Something went wrong with that request. Please try again.