Skip to content

Commit a053d7a

Browse files
committed
encoder: to ignore null property
1 parent 7ce1a72 commit a053d7a

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

c_src/encoder.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,15 @@ match_record(ErlNifEnv* env, int arity, const ERL_NIF_TERM *tuple, State *st){
278278
unsigned bin_size = 0;
279279
b_putc('{', st);
280280
for(k = 0; k < records[i].arity; k++){
281-
EncField field = fields[fds_offset + k];
282-
bin_size += field.size;
281+
282+
if(tuple[k+1] != st->priv->am_null){
283+
EncField field = fields[fds_offset + k];
284+
bin_size += field.size;
283285
//FIXME {
284-
b_puts(field.size, st->records->bin.data + field.offset, st);
285-
if(!match_term(env, tuple[k+1], st))
286-
return 0;
286+
b_puts(field.size, st->records->bin.data + field.offset, st);
287+
if(!match_term(env, tuple[k+1], st))
288+
return 0;
289+
}
287290
}
288291
b_putc('}', st);
289292
return 1;

test/encode_records_test.erl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,10 @@ encrec1_test() ->
2020
encrec2_test() ->
2121
F = jsonx:encoder([ {rec, record_info(fields, rec)} ]),
2222
<<"{\"a\": \"x\",\"b\": {\"a\": {\"a\": \"x\",\"b\": \"y\"},\"b\": \"w\"}}">> = F({rec, x, {rec, {rec, x, y}, w}}).
23+
24+
25+
26+
encrec3_test() ->
27+
%%F = jsonx:encoder([ {spam, [a, b, c]} ], [{ignore,null}]),
28+
F = jsonx:encoder([ {spam, [a, b, c]} ]),
29+
<<"{}">> = F({spam, null, null, null}).

0 commit comments

Comments
 (0)