-
Notifications
You must be signed in to change notification settings - Fork 28
/
io_json_xml_pb.erl
59 lines (37 loc) · 2.13 KB
/
io_json_xml_pb.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
-module(io_json_xml_pb).
-compile([export_all, nowarn_export_all]).
-include("addressbook_piqi.hrl").
run([Filename]) ->
% Read the existing address book in Protobuf format
{ok, Bytes} = file:read_file(Filename),
AddressBook = addressbook_piqi:parse_address_book(Bytes, 'pb'),
io:format("~n~nErlang: ~n~n~p~n", [AddressBook]),
Json = addressbook_piqi:gen_address_book(AddressBook, 'json'),
io:format("~n~nJSON: ~n~n~s~n", [Json]),
AddressBook = addressbook_piqi:parse_address_book(Json, 'json'),
JsonPretty = addressbook_piqi:gen_address_book(AddressBook, 'json_pretty'),
io:format("~n~nJSON pretty: ~n~n~s~n", [JsonPretty]),
AddressBook = addressbook_piqi:parse_address_book(JsonPretty, 'json'),
% true by default
JsonWithNullFields = addressbook_piqi:gen_address_book(AddressBook, 'json', [{'json_omit_missing_fields', false}]),
io:format("~n~nJSON with null fields: ~n~n~s~n", [JsonWithNullFields]),
AddressBook = addressbook_piqi:parse_address_book(JsonWithNullFields, 'json'),
% true by default
JsonNoPretty = addressbook_piqi:gen_address_book(AddressBook, 'json', [{'pretty_print', false}]),
io:format("~n~nJSON w/o pretty-printing: ~n~n~s~n", [JsonNoPretty]),
AddressBook = addressbook_piqi:parse_address_book(JsonNoPretty, 'json'),
Xml = addressbook_piqi:gen_address_book(AddressBook, 'xml'),
io:format("~n~nXML: ~n~n~s~n", [Xml]),
AddressBook = addressbook_piqi:parse_address_book(Xml, 'xml'),
XmlPretty = addressbook_piqi:gen_address_book(AddressBook, 'xml_pretty'),
io:format("~n~nXML pretty: ~n~n~s~n", [XmlPretty]),
AddressBook = addressbook_piqi:parse_address_book(XmlPretty, 'xml'),
XmlNoPretty = addressbook_piqi:gen_address_book(AddressBook, 'xml', [{'pretty_print', false}]),
io:format("~n~nXML w/o pretty-printing: ~n~n~s~n", [XmlNoPretty]),
AddressBook = addressbook_piqi:parse_address_book(XmlNoPretty, 'xml'),
Piq = addressbook_piqi:gen_address_book(AddressBook, 'piq'),
io:format("~n~nPiq: ~n~n~s~n", [Piq]),
AddressBook = addressbook_piqi:parse_address_book(Piq, 'piq'),
ok;
run(_) ->
erlang:halt(1).