Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #7 from avkhozov/master

rfc4627:exclude_field method
  • Loading branch information...
commit ae6f55ef059d87dae9d32cdc8dda2175234f6ae2 2 parents a58985c + c93a01e
@tonyg authored
Showing with 23 additions and 2 deletions.
  1. +7 −2 src/rfc4627.erl
  2. +16 −0 test/test_rfc4627.erl
View
9 src/rfc4627.erl
@@ -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()
@@ -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};
@@ -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) ->
View
16 test/test_rfc4627.erl
@@ -38,6 +38,7 @@ test_all() ->
passed = test_unicode(),
passed = test_equiv(),
passed = test_eof_detection(),
+ passed = test_exclude(),
passed.
test_codec() ->
@@ -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]),
Please sign in to comment.
Something went wrong with that request. Please try again.