Skip to content

Commit

Permalink
Merge pull request #7 from avkhozov/master
Browse files Browse the repository at this point in the history
rfc4627:exclude_field method
  • Loading branch information
tonyg committed May 4, 2012
2 parents a58985c + c93a01e commit ae6f55e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/rfc4627.erl
Expand Up @@ -94,7 +94,7 @@
-export([unicode_decode/1, unicode_encode/1]).
-export([from_record/3, to_record/3]).
-export([hex_digit/1, digit_hex/1]).
-export([get_field/2, get_field/3, set_field/3]).
-export([get_field/2, get_field/3, set_field/3, exclude_field/2]).
-export([equiv/2]).

%% @spec () -> string()
Expand Down Expand Up @@ -357,7 +357,7 @@ parse_codepoint([$" | Rest]) -> %% " emacs balancing
{done, Rest};
parse_codepoint([$\\, Key | Rest]) ->
parse_general_char(Key, Rest);
parse_codepoint([X | Rest]) ->
parse_codepoint([X | Rest]) ->
{ok, X, Rest}.

parse_general_char($b, Rest) -> {ok, 8, Rest};
Expand Down Expand Up @@ -526,6 +526,11 @@ decode_record_fields(Values, Fallback, Index, [Field | Rest]) ->
element(Index, Fallback)
end | decode_record_fields(Values, Fallback, Index + 1, Rest)].

%% @spec (JsonObject::jsonobj(), atom()) -> jsonobj()
%% @doc Exclude a named field from a JSON "object".
exclude_field({obj, Props}, Key) ->
{obj, lists:keydelete(Key, 1, Props)}.

%% @spec (JsonObject::jsonobj(), atom()) -> {ok, json()} | not_found
%% @doc Retrieves the value of a named field of a JSON "object".
get_field({obj, Props}, Key) ->
Expand Down
16 changes: 16 additions & 0 deletions test/test_rfc4627.erl
Expand Up @@ -38,6 +38,7 @@ test_all() ->
passed = test_unicode(),
passed = test_equiv(),
passed = test_eof_detection(),
passed = test_exclude(),
passed.

test_codec() ->
Expand Down Expand Up @@ -158,6 +159,21 @@ test_dict() ->
{ok, [1, 2]} = rfc4627:get_field(Obj, "c"),
passed.

test_exclude() ->
Dict = dict:store("c", 2,
dict:store("b", <<"hello">>,
dict:store("a", 123, dict:new()))),
{ok, Obj, ""} = rfc4627:decode(rfc4627:encode(Dict)),
Obj2 = rfc4627:exclude_field(Obj, "a"),
true = rfc4627:equiv({obj, [{"c", 2}, {"b", <<"hello">>}]}, Obj2),
Obj3 = rfc4627:exclude_field(Obj2, "b"),
true = rfc4627:equiv({obj, [{"c", 2}]}, Obj3),
Obj4 = rfc4627:exclude_field(Obj3, "x"),
true = rfc4627:equiv({obj, [{"c", 2}]}, Obj4),
Obj5 = rfc4627:exclude_field(Obj3, "c"),
true = rfc4627:equiv({obj, []}, Obj5),
passed.

test_equiv() ->
true = rfc4627:equiv([1, 2], [1, 2]),
false = rfc4627:equiv([1, 2], [2, 1]),
Expand Down

0 comments on commit ae6f55e

Please sign in to comment.