You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ja...@apache.org on 2020/11/08 23:07:18 UTC

[couchdb] branch port-json-decode-2 created (now c5b5efb)

This is an automated email from the ASF dual-hosted git repository.

jaydoane pushed a change to branch port-json-decode-2
in repository https://gitbox.apache.org/repos/asf/couchdb.git.


      at c5b5efb  Expose `couch_util:decode/2` to support jiffy options

This branch includes the following new commits:

     new c5b5efb  Expose `couch_util:decode/2` to support jiffy options

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[couchdb] 01/01: Expose `couch_util:decode/2` to support jiffy options

Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jaydoane pushed a commit to branch port-json-decode-2
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit c5b5efbad75552a9b3c8786de251f8a0b32eb129
Author: Jay Doane <ja...@apache.org>
AuthorDate: Fri Feb 7 14:44:41 2020 -0800

    Expose `couch_util:decode/2` to support jiffy options
    
    It can be desirable in some cases for decoded JSON to e.g. return
    maps instead of the default data structure, which is not currently
    possible.
    
    This exposes a new function `couch_util:decode/2`, the second
    parameter being a list of options passed to `jiffy:decode/2`.
---
 src/couch/src/couch_util.erl              | 7 +++++--
 src/couch/test/eunit/couch_util_tests.erl | 7 +++++++
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/src/couch/src/couch_util.erl b/src/couch/src/couch_util.erl
index 2e06b09..95780e8 100644
--- a/src/couch/src/couch_util.erl
+++ b/src/couch/src/couch_util.erl
@@ -21,7 +21,7 @@
 -export([get_nested_json_value/2, json_user_ctx/1]).
 -export([proplist_apply_field/2, json_apply_field/2]).
 -export([to_binary/1, to_integer/1, to_list/1, url_encode/1]).
--export([json_encode/1, json_decode/1]).
+-export([json_encode/1, json_decode/1, json_decode/2]).
 -export([verify/2,simple_call/2,shutdown_sync/1]).
 -export([get_value/2, get_value/3]).
 -export([reorder_results/2]).
@@ -499,8 +499,11 @@ json_encode(V) ->
     jiffy:encode(V, [force_utf8]).
 
 json_decode(V) ->
+    json_decode(V, []).
+
+json_decode(V, Opts) ->
     try
-        jiffy:decode(V, [dedupe_keys])
+        jiffy:decode(V, [dedupe_keys | Opts])
     catch
         error:Error ->
             throw({invalid_json, Error})
diff --git a/src/couch/test/eunit/couch_util_tests.erl b/src/couch/test/eunit/couch_util_tests.erl
index 3e145c4..012c961 100644
--- a/src/couch/test/eunit/couch_util_tests.erl
+++ b/src/couch/test/eunit/couch_util_tests.erl
@@ -168,3 +168,10 @@ to_hex_test_() ->
         ?_assertEqual("", couch_util:to_hex(<<>>)),
         ?_assertEqual("010203faff", couch_util:to_hex(<<1, 2, 3, 250, 255>>))
     ].
+
+json_decode_test_() ->
+    [
+        ?_assertEqual({[]}, couch_util:json_decode(<<"{}">>)),
+        ?_assertEqual({[]}, couch_util:json_decode(<<"{}">>, [])),
+        ?_assertEqual(#{}, couch_util:json_decode(<<"{}">>, [return_maps]))
+    ].