janl closed pull request #945: ejson dupes
URL: https://github.com/apache/couchdb/pull/945
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/src/ejson/ejson.erl b/src/ejson/ejson.erl
index 72bb6c1570..aff5f3d417 100644
--- a/src/ejson/ejson.erl
+++ b/src/ejson/ejson.erl
@@ -36,6 +36,9 @@ init() ->
 decode(undefined) ->
     throw({invalid_json, undefined});
 decode(IoList) ->
+    dedupe_objs(decode_int(IoList)).
+
+decode_int(IoList) ->
     try
         nif_decode(IoList)
     catch exit:ejson_nif_not_loaded ->
@@ -161,6 +164,23 @@ make_ejson([Value | RevEvs], [Vals | RestStack] = _Stack) 
->
     make_ejson(RevEvs, [[Value | Vals] | RestStack]).
 
 
+dedupe_objs({Props}) when is_list(Props) ->
+    RevProps = lists:reverse(Props),
+    {_, NewProps} = lists:foldl(fun({Key, Val}, {Seen, PropAcc}) ->
+        case sets:is_element(Key, Seen) of
+            true ->
+                {Seen, PropAcc};
+            false ->
+                {sets:add_element(Key, Seen), [{Key, Val} | PropAcc]}
+        end
+    end, {sets:new(), []}, RevProps),
+    {NewProps};
+dedupe_objs(Vals) when is_list(Vals) ->
+    lists:map(fun dedupe_objs/1, Vals);
+dedupe_objs(Val) ->
+    Val.
+
+
 reverse_tokens(_) ->
     exit(ejson_nif_not_loaded).
 


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to