diff options
author | Francesco Mazzoli <francesco@rabbitmq.com> | 2012-08-23 14:53:13 +0100 |
---|---|---|
committer | Francesco Mazzoli <francesco@rabbitmq.com> | 2012-08-23 14:53:13 +0100 |
commit | 4eadf72452fd7813413fe2d576164a00140490a2 (patch) | |
tree | b4a8b962de050749109b9e9015cba6ee3217b8da | |
parent | 444cc742c9c67c06c488ee46ac0974ae81ebb847 (diff) | |
download | rabbitmq-server-4eadf72452fd7813413fe2d576164a00140490a2.tar.gz |
re-introduced wrapper functions to strip and add the `struct' tags
We can live with the ambiguity of the empty list, since we only use the
term->json function to display the JSON back to the users
-rw-r--r-- | src/rabbit_misc.erl | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/src/rabbit_misc.erl b/src/rabbit_misc.erl index 9b54958d..20f541e5 100644 --- a/src/rabbit_misc.erl +++ b/src/rabbit_misc.erl @@ -60,7 +60,7 @@ -export([multi_call/2]). -export([os_cmd/1]). -export([gb_sets_difference/2]). --export([json_encode/1, json_decode/1]). +-export([json_encode/1, json_decode/1, json_to_term/1, term_to_json/1]). %% Horrible macro to use in guards -define(IS_BENIGN_EXIT(R), @@ -220,6 +220,8 @@ -spec(gb_sets_difference/2 :: (gb_set(), gb_set()) -> gb_set()). -spec(json_encode/1 :: (any()) -> {'ok', string()} | {'error', any()}). -spec(json_decode/1 :: (string()) -> {'ok', any()} | 'error'). +-spec(json_to_term/1 :: (any()) -> any()). +-spec(term_to_json/1 :: (any()) -> any()). -endif. @@ -954,3 +956,21 @@ json_decode(Term) -> %% decoding errors... error:_ -> error end. + +json_to_term({struct, L}) -> + [{K, json_to_term(V)} || {K, V} <- L]; +json_to_term(L) when is_list(L) -> + [json_to_term(I) || I <- L]; +json_to_term(V) when is_binary(V) orelse is_number(V) orelse V =:= null orelse + V =:= true orelse V =:= false -> + V. + +%% This has the flaw that empty lists will never be JSON objects, so use with +%% care. +term_to_json([{_, _}|_] = L) -> + {struct, [{K, term_to_json(V)} || {K, V} <- L]}; +term_to_json(L) when is_list(L) -> + [term_to_json(I) || I <- L]; +term_to_json(V) when is_binary(V) orelse is_number(V) orelse V =:= null orelse + V =:= true orelse V =:= false -> + V. |