diff options
author | Jay Doane <jaydoane@apache.org> | 2020-02-07 14:44:41 -0800 |
---|---|---|
committer | Jay Doane <jaydoane@apache.org> | 2020-11-08 15:06:28 -0800 |
commit | c5b5efbad75552a9b3c8786de251f8a0b32eb129 (patch) | |
tree | 4c41db0b1f6847ae83bb4ce60d45c151a76c04f3 | |
parent | c563243e49548aff7c551484f3713f948a8d8a75 (diff) | |
download | couchdb-port-json-decode-2.tar.gz |
Expose `couch_util:decode/2` to support jiffy optionsport-json-decode-2
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`.
-rw-r--r-- | src/couch/src/couch_util.erl | 7 | ||||
-rw-r--r-- | 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 2e06b0911..95780e8cc 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 3e145c4f6..012c961a4 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])) + ]. |