jsone_decode_tests.erl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. %% Copyright (c) 2013-2015, Takeru Ohta <phjgt308@gmail.com>
  2. %% coding: latin-1
  3. -module(jsone_decode_tests).
  4. -include_lib("eunit/include/eunit.hrl").
  5. -ifdef('NO_MAP_TYPE').
  6. -define(MAP_OBJECT_TYPE, tuple).
  7. -define(OBJ0, {[]}).
  8. -define(OBJ1(K, V), {[{K, V}]}).
  9. -define(OBJ2(K1, V1, K2, V2), {[{K1, V1}, {K2, V2}]}).
  10. -define(OBJ2_DUP_KEY(K1, V1, K2, V2), ?OBJ2(K1, V1, K2, V2)).
  11. -else.
  12. -define(MAP_OBJECT_TYPE, map).
  13. -define(OBJ0, #{}).
  14. -define(OBJ1(K, V), #{K => V}).
  15. -define(OBJ2(K1, V1, K2, V2), #{K1 => V1, K2 => V2}).
  16. -define(OBJ2_DUP_KEY(K1, V1, _K2, _V2), #{K1 => V1}). % the first (leftmost) value is used
  17. -define(OBJ2_DUP_KEY_LAST(_K1, _V1, K2, V2), #{K2 => V2}). % the last value is used
  18. -endif.
  19. decode_test_() ->
  20. [
  21. %% Symbols
  22. {"false",
  23. fun () ->
  24. ?assertEqual({ok, false, <<"">>}, jsone_decode:decode(<<"false">>))
  25. end},
  26. {"true",
  27. fun () ->
  28. ?assertEqual({ok, true, <<"">>}, jsone_decode:decode(<<"true">>))
  29. end},
  30. {"null",
  31. fun () ->
  32. ?assertEqual({ok, null, <<"">>}, jsone_decode:decode(<<"null">>))
  33. end},
  34. %% Numbers: Integer
  35. {"positive integer",
  36. fun () ->
  37. ?assertEqual({ok, 1, <<"">>}, jsone_decode:decode(<<"1">>))
  38. end},
  39. {"zero",
  40. fun () ->
  41. ?assertEqual({ok, 0, <<"">>}, jsone_decode:decode(<<"0">>))
  42. end},
  43. {"negative integer",
  44. fun () ->
  45. ?assertEqual({ok, -1, <<"">>}, jsone_decode:decode(<<"-1">>))
  46. end},
  47. {"large integer (no limit on size)",
  48. fun () ->
  49. ?assertEqual({ok, 111111111111111111111111111111111111111111111111111111111111111111111111111111, <<"">>},
  50. jsone_decode:decode(<<"111111111111111111111111111111111111111111111111111111111111111111111111111111">>))
  51. end},
  52. {"integer with leading zero (interpreted as zero and remaining binary)",
  53. fun () ->
  54. ?assertEqual({ok, 0, <<"0">>}, jsone_decode:decode(<<"00">>)),
  55. ?assertEqual({ok, 0, <<"1">>}, jsone_decode:decode(<<"01">>)),
  56. ?assertEqual({ok, 0, <<"1">>}, jsone_decode:decode(<<"-01">>))
  57. end},
  58. {"integer can't begin with an explicit plus sign",
  59. fun () ->
  60. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(<<"+1">>))
  61. end},
  62. %% Numbers: Floats
  63. {"float: decimal notation",
  64. fun () ->
  65. ?assertEqual({ok, 1.23, <<"">>}, jsone_decode:decode(<<"1.23">>))
  66. end},
  67. {"float: exponential notation",
  68. fun () ->
  69. ?assertEqual({ok, 12.345, <<"">>}, jsone_decode:decode(<<"12345e-3">>)), % lower case 'e'
  70. ?assertEqual({ok, 12.345, <<"">>}, jsone_decode:decode(<<"12345E-3">>)), % upper case 'E'
  71. ?assertEqual({ok, 12.345, <<"">>}, jsone_decode:decode(<<"12345.0e-3">>)),
  72. ?assertEqual({ok, 12.345, <<"">>}, jsone_decode:decode(<<"0.12345E2">>)),
  73. ?assertEqual({ok, 12.345, <<"">>}, jsone_decode:decode(<<"0.12345e+2">>)), % exponent part can begin with plus sign
  74. ?assertEqual({ok, 12.345, <<"">>}, jsone_decode:decode(<<"0.12345E+2">>)),
  75. ?assertEqual({ok, -12.345, <<"">>}, jsone_decode:decode(<<"-0.012345e3">>))
  76. end},
  77. {"float: invalid format",
  78. fun () ->
  79. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(<<".123">>)), % omitted integer part
  80. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(<<"0.">>)), % omitted fraction part: EOS
  81. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(<<"0.e+3">>)), % omitted fraction part: with exponent part
  82. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(<<"0.1e">>)), % imcomplete fraction part
  83. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(<<"0.1e-">>)), % imcomplete fraction part
  84. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(<<"0.1ee-1">>)), % duplicated 'e'
  85. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(<<"0.1e--1">>)), % duplicated sign
  86. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(<<"1e999">>)), % exponent out of range
  87. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(<<"0.1e999">>)), % exponent out of range
  88. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(<<"100000000000000000000000000000000000e300">>)), % product out of range
  89. ?assertEqual({ok, 0.1, <<".2">>}, jsone_decode:decode(<<"0.1.2">>)) % duplicated '.': interpreted as individual tokens
  90. end},
  91. %% Strings
  92. {"simple string",
  93. fun () ->
  94. ?assertEqual({ok, <<"abc">>, <<"">>}, jsone_decode:decode(<<"\"abc\"">>))
  95. end},
  96. {"string: escaped characters",
  97. fun () ->
  98. Input = list_to_binary([$", [[$\\, C] || C <- [$", $/, $\\, $b, $f, $n, $r, $t]], $"]),
  99. Expected = <<"\"\/\\\b\f\n\r\t">>,
  100. ?assertEqual({ok, Expected, <<"">>}, jsone_decode:decode(Input))
  101. end},
  102. {"string: escaped Unicode characters",
  103. fun () ->
  104. %% japanese
  105. Input1 = <<"\"\\u3042\\u3044\\u3046\\u3048\\u304A\"">>,
  106. Expected1 = <<"あいうえお">>, % assumed that the encoding of this file is UTF-8
  107. ?assertEqual({ok, Expected1, <<"">>}, jsone_decode:decode(Input1)),
  108. %% ascii
  109. Input2 = <<"\"\\u0061\\u0062\\u0063\"">>,
  110. Expected2 = <<"abc">>,
  111. ?assertEqual({ok, Expected2, <<"">>}, jsone_decode:decode(Input2)),
  112. %% other multi-byte characters
  113. Input3 = <<"\"\\u06DD\\u06DE\\u10AE\\u10AF\"">>,
  114. Expected3 = <<"۝۞ႮႯ">>,
  115. ?assertEqual({ok, Expected3, <<"">>}, jsone_decode:decode(Input3)),
  116. %% mixture of ascii and japanese characters
  117. Input4 = <<"\"a\\u30421\\u3044bb\\u304622\\u3048ccc\\u304A333\"">>,
  118. Expected4 = <<"aあ1いbbう22えcccお333">>, % assumed that the encoding of this file is UTF-8
  119. ?assertEqual({ok, Expected4, <<"">>}, jsone_decode:decode(Input4))
  120. end},
  121. {"string: surrogate pairs",
  122. fun () ->
  123. Input = <<"\"\\ud848\\udc49\\ud848\\udc9a\\ud848\\udcfc\"">>,
  124. Expected = <<"𢁉𢂚𢃼">>,
  125. ?assertEqual({ok, Expected, <<"">>}, jsone_decode:decode(Input))
  126. end},
  127. {"string: control characters",
  128. fun () ->
  129. Ctrls = lists:seq(0, 16#1f),
  130. lists:foreach(
  131. fun (C) ->
  132. %% Control characters are unacceptable
  133. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(<<$", C, $">>))
  134. end,
  135. Ctrls),
  136. lists:foreach(
  137. fun (C) ->
  138. %% `allow_ctrl_chars' option allows strings which contain unescaped control characters
  139. ?assertEqual({ok, <<C>>, <<"">>}, jsone_decode:decode(<<$", C, $">>, [{allow_ctrl_chars, true}]))
  140. end,
  141. Ctrls)
  142. end},
  143. {"string: invalid escape characters",
  144. fun () ->
  145. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(<<"\"\\z\"">>)), % '\z' is undefined
  146. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(<<"\"\\uab\"">>)), % too few hex characters
  147. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(<<"\"\\ud848\"">>)), % high(first) surrogate only
  148. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(<<"\"\\udc49\"">>)), % low(second) surrogate only
  149. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(<<"\"\\ud848\\u0061\"">>)), % missing low(second) surrogate
  150. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(<<"\"\\udf0u\"">>)),
  151. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(<<"\"\\ud848\\udf0u\"">>)),
  152. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(<<"\"\\u-3351\"">>))
  153. end},
  154. %% Arrays
  155. {"simple array",
  156. fun () ->
  157. Input = <<"[1,2,\"abc\",null]">>,
  158. Expected = [1, 2, <<"abc">>, null],
  159. ?assertEqual({ok, Expected, <<"">>}, jsone_decode:decode(Input))
  160. end},
  161. {"array: contains whitespaces",
  162. fun () ->
  163. Input = <<"[ 1,\t2, \n \"abc\",\r null]">>,
  164. Expected = [1, 2, <<"abc">>, null],
  165. ?assertEqual({ok, Expected, <<"">>}, jsone_decode:decode(Input))
  166. end},
  167. {"empty array",
  168. fun () ->
  169. ?assertEqual({ok, [], <<"">>}, jsone_decode:decode(<<"[]">>)),
  170. ?assertEqual({ok, [], <<"">>}, jsone_decode:decode(<<"[ \t\r\n]">>))
  171. end},
  172. {"array: trailing comma is disallowed",
  173. fun () ->
  174. Input = <<"[1, 2, \"abc\", null, ]">>,
  175. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(Input))
  176. end},
  177. {"array: missing comma",
  178. fun () ->
  179. Input = <<"[1 2, \"abc\", null]">>, % a missing comma between '1' and '2'
  180. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(Input))
  181. end},
  182. {"array: missing closing bracket",
  183. fun () ->
  184. Input = <<"[1, 2, \"abc\", null">>,
  185. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(Input))
  186. end},
  187. %% Objects
  188. {"simple object",
  189. fun () ->
  190. Input = <<"{\"1\":2,\"key\":\"value\"}">>,
  191. Expected = ?OBJ2(<<"1">>, 2, <<"key">>, <<"value">>),
  192. ?assertEqual({ok, Expected, <<"">>}, jsone_decode:decode(Input)), % `map' is the default format
  193. ?assertEqual({ok, Expected, <<"">>}, jsone_decode:decode(Input, [{object_format, ?MAP_OBJECT_TYPE}]))
  194. end},
  195. {"simple object: tuple or proplist",
  196. fun () ->
  197. Input = <<"{\"1\":2,\"key\":\"value\"}">>,
  198. Expected = {[{<<"1">>, 2},{<<"key">>, <<"value">>}]},
  199. ?assertEqual({ok, Expected, <<"">>}, jsone_decode:decode(Input, [{object_format, tuple}])),
  200. ?assertEqual({ok, element(1, Expected), <<"">>}, jsone_decode:decode(Input, [{object_format, proplist}]))
  201. end},
  202. {"object: contains whitespaces",
  203. fun () ->
  204. Input = <<"{ \"1\" :\t 2,\n\r\"key\" : \n \"value\"}">>,
  205. Expected = ?OBJ2(<<"1">>, 2, <<"key">>, <<"value">>),
  206. ?assertEqual({ok, Expected, <<"">>}, jsone_decode:decode(Input))
  207. end},
  208. {"empty object",
  209. fun () ->
  210. ?assertEqual({ok, ?OBJ0, <<"">>}, jsone_decode:decode(<<"{}">>)),
  211. ?assertEqual({ok, ?OBJ0, <<"">>}, jsone_decode:decode(<<"{ \t\r\n}">>)),
  212. ?assertEqual({ok, {[]}, <<"">>}, jsone_decode:decode(<<"{}">>, [{object_format, tuple}])),
  213. ?assertEqual({ok, [{}], <<"">>}, jsone_decode:decode(<<"{}">>, [{object_format, proplist}]))
  214. end},
  215. {"empty object: map",
  216. fun () ->
  217. ?assertEqual({ok, ?OBJ0, <<"">>}, jsone_decode:decode(<<"{}">>, [{object_format, ?MAP_OBJECT_TYPE}]))
  218. end},
  219. {"duplicated members: map",
  220. fun () ->
  221. Input = <<"{\"1\":\"first\",\"1\":\"second\"}">>,
  222. Expected = ?OBJ2_DUP_KEY(<<"1">>, <<"first">>, <<"1">>, <<"second">>),
  223. ?assertEqual({ok, Expected, <<"">>}, jsone_decode:decode(Input, [{object_format, ?MAP_OBJECT_TYPE}]))
  224. end},
  225. {"duplicated members last: map",
  226. fun () ->
  227. Input = <<"{\"1\":\"first\",\"1\":\"second\"}">>,
  228. Expected = ?OBJ2_DUP_KEY_LAST(<<"1">>, <<"first">>, <<"1">>, <<"second">>),
  229. ?assertEqual({ok, Expected, <<"">>}, jsone_decode:decode(Input,
  230. [{object_format, ?MAP_OBJECT_TYPE},
  231. {duplicate_map_keys, last}]))
  232. end},
  233. {"object: trailing comma is disallowed",
  234. fun () ->
  235. Input = <<"{\"1\":2, \"key\":\"value\", }">>,
  236. io:format("~p\n", [catch jsone_decode:decode(Input)]),
  237. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(Input, [{object_format, tuple}]))
  238. end},
  239. {"object: missing comma",
  240. fun () ->
  241. Input = <<"{\"1\":2 \"key\":\"value\"}">>,
  242. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(Input))
  243. end},
  244. {"object: missing field key",
  245. fun () ->
  246. Input = <<"{:2, \"key\":\"value\"}">>,
  247. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(Input))
  248. end},
  249. {"object: non string key",
  250. fun () ->
  251. Input = <<"{1:2, \"key\":\"value\"}">>,
  252. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(Input))
  253. end},
  254. {"object: missing field value",
  255. fun () ->
  256. Input = <<"{\"1\", \"key\":\"value\"}">>,
  257. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(Input))
  258. end},
  259. {"object: missing closing brace",
  260. fun () ->
  261. Input = <<"{\"1\":2 \"key\":\"value\"">>,
  262. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(Input))
  263. end},
  264. {"atom keys",
  265. fun () ->
  266. KeyOpt = fun(Keys) -> [{keys, Keys}, {object_format, proplist}]
  267. end,
  268. Input = <<"{\"foo\":\"ok\"}">>,
  269. ?assertEqual([{<<"foo">>, <<"ok">>}], jsone:decode(Input, KeyOpt(binary))),
  270. ?assertEqual([{foo, <<"ok">>}], jsone:decode(Input, KeyOpt(atom))),
  271. ?assertEqual([{foo, <<"ok">>}], jsone:decode(Input, KeyOpt(existing_atom))),
  272. ?assertError(badarg, jsone:decode(<<"{\"@#$%^!\":\"ok\"}">>, KeyOpt(existing_atom))),
  273. ?assertEqual([{foo, <<"ok">>}], jsone:decode(Input, KeyOpt(attempt_atom))),
  274. ?assertEqual([{<<"@#$%^!">>, <<"ok">>}], jsone:decode(<<"{\"@#$%^!\":\"ok\"}">>, KeyOpt(attempt_atom))),
  275. Value = integer_to_binary(1234),
  276. % do not make atom in test code
  277. [{Atom, <<"ok">>}] = jsone:decode(<<"{\"", Value/binary, "\":\"ok\"}">>, KeyOpt(atom)),
  278. ?assertEqual(Value, atom_to_binary(Atom, latin1))
  279. end},
  280. %% Others
  281. {"compound data",
  282. fun () ->
  283. Input = <<" [true, {\"1\" : 2, \"array\":[[[[1]]], {\"ab\":\"cd\"}, false]}, null] ">>,
  284. Expected = [true, ?OBJ2(<<"1">>, 2, <<"array">>, [[[[1]]], ?OBJ1(<<"ab">>, <<"cd">>), false]), null],
  285. ?assertEqual({ok, Expected, <<" ">>}, jsone_decode:decode(Input))
  286. end},
  287. {"undefined_as_null option",
  288. fun() ->
  289. ?assertEqual({ok, undefined, <<>>}, jsone_decode:decode(<<"null">>,[undefined_as_null])), % OK
  290. ?assertEqual({ok, null, <<>>}, jsone_decode:decode(<<"null">>,[])) % OK
  291. end},
  292. {"Invalid UTF-8 characters",
  293. fun () ->
  294. Input = <<123,34,105,100,34,58,34,190,72,94,90,253,121,94,71,73,68,91,122,211,253,32,94,86,67,163,253,230,34,125>>,
  295. ?assertMatch({ok, _, _}, jsone:try_decode(Input)),
  296. ?assertMatch({error, {badarg, _}}, jsone:try_decode(Input, [reject_invalid_utf8]))
  297. end}
  298. ].