Skip to content

Commit 6cb607e

Browse files
committed
Merge branch 'decoder2'
2 parents 0a7f43a + 9e3b826 commit 6cb607e

File tree

5 files changed

+67
-52
lines changed

5 files changed

+67
-52
lines changed

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@ JSONX can encode and decode Erlang records!
1818
-record(person2, {name, age, phone}).
1919

2020
encoder() ->
21-
jsonx:encoder([{person, record_info(fields, person)},
21+
jsonx:encoder1([{person, record_info(fields, person)},
2222
{person2, record_info(fields, person2)} ]).
2323

2424
decoder() ->
25-
jsonx:decoder([{person, record_info(fields, person)},
25+
jsonx:decoder1([{person, record_info(fields, person)},
2626
{person2, record_info(fields, person2)}]).
2727

28-
nstrict_decoder() ->
29-
jsonx:nonstrict_decoder([{person, record_info(fields, person)},
30-
{person2, record_info(fields, person2)}], [{format, struct}]).
28+
nonstrict_decoder1() ->
29+
jsonx:decoder([{person, record_info(fields, person)},
30+
{person2, record_info(fields, person2)}],
31+
[{format, struct}]).
3132
```
3233

3334
```erlang
@@ -46,10 +47,10 @@ nstrict_decoder() ->
4647
friends = [#person2{name = <<"BabaYaga">>,age = 118,
4748
phone = <<"666-66-66">>}]}
4849

49-
5> Encoder = examples:encoder().
50+
5> Encoder = examples:encoder1().
5051
#Fun<jsonx.0.45888425>
5152

52-
6> Decoder = examples:decoder().
53+
6> Decoder = examples:decoder1().
5354
#Fun<jsonx.1.21317315>
5455

5556
7> Json = Encoder(BabaYaga).
@@ -72,10 +73,10 @@ nstrict_decoder() ->
7273
12> Decoder(Json3).
7374
{error,undefined_record,64}
7475

75-
13> NSDecoder = examples:nstrict_decoder().
76+
13> NonStrictDecoder = examples:nonstrict_decoder1().
7677
#Fun<jsonx.2.71844966>
7778

78-
14> JTerm = NSDecoder(Json3).
79+
14> JTerm = NonStrictDecoder(Json3).
7980
[#person2{name = <<"BabaYaga">>,age = 118,
8081
phone = <<"666-66-66">>},
8182
[{<<"record">>,<<"undefined">>},{<<"strict">>,false}]]

c_src/jsonx.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ make_decoder_resource_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]){
197197

198198
static ErlNifFunc
199199
nif_funcs[] = {
200-
{"encode", 1, encode_nif},
200+
{"encode1", 1, encode_nif},
201201
{"encode_res", 2, encode_nif}, // with resource
202202
{"decode_opt", 2, decode_nif}, // with options
203203
{"decode_res", 4, decode_nif}, // with options, resource and strict flag

examples/examples.erl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
-record(person, {name, age, friends}).
55
-record(person2, {name, age, phone}).
66

7-
encoder() ->
7+
encoder1() ->
88
jsonx:encoder([{person, record_info(fields, person)},
99
{person2, record_info(fields, person2)} ]).
1010

11-
decoder() ->
11+
decoder1() ->
1212
jsonx:decoder([{person, record_info(fields, person)},
1313
{person2, record_info(fields, person2)}]).
1414

15-
nstrict_decoder() ->
16-
jsonx:nonstrict_decoder([{person, record_info(fields, person)},
15+
nonstrict_decoder1() ->
16+
jsonx:decoder([{person, record_info(fields, person)},
1717
{person2, record_info(fields, person2)}], [{format, proplist}]).

src/jsonx.erl

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,34 @@
3030
%% </ul>
3131

3232
-module(jsonx).
33-
-export([encode/1, decode/1, decode/2, encoder/1, decoder/1, nonstrict_decoder/2]).
33+
-export([encode/1, decode/1, decode/2, encoder/1, decoder/1, decoder/2]).
3434
-on_load(init/0).
3535
-define(LIBNAME, jsonx).
3636
-define(APPNAME, jsonx).
3737

38+
%% =================
39+
%% API Encoding JSON
40+
%% =================
41+
3842
%%@doc Encode JSON.
3943
-spec encode(JSON_TERM) -> JSON when
4044
JSON :: binary(),
4145
JSON_TERM :: any().
42-
encode(_) ->
43-
not_loaded(?LINE).
46+
encode(JSON_TERM)->
47+
encode1(JSON_TERM).
48+
49+
%%@doc Build a JSON encoder.
50+
-spec encoder(RECORDS_DESC) -> ENCODER when
51+
RECORDS_DESC :: [{tag, [names]}],
52+
ENCODER :: function().
53+
encoder(Records_desc) ->
54+
{Rcnt, Fcnt, Binsz, Records, Fields, Bin} = prepare_enc_desc(Records_desc),
55+
Resource = make_encoder_resource(Rcnt, Fcnt, Records, Fields, Binsz, Bin),
56+
fun(JSON_TERM) -> encode_res(JSON_TERM, Resource) end.
4457

58+
%% ==================
59+
%% API Decoding JSON
60+
%% ==================
4561

4662
%%@doc Decode JSON to Erlang term.
4763
-spec decode(JSON) -> JSON_TERM when
@@ -56,27 +72,12 @@ decode(JSON) ->
5672
OPTIONS :: [{format, struct|eep18|proplist}],
5773
JSON_TERM :: any().
5874
decode(JSON, Options) ->
59-
decode_opt(JSON, parse_opt(Options)).
60-
61-
%% %% Records descriptions for encoder resource
62-
%% {Rcnt %% Records count
63-
%% ,Fcnt %% Counter all fields in records
64-
%% ,Records = [{Tag, Fields_off, Arity}] %% List of records tag, position and length fields
65-
%% ,Fields = [{Name_off, Size}] %% List of position and size fields names in binary storage
66-
%% ,Binsz %% Binary data size
67-
%% ,Bin %% Binary storage for names of fields, format - <,"name": >
68-
%% }
69-
70-
%%@doc Build JSON encoder.
71-
-spec encoder(RECORDS_DESC) -> ENCODER when
72-
RECORDS_DESC :: [{tag, [names]}],
73-
ENCODER :: function().
74-
encoder(Records_desc) ->
75-
{Rcnt, Fcnt, Binsz, Records, Fields, Bin} = prepare_enc_desc(Records_desc),
76-
Resource = make_encoder_resource(Rcnt, Fcnt, Records, Fields, Binsz, Bin),
77-
fun(JSON_TERM) -> encode_res(JSON_TERM, Resource) end.
75+
case parse_format(Options) of
76+
undefined -> decode_opt(JSON, eep18);
77+
F -> decode_opt(JSON, F)
78+
end.
7879

79-
%%@doc Build JSON decoder.
80+
%%@doc Build a JSON decoder.
8081
-spec decoder(RECORDS_DESC) -> DECODER when
8182
RECORDS_DESC :: [{tag, [names]}],
8283
DECODER :: function().
@@ -85,25 +86,33 @@ decoder(Records_desc) ->
8586
Resource = make_decoder_resource(RecCnt, UKeyCnt, KeyCnt, UKeys, Keys, Records3),
8687
fun(JSON_TERM) -> decode_res(JSON_TERM, eep18, Resource, true) end.
8788

88-
%%@doc Build JSON decoder with output undefined objects.
89-
-spec nonstrict_decoder(RECORDS_DESC, OPTIONS) -> DECODER when
89+
%%@doc Build a JSON decoder with output undefined objects.
90+
-spec decoder(RECORDS_DESC, OPTIONS) -> DECODER when
9091
RECORDS_DESC :: [{tag, [names]}],
9192
OPTIONS :: [{format, struct|eep18|proplist}],
9293
DECODER :: function().
93-
nonstrict_decoder(Records_desc, Options) ->
94+
decoder(Records_desc, Options) ->
9495
{RecCnt, UKeyCnt, KeyCnt, UKeys, Keys, Records3} = prepare_for_dec(Records_desc),
9596
Resource = make_decoder_resource(RecCnt, UKeyCnt, KeyCnt, UKeys, Keys, Records3),
96-
Opt = parse_opt(Options),
97-
fun(JSON_TERM) -> decode_res(JSON_TERM, Opt, Resource, false) end.
97+
%%Format = parse_format(Options),
98+
case parse_format(Options) of
99+
undefined -> fun(JSON_TERM) -> decode_res(JSON_TERM, eep18, Resource, false) end;
100+
Format -> fun(JSON_TERM) -> decode_res(JSON_TERM, Format, Resource, false) end
101+
end.
98102

99-
%% Private, call NIF
103+
%% ==========
104+
%% Call NIFs
105+
%% ==========
100106

101-
decode_opt(_JSON, _OPTIONS) ->
107+
encode1(_JSON_TERM) ->
102108
not_loaded(?LINE).
103109

104110
encode_res(_JSON_TERM, _RESOURCE) ->
105111
not_loaded(?LINE).
106112

113+
decode_opt(_JSON, _FORMAT) ->
114+
not_loaded(?LINE).
115+
107116
decode_res(_JSON_TERM, _FORMAT, _RESOURCE, _STRICT_FLAG) ->
108117
not_loaded(?LINE).
109118

@@ -113,16 +122,21 @@ make_encoder_resource(_Rcnt, _Fcnt, _Records, _Fields, _Binsz, _Bin) ->
113122
make_decoder_resource(_RecCnt, _UKeyCnt, _KeyCnt, _UKeys, _Keys, _Records3) ->
114123
not_loaded(?LINE).
115124

116-
%% Internal
125+
%% =================
126+
%% Private functions
127+
%% =================
117128

118-
parse_opt([]) ->
119-
eep18;
120-
parse_opt([{format, struct} | _]) ->
129+
parse_format([]) ->
130+
undefined;
131+
parse_format([{format, struct} | _]) ->
121132
struct;
122-
parse_opt([{format, proplist} | _]) ->
133+
parse_format([{format, proplist} | _]) ->
123134
proplist;
124-
parse_opt([{format, eep18} | _]) ->
125-
eep18.
135+
parse_format([{format, eep18} | _]) ->
136+
eep18;
137+
parse_format([_H | T]) ->
138+
parse_format(T).
139+
126140
%%%% Internal for decoder
127141

128142
prepare_for_dec(Records) ->

test/decode_records_test.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ decrec1_test() ->
2222

2323
%% Test nonstrict decoder
2424
decrec2_test() ->
25-
ND = jsonx:nonstrict_decoder([{r0,record_info(fields, r0)}, {r1,record_info(fields, r1)}, {r2, record_info(fields,r2)}, {r4,record_info(fields, r4)}], [{format, eep18}]),
25+
ND = jsonx:decoder([{r0,record_info(fields, r0)}, {r1,record_info(fields, r1)}, {r2, record_info(fields,r2)}, {r4,record_info(fields, r4)}], [{format, eep18}]),
2626
E = jsonx:encoder([{r0,record_info(fields, r0)}, {r1,record_info(fields, r1)}, {r2, record_info(fields,r2)}, {r4,record_info(fields, r4)}]),
2727
JTerm = [{r4, 1, 2, 3, 4}, [{record, undefined}, {strict, false}]],
2828
Json = E(JTerm),

0 commit comments

Comments
 (0)