30
30
% % </ul>
31
31
32
32
-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 ]).
34
34
-on_load (init / 0 ).
35
35
-define (LIBNAME , jsonx ).
36
36
-define (APPNAME , jsonx ).
37
37
38
+ % % =================
39
+ % % API Encoding JSON
40
+ % % =================
41
+
38
42
% %@doc Encode JSON.
39
43
-spec encode (JSON_TERM ) -> JSON when
40
44
JSON :: binary (),
41
45
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 .
44
57
58
+ % % ==================
59
+ % % API Decoding JSON
60
+ % % ==================
45
61
46
62
% %@doc Decode JSON to Erlang term.
47
63
-spec decode (JSON ) -> JSON_TERM when
@@ -56,27 +72,12 @@ decode(JSON) ->
56
72
OPTIONS :: [{format , struct |eep18 |proplist }],
57
73
JSON_TERM :: any ().
58
74
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 .
78
79
79
- % %@doc Build JSON decoder.
80
+ % %@doc Build a JSON decoder.
80
81
-spec decoder (RECORDS_DESC ) -> DECODER when
81
82
RECORDS_DESC :: [{tag , [names ]}],
82
83
DECODER :: function ().
@@ -85,25 +86,33 @@ decoder(Records_desc) ->
85
86
Resource = make_decoder_resource (RecCnt , UKeyCnt , KeyCnt , UKeys , Keys , Records3 ),
86
87
fun (JSON_TERM ) -> decode_res (JSON_TERM , eep18 , Resource , true ) end .
87
88
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
90
91
RECORDS_DESC :: [{tag , [names ]}],
91
92
OPTIONS :: [{format , struct |eep18 |proplist }],
92
93
DECODER :: function ().
93
- nonstrict_decoder (Records_desc , Options ) ->
94
+ decoder (Records_desc , Options ) ->
94
95
{RecCnt , UKeyCnt , KeyCnt , UKeys , Keys , Records3 } = prepare_for_dec (Records_desc ),
95
96
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 .
98
102
99
- % % Private, call NIF
103
+ % % ==========
104
+ % % Call NIFs
105
+ % % ==========
100
106
101
- decode_opt ( _JSON , _OPTIONS ) ->
107
+ encode1 ( _JSON_TERM ) ->
102
108
not_loaded (? LINE ).
103
109
104
110
encode_res (_JSON_TERM , _RESOURCE ) ->
105
111
not_loaded (? LINE ).
106
112
113
+ decode_opt (_JSON , _FORMAT ) ->
114
+ not_loaded (? LINE ).
115
+
107
116
decode_res (_JSON_TERM , _FORMAT , _RESOURCE , _STRICT_FLAG ) ->
108
117
not_loaded (? LINE ).
109
118
@@ -113,16 +122,21 @@ make_encoder_resource(_Rcnt, _Fcnt, _Records, _Fields, _Binsz, _Bin) ->
113
122
make_decoder_resource (_RecCnt , _UKeyCnt , _KeyCnt , _UKeys , _Keys , _Records3 ) ->
114
123
not_loaded (? LINE ).
115
124
116
- % % Internal
125
+ % % =================
126
+ % % Private functions
127
+ % % =================
117
128
118
- parse_opt ([]) ->
119
- eep18 ;
120
- parse_opt ([{format , struct } | _ ]) ->
129
+ parse_format ([]) ->
130
+ undefined ;
131
+ parse_format ([{format , struct } | _ ]) ->
121
132
struct ;
122
- parse_opt ([{format , proplist } | _ ]) ->
133
+ parse_format ([{format , proplist } | _ ]) ->
123
134
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
+
126
140
% %%% Internal for decoder
127
141
128
142
prepare_for_dec (Records ) ->
0 commit comments