Skip to content

Commit

Permalink
Added exclude_field method to rfc4627.
Browse files Browse the repository at this point in the history
  • Loading branch information
avkhozov committed May 4, 2012
1 parent 416a400 commit 788e60a
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([unicode_decode/1, unicode_encode/1]).
-export([from_record/3, to_record/3]). -export([from_record/3, to_record/3]).
-export([hex_digit/1, digit_hex/1]). -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]). -export([equiv/2]).


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


parse_general_char($b, Rest) -> {ok, 8, 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) element(Index, Fallback)
end | decode_record_fields(Values, Fallback, Index + 1, Rest)]. end | decode_record_fields(Values, Fallback, Index + 1, Rest)].


%% @spec (JsonOnbect::jsonobj(), atom()) -> jsononj()
%% @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 %% @spec (JsonObject::jsonobj(), atom()) -> {ok, json()} | not_found
%% @doc Retrieves the value of a named field of a JSON "object". %% @doc Retrieves the value of a named field of a JSON "object".
get_field({obj, Props}, Key) -> 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_unicode(),
passed = test_equiv(), passed = test_equiv(),
passed = test_eof_detection(), passed = test_eof_detection(),
passed = test_exclude(),
passed. passed.


test_codec() -> test_codec() ->
Expand Down Expand Up @@ -158,6 +159,21 @@ test_dict() ->
{ok, [1, 2]} = rfc4627:get_field(Obj, "c"), {ok, [1, 2]} = rfc4627:get_field(Obj, "c"),
passed. 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() -> test_equiv() ->
true = rfc4627:equiv([1, 2], [1, 2]), true = rfc4627:equiv([1, 2], [1, 2]),
false = rfc4627:equiv([1, 2], [2, 1]), false = rfc4627:equiv([1, 2], [2, 1]),
Expand Down

0 comments on commit 788e60a

Please sign in to comment.