25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

97 satır
3.7 KiB

5 yıl önce
5 yıl önce
5 yıl önce
5 yıl önce
5 yıl önce
5 yıl önce
5 yıl önce
5 yıl önce
  1. % This file is part of Jiffy released under the MIT license.
  2. % See the LICENSE file for more information.
  3. -module(jiffy_18_partials_tests).
  4. -include_lib("eunit/include/eunit.hrl").
  5. decode_levels_test_() ->
  6. MaxOptMaxLevels = 4,
  7. {"Test max_levels", lists:map(fun(Json) ->
  8. [begin
  9. EJson = jiffy:decode(Json, [{max_levels, MaxLevels} | Opts]),
  10. FullEJson = to_full_json(EJson, MaxLevels, Opts),
  11. ?_assertEqual(jiffy:decode(Json, Opts), FullEJson)
  12. end || MaxLevels <- lists:seq(1, MaxOptMaxLevels), Opts <- generate_options_groups()]
  13. end, jsons())}.
  14. encode_resources_test_() ->
  15. {"Test encode resources", lists:map(fun(Json) ->
  16. [begin
  17. EJsonWithResources = jiffy:decode(Json, [{max_levels, 1} | Opts]),
  18. JsonFromResources = jiffy:encode(EJsonWithResources),
  19. ?_assertEqual(jiffy:decode(Json, Opts), jiffy:decode(JsonFromResources, Opts))
  20. end || Opts <- generate_options_groups()]
  21. end, jsons())}.
  22. encode_partials_test_() ->
  23. {"Test encode partials", lists:map(fun(Json) ->
  24. [begin
  25. EJson = jiffy:decode(Json, Opts),
  26. PartialResource = jiffy:encode(EJson, [partial]),
  27. true = is_reference(PartialResource),
  28. PartialIOData = jiffy:encode(PartialResource),
  29. ?_assertEqual(EJson, jiffy:decode(PartialIOData, Opts))
  30. end || Opts <- generate_options_groups()]
  31. end, jsons())}.
  32. jsons() ->
  33. [
  34. <<"{\"foo\":\"bar\"}">>,
  35. <<"{\"foo\":[\"bar\"]}">>,
  36. <<"[[[[]],\"foo\"], [\"bar\", []], [\"baz\"], [[], 1]]">>,
  37. <<"{\"foo\":{},\"bar\":{\"baz\":[1,2,3], \"foo2\":{}}}">>
  38. ].
  39. -ifndef(JIFFY_NO_MAPS).
  40. generate_options_groups() -> generate_options_groups([copy_strings, return_maps]).
  41. -else.
  42. generate_options_groups() -> generate_options_groups([copy_strings]).
  43. -endif.
  44. generate_options_groups(AvailableOptions) ->
  45. generate_options_groups(AvailableOptions, [[]]).
  46. generate_options_groups([], Acc) ->
  47. Acc;
  48. generate_options_groups([Option | AvailableOptions], Acc) ->
  49. generate_options_groups(AvailableOptions, [[Option | Group] || Group <- Acc] ++ Acc).
  50. to_full_json(Val, MaxDepth, DecodeOptions) ->
  51. to_full_json(Val, 0, MaxDepth, DecodeOptions).
  52. to_full_json(_Val, Depth, MaxDepth, _DecodeOptions) when Depth > MaxDepth ->
  53. error(too_deep);
  54. to_full_json(PartialResource, Depth, MaxDepth, DecodeOptions) when is_reference(PartialResource) ->
  55. MaxDepth = Depth,
  56. IOData = jiffy:encode(PartialResource),
  57. [begin
  58. ByteSize = byte_size(ValueBin),
  59. case lists:member(copy_strings, DecodeOptions) of
  60. true ->
  61. ByteSize = binary:referenced_byte_size(ValueBin);
  62. _ ->
  63. % With small binaries, the copies between environments involve a
  64. % full copy if the binary is small enough (thus the =)
  65. true = ByteSize =< binary:referenced_byte_size(ValueBin)
  66. end
  67. end || ValueBin <- lists:flatten(IOData)],
  68. jiffy:decode(IOData, DecodeOptions);
  69. to_full_json({Pairs}, Depth, MaxDepth, DecodeOptions) when is_list(Pairs) ->
  70. {[{K, to_full_json(V, Depth+1, MaxDepth, DecodeOptions)} || {K, V} <- Pairs]};
  71. to_full_json(Vals, Depth, MaxDepth, DecodeOptions) when is_list(Vals) ->
  72. [to_full_json(V, Depth+1, MaxDepth, DecodeOptions) || V <- Vals];
  73. to_full_json(Val, Depth, MaxDepth, DecodeOptions) ->
  74. maybe_map(Val, Depth, MaxDepth, DecodeOptions).
  75. -ifndef(JIFFY_NO_MAPS).
  76. maybe_map(Obj, Depth, MaxDepth, DecodeOptions) when is_map(Obj) ->
  77. maps:map(fun(_K, V) -> to_full_json(V, Depth+1, MaxDepth, DecodeOptions) end, Obj);
  78. maybe_map(Val, _Depth, _MaxDepth, _DecodeOptions) ->
  79. Val.
  80. -else.
  81. maybe_map(Val, _Depth, _MaxDepth, _DecodeOptions) ->
  82. Val.
  83. -endif.