emqx_utils_json_SUITE.erl 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2018-2024 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -module(emqx_utils_json_SUITE).
  17. -compile(export_all).
  18. -compile(nowarn_export_all).
  19. -include_lib("eunit/include/eunit.hrl").
  20. -import(
  21. emqx_utils_json,
  22. [
  23. encode/1,
  24. decode/1,
  25. decode/2
  26. ]
  27. ).
  28. %% copied jiffy/readme
  29. %%--------------------------------------------------------------------
  30. %% Erlang JSON Erlang
  31. %% -------------------------------------------------------------------
  32. %%
  33. %% null -> null -> null
  34. %% true -> true -> true
  35. %% false -> false -> false
  36. %% "hi" -> [104, 105] -> [104, 105]
  37. %% <<"hi">> -> "hi" -> <<"hi">>
  38. %% hi -> "hi" -> <<"hi">>
  39. %% 1 -> 1 -> 1
  40. %% 1.25 -> 1.25 -> 1.25
  41. %% [] -> [] -> []
  42. %% [true, 1.0] -> [true, 1.0] -> [true, 1.0]
  43. %% {[]} -> {} -> {[]}
  44. %% {[{foo, bar}]} -> {"foo": "bar"} -> {[{<<"foo">>, <<"bar">>}]}
  45. %% {[{<<"foo">>, <<"bar">>}]} -> {"foo": "bar"} -> {[{<<"foo">>, <<"bar">>}]}
  46. %% #{<<"foo">> => <<"bar">>} -> {"foo": "bar"} -> #{<<"foo">> => <<"bar">>}
  47. %%--------------------------------------------------------------------
  48. %% but in emqx_utils_json, we use the jsx style for it:
  49. %%--------------------------------------------------------------------
  50. %% Erlang JSON Erlang
  51. %% -------------------------------------------------------------------
  52. %%
  53. %% null -> null -> null
  54. %% true -> true -> true
  55. %% false -> false -> false
  56. %% "hi" -> [104, 105] -> [104, 105]
  57. %% <<"hi">> -> "hi" -> <<"hi">>
  58. %% hi -> "hi" -> <<"hi">>
  59. %% 1 -> 1 -> 1
  60. %% 1.25 -> 1.25 -> 1.25
  61. %% [] -> [] -> []
  62. %% [true, 1.0] -> [true, 1.0] -> [true, 1.0]
  63. %m [{}] -> {} -> [{}]
  64. %a [{<<"foo">>, <<"bar">>}] -> {"foo": "bar"} -> [{<<"foo">>, <<"bar">>}]
  65. %% #{<<"foo">> => <<"bar">>} -> {"foo": "bar"} -> #{<<"foo">> => <<"bar">>}
  66. %m #{<<"foo">> => [{}]} NOT SUPPORT
  67. %%--------------------------------------------------------------------
  68. all() -> emqx_common_test_helpers:all(?MODULE).
  69. t_decode_encode(_) ->
  70. null = decode(encode(null)),
  71. true = decode(encode(true)),
  72. false = decode(encode(false)),
  73. "hi" = decode(encode("hi")),
  74. <<"hi">> = decode(encode(hi)),
  75. 1 = decode(encode(1)),
  76. 1.25 = decode(encode(1.25)),
  77. [] = decode(encode([])),
  78. [true, 1] = decode(encode([true, 1])),
  79. [{}] = decode(encode([{}]), []),
  80. [{<<"foo">>, <<"bar">>}] = decode(encode([{foo, bar}]), []),
  81. [{<<"foo">>, <<"bar">>}] = decode(encode([{<<"foo">>, <<"bar">>}]), []),
  82. [[{<<"foo">>, <<"bar">>}]] = decode(encode([[{<<"foo">>, <<"bar">>}]]), []),
  83. [
  84. [
  85. {<<"foo">>, <<"bar">>},
  86. {<<"a">>, <<"b">>}
  87. ],
  88. [{<<"x">>, <<"y">>}]
  89. ] = decode(
  90. encode([
  91. [
  92. {<<"foo">>, <<"bar">>},
  93. {<<"a">>, <<"b">>}
  94. ],
  95. [{<<"x">>, <<"y">>}]
  96. ]),
  97. []
  98. ),
  99. #{<<"foo">> := <<"bar">>} = decode(encode(#{<<"foo">> => <<"bar">>}), [return_maps]),
  100. JsonText = <<"{\"bool\":true,\"int\":10,\"foo\":\"bar\"}">>,
  101. JsonMaps = #{
  102. <<"bool">> => true,
  103. <<"int">> => 10,
  104. <<"foo">> => <<"bar">>
  105. },
  106. ?assertEqual(JsonText, encode({decode(JsonText, [])})),
  107. ?assertEqual(JsonMaps, decode(JsonText, [return_maps])),
  108. ?assertEqual(
  109. #{<<"foo">> => #{<<"bar">> => <<"baz">>}},
  110. decode(encode(#{<<"foo">> => [{<<"bar">>, <<"baz">>}]}))
  111. ).
  112. t_safe_decode_encode(_) ->
  113. safe_encode_decode(null),
  114. safe_encode_decode(true),
  115. safe_encode_decode(false),
  116. "hi" = safe_encode_decode("hi"),
  117. <<"hi">> = safe_encode_decode(hi),
  118. 1 = safe_encode_decode(1),
  119. 1.25 = safe_encode_decode(1.25),
  120. [] = safe_encode_decode([]),
  121. [true, 1] = safe_encode_decode([true, 1]),
  122. [{}] = decode(encode([{}]), []),
  123. [{<<"foo">>, <<"bar">>}] = safe_encode_decode([{foo, bar}]),
  124. [{<<"foo">>, <<"bar">>}] = safe_encode_decode([{<<"foo">>, <<"bar">>}]),
  125. [[{<<"foo">>, <<"bar">>}]] = safe_encode_decode([[{<<"foo">>, <<"bar">>}]]),
  126. {ok, Json} = emqx_utils_json:safe_encode(#{<<"foo">> => <<"bar">>}),
  127. {ok, #{<<"foo">> := <<"bar">>}} = emqx_utils_json:safe_decode(Json, [return_maps]).
  128. safe_encode_decode(Term) ->
  129. {ok, Json} = emqx_utils_json:safe_encode(Term),
  130. case emqx_utils_json:safe_decode(Json, []) of
  131. {ok, {NTerm}} -> NTerm;
  132. {ok, NTerm} -> NTerm
  133. end.
  134. t_is_json(_) ->
  135. ?assert(emqx_utils_json:is_json(<<"{}">>)),
  136. ?assert(not emqx_utils_json:is_json(<<"foo">>)).