summaryrefslogtreecommitdiff
path: root/deps/rabbit_common/src/rabbit_json.erl
diff options
context:
space:
mode:
authordcorbacho <dparracorbacho@piotal.io>2020-11-18 14:27:41 +0000
committerdcorbacho <dparracorbacho@piotal.io>2020-11-18 14:27:41 +0000
commitf23a51261d9502ec39df0f8db47ba6b22aa7659f (patch)
tree53dcdf46e7dc2c14e81ee960bce8793879b488d3 /deps/rabbit_common/src/rabbit_json.erl
parentafa2c2bf6c7e0e9b63f4fb53dc931c70388e1c82 (diff)
parent9f6d64ec4a4b1eeac24d7846c5c64fd96798d892 (diff)
downloadrabbitmq-server-git-stream-timestamp-offset.tar.gz
Merge remote-tracking branch 'origin/master' into stream-timestamp-offsetstream-timestamp-offset
Diffstat (limited to 'deps/rabbit_common/src/rabbit_json.erl')
-rw-r--r--deps/rabbit_common/src/rabbit_json.erl63
1 files changed, 63 insertions, 0 deletions
diff --git a/deps/rabbit_common/src/rabbit_json.erl b/deps/rabbit_common/src/rabbit_json.erl
new file mode 100644
index 0000000000..a10569135b
--- /dev/null
+++ b/deps/rabbit_common/src/rabbit_json.erl
@@ -0,0 +1,63 @@
+%% This Source Code Form is subject to the terms of the Mozilla Public
+%% License, v. 2.0. If a copy of the MPL was not distributed with this
+%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
+%%
+%% Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved.
+%%
+
+-module(rabbit_json).
+
+-export([decode/1, decode/2, try_decode/1, try_decode/2,
+ encode/1, encode/2, try_encode/1, try_encode/2]).
+
+-define(DEFAULT_DECODE_OPTIONS, [return_maps]).
+
+
+-spec decode(jsx:json_text()) -> jsx:json_term().
+decode(JSON) ->
+ decode(JSON, ?DEFAULT_DECODE_OPTIONS).
+
+
+-spec decode(jsx:json_text(), jsx_to_term:config()) -> jsx:json_term().
+decode(JSON, Opts) ->
+ jsx:decode(JSON, Opts).
+
+
+-spec try_decode(jsx:json_text()) -> {ok, jsx:json_term()} |
+ {error, Reason :: term()}.
+try_decode(JSON) ->
+ try_decode(JSON, ?DEFAULT_DECODE_OPTIONS).
+
+
+-spec try_decode(jsx:json_text(), jsx_to_term:config()) ->
+ {ok, jsx:json_term()} | {error, Reason :: term()}.
+try_decode(JSON, Opts) ->
+ try
+ {ok, decode(JSON, Opts)}
+ catch error: Reason ->
+ {error, Reason}
+ end.
+
+-spec encode(jsx:json_term()) -> jsx:json_text().
+encode(Term) ->
+ encode(Term, []).
+
+-spec encode(jsx:json_term(), jsx_to_json:config()) -> jsx:json_text().
+encode(Term, Opts) ->
+ jsx:encode(Term, Opts).
+
+
+-spec try_encode(jsx:json_term()) -> {ok, jsx:json_text()} |
+ {error, Reason :: term()}.
+try_encode(Term) ->
+ try_encode(Term, []).
+
+
+-spec try_encode(jsx:json_term(), jsx_to_term:config()) ->
+ {ok, jsx:json_text()} | {error, Reason :: term()}.
+try_encode(Term, Opts) ->
+ try
+ {ok, encode(Term, Opts)}
+ catch error: Reason ->
+ {error, Reason}
+ end.