jsone_encode.erl 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. %%% @doc JSON encoding module
  2. %%% @private
  3. %%% @end
  4. %%%
  5. %%% Copyright (c) 2013-2016, Takeru Ohta <phjgt308@gmail.com>
  6. %%%
  7. %%% The MIT License
  8. %%%
  9. %%% Permission is hereby granted, free of charge, to any person obtaining a copy
  10. %%% of this software and associated documentation files (the "Software"), to deal
  11. %%% in the Software without restriction, including without limitation the rights
  12. %%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. %%% copies of the Software, and to permit persons to whom the Software is
  14. %%% furnished to do so, subject to the following conditions:
  15. %%%
  16. %%% The above copyright notice and this permission notice shall be included in
  17. %%% all copies or substantial portions of the Software.
  18. %%%
  19. %%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. %%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. %%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. %%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. %%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. %%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. %%% THE SOFTWARE.
  26. %%%
  27. %%%---------------------------------------------------------------------------------------
  28. -module(jsone_encode).
  29. -ifdef(ENABLE_HIPE).
  30. -compile([native, {hipe, [o3]}]).
  31. -endif.
  32. %%--------------------------------------------------------------------------------
  33. %% Exported API
  34. %%--------------------------------------------------------------------------------
  35. -export([encode/1, encode/2]).
  36. %%--------------------------------------------------------------------------------
  37. %% Macros & Records & Types
  38. %%--------------------------------------------------------------------------------
  39. -define(ERROR(Function, Args), {error, {badarg, [{?MODULE, Function, Args, [{line, ?LINE}]}]}}).
  40. -define(IS_STR(X), (is_binary(X) orelse is_atom(X))).
  41. -define(IS_UINT(X), (is_integer(X) andalso X >= 0)).
  42. -define(IS_PNUM(X), (is_number(X) andalso X >=0)).
  43. -define(IS_DATETIME(Y,M,D,H,Mi,S), (?IS_UINT(Y) andalso ?IS_UINT(M) andalso ?IS_UINT(D) andalso
  44. ?IS_UINT(H) andalso ?IS_UINT(Mi) andalso
  45. ?IS_PNUM(S))).
  46. -ifdef('NO_MAP_TYPE').
  47. -define(IS_MAP(X), is_tuple(X)).
  48. -define(ENCODE_MAP(Value, Nexts, Buf, Opt), ?ERROR(value, [Value, Nexts, Buf, Opt])).
  49. -else.
  50. -define(IS_MAP(X), is_map(X)).
  51. -define(ENCODE_MAP(Value, Nexts, Buf, Opt), object(maps:to_list(Value), Nexts, Buf, Opt)).
  52. -endif.
  53. -type encode_result() :: {ok, binary()} | {error, {Reason::term(), [jsone:stack_item()]}}.
  54. -type next() :: {array_values, [jsone:json_value()]}
  55. | {object_value, jsone:json_value(), jsone:json_object_members()}
  56. | {object_members, jsone:json_object_members()}
  57. | {char, binary()}.
  58. -record(encode_opt_v2, {
  59. native_utf8 = false :: boolean(),
  60. native_forward_slash = false :: boolean(),
  61. canonical_form = false :: boolean(),
  62. float_format = [{scientific, 20}] :: [jsone:float_format_option()],
  63. datetime_format = {iso8601, 0} :: {jsone:datetime_format(), jsone:utc_offset_seconds()},
  64. object_key_type = string :: string | scalar | value,
  65. space = 0 :: non_neg_integer(),
  66. indent = 0 :: non_neg_integer(),
  67. undefined_as_null = false :: boolean(),
  68. skip_undefined = false :: boolean(),
  69. map_unknown_value = fun jsone:term_to_json_string/1 :: undefined | fun ((term()) -> {ok, jsone:json_value()} | error)
  70. }).
  71. -define(OPT, #encode_opt_v2).
  72. -type opt() :: #encode_opt_v2{}.
  73. %%--------------------------------------------------------------------------------
  74. %% Exported Functions
  75. %%--------------------------------------------------------------------------------
  76. -spec encode(jsone:json_value()) -> encode_result().
  77. encode(Value) ->
  78. encode(Value, []).
  79. -spec encode(jsone:json_value(), [jsone:encode_option()]) -> encode_result().
  80. encode(Value, Options) ->
  81. Opt = parse_options(Options),
  82. value(Value, [], <<"">>, Opt).
  83. %%--------------------------------------------------------------------------------
  84. %% Internal Functions
  85. %%--------------------------------------------------------------------------------
  86. -spec next([next()], binary(), opt()) -> encode_result().
  87. next([], Buf, _) -> {ok, Buf};
  88. next(Level = [Next | Nexts], Buf, Opt) ->
  89. case Next of
  90. {array_values, Values} ->
  91. case Values of
  92. [] -> array_values(Values, Nexts, Buf, Opt);
  93. _ -> array_values(Values, Nexts, pp_newline_or_space(<<Buf/binary, $,>>, Level, Opt), Opt)
  94. end;
  95. {object_value, Value, Members} ->
  96. object_value(Value, Members, Nexts, pp_space(<<Buf/binary, $:>>, Opt), Opt);
  97. {object_members, Members} ->
  98. case Members of
  99. [] -> object_members(Members, Nexts, Buf, Opt);
  100. _ -> object_members(Members, Nexts, pp_newline_or_space(<<Buf/binary, $,>>, Level, Opt), Opt)
  101. end;
  102. {char, C} ->
  103. next(Nexts, <<Buf/binary, C>>, Opt)
  104. end.
  105. -spec value(jsone:json_value(), [next()], binary(), opt()) -> encode_result().
  106. value(null, Nexts, Buf, Opt) -> next(Nexts, <<Buf/binary, "null">>, Opt);
  107. value(undefined, Nexts, Buf, Opt = ?OPT{undefined_as_null = true}) -> next(Nexts, <<Buf/binary, "null">>, Opt);
  108. value(false, Nexts, Buf, Opt) -> next(Nexts, <<Buf/binary, "false">>, Opt);
  109. value(true, Nexts, Buf, Opt) -> next(Nexts, <<Buf/binary, "true">>, Opt);
  110. value({{json, T}}, Nexts, Buf, Opt) ->
  111. try
  112. next(Nexts, <<Buf/binary, (iolist_to_binary(T))/binary>>, Opt)
  113. catch
  114. error:badarg ->
  115. ?ERROR(value, [{json, T}, Nexts, Buf, Opt])
  116. end;
  117. value({{json_utf8, T}}, Nexts, Buf, Opt) ->
  118. try unicode:characters_to_binary(T) of
  119. {error, OK, Invalid} ->
  120. {error, {{invalid_json_utf8, OK, Invalid}, [{?MODULE, value, [{json_utf8, T}, Nexts, Buf, Opt], [{line, ?LINE}]}]}};
  121. B when is_binary(B) ->
  122. next(Nexts, <<Buf/binary, B/binary>>, Opt)
  123. catch
  124. error:badarg ->
  125. ?ERROR(value, [{json_utf8, T}, Nexts, Buf, Opt])
  126. end;
  127. value(Value, Nexts, Buf, Opt) when is_integer(Value) -> next(Nexts, <<Buf/binary, (integer_to_binary(Value))/binary>>, Opt);
  128. value(Value, Nexts, Buf, Opt) when is_float(Value) -> next(Nexts, <<Buf/binary, (float_to_binary(Value, Opt?OPT.float_format))/binary>>, Opt);
  129. value(Value, Nexts, Buf, Opt) when ?IS_STR(Value) -> string(Value, Nexts, Buf, Opt);
  130. value({{_,_,_},{_,_,_}} = Value, Nexts, Buf, Opt) -> datetime(Value, Nexts, Buf, Opt);
  131. value({Value}, Nexts, Buf, Opt) when is_list(Value) -> object(Value, Nexts, Buf, Opt);
  132. value([{}], Nexts, Buf, Opt) -> object([], Nexts, Buf, Opt);
  133. value([{{_,_,_},{_,_,_}}|_] = Value, Nexts, Buf, Opt)-> array(Value, Nexts, Buf, Opt);
  134. value([{_, _}|_] = Value, Nexts, Buf, Opt) -> object(Value, Nexts, Buf, Opt);
  135. value(Value, Nexts, Buf, Opt) when ?IS_MAP(Value) -> ?ENCODE_MAP(Value, Nexts, Buf, Opt);
  136. value(Value, Nexts, Buf, Opt) when is_list(Value) -> array(Value, Nexts, Buf, Opt);
  137. value(Value, Nexts, Buf, Opt) ->
  138. case Opt?OPT.map_unknown_value of
  139. undefined -> ?ERROR(value, [Value, Nexts, Buf, Opt]);
  140. Fun ->
  141. case Fun(Value) of
  142. error -> ?ERROR(value, [Value, Nexts, Buf, Opt]);
  143. {ok, NewValue} -> value(NewValue, Nexts, Buf, Opt)
  144. end
  145. end.
  146. -spec string(jsone:json_string(), [next()], binary(), opt()) -> encode_result().
  147. string(<<Str/binary>>, Nexts, Buf, Opt) ->
  148. Len = unescaped_string_length(Str, Opt),
  149. <<Unescaped:Len/binary, Rest/binary>> = Str,
  150. escape_string(Rest, Nexts, <<Buf/binary, $", Unescaped/binary>>, Opt);
  151. string(Str, Nexts, Buf, Opt) ->
  152. string(atom_to_binary(Str, utf8), Nexts, Buf, Opt).
  153. -spec datetime(calendar:datetime(), [next()], binary(), opt()) -> encode_result().
  154. datetime({{Y,M,D}, {H,Mi,S}}, Nexts, Buf, Opt) when ?IS_DATETIME(Y,M,D,H,Mi,S) ->
  155. {iso8601, Tz} = Opt?OPT.datetime_format,
  156. Str =
  157. [format_year(Y), $-, format2digit(M), $-, format2digit(D), $T,
  158. format2digit(H), $:, format2digit(Mi), $:, format_seconds(S),
  159. format_tz(Tz)],
  160. next(Nexts, <<Buf/binary, $", (list_to_binary(Str))/binary, $">>, Opt);
  161. datetime(Datetime, Nexts, Buf, Opt) ->
  162. ?ERROR(datetime, [Datetime, Nexts, Buf, Opt]).
  163. -ifndef(NO_DIALYZER_SPEC).
  164. -dialyzer({no_improper_lists, [format_year/1]}).
  165. -endif.
  166. -spec format_year(non_neg_integer()) -> iodata().
  167. format_year(Y) when Y > 999 -> integer_to_binary(Y);
  168. format_year(Y) ->
  169. B = integer_to_binary(Y),
  170. [lists:duplicate(4-byte_size(B), $0)|B].
  171. -spec format2digit(non_neg_integer()) -> iolist().
  172. format2digit(0) -> "00";
  173. format2digit(1) -> "01";
  174. format2digit(2) -> "02";
  175. format2digit(3) -> "03";
  176. format2digit(4) -> "04";
  177. format2digit(5) -> "05";
  178. format2digit(6) -> "06";
  179. format2digit(7) -> "07";
  180. format2digit(8) -> "08";
  181. format2digit(9) -> "09";
  182. format2digit(X) -> integer_to_list(X).
  183. -spec format_seconds(non_neg_integer() | float()) -> iolist().
  184. format_seconds(S) when is_integer(S) -> format2digit(S);
  185. format_seconds(S) when is_float(S) -> io_lib:format("~6.3.0f", [S]).
  186. -spec format_tz(integer()) -> byte() | iolist().
  187. format_tz(0) -> $Z;
  188. format_tz(Tz) when Tz > 0 -> [$+|format_tz_(Tz)];
  189. format_tz(Tz) -> [$-|format_tz_(-Tz)].
  190. -define(SECONDS_PER_MINUTE, 60).
  191. -define(SECONDS_PER_HOUR, 3600).
  192. -spec format_tz_(integer()) -> iolist().
  193. format_tz_(S) ->
  194. H = S div ?SECONDS_PER_HOUR,
  195. S1 = S rem ?SECONDS_PER_HOUR,
  196. M = S1 div ?SECONDS_PER_MINUTE,
  197. [format2digit(H), $:, format2digit(M)].
  198. -spec object_key(jsone:json_value(), [next()], binary(), opt()) -> encode_result().
  199. object_key(Key, Nexts, Buf, Opt) when ?IS_STR(Key) ->
  200. string(Key, Nexts, Buf, Opt);
  201. object_key(Key, Nexts, Buf, Opt = ?OPT{object_key_type = scalar}) when is_number(Key) ->
  202. value(Key, [{char, $"} | Nexts], <<Buf/binary, $">>, Opt);
  203. object_key(Key = {{Y,M,D},{H,Mi,S}}, Nexts, Buf, Opt = ?OPT{object_key_type = Type}) when ?IS_DATETIME(Y,M,D,H,Mi,S), Type =/= string ->
  204. value(Key, Nexts, Buf, Opt);
  205. object_key(Key, Nexts, Buf, Opt = ?OPT{object_key_type = value}) ->
  206. case value(Key, [], <<>>, Opt) of
  207. {error, Reason} -> {error, Reason};
  208. {ok, BinaryKey} -> string(BinaryKey, Nexts, Buf, Opt)
  209. end;
  210. object_key(Key, Nexts, Buf, Opt) ->
  211. ?ERROR(object_key, [Key, Nexts, Buf, Opt]).
  212. -define(H8(X), (hex(X)):16).
  213. -define(H16(X), ?H8(X bsr 8), ?H8(X band 16#FF)).
  214. -ifdef(ENABLE_HIPE).
  215. -define(COPY_UTF8,
  216. escape_string(<<2#110:3, C1:5, C2, Str/binary>>, Nexts, Buf, Opt) ->
  217. escape_string(Str, Nexts, <<Buf/binary, (2#11000000+C1), C2>>, Opt);
  218. escape_string(<<2#1110:4, C1:4, C2:16, Str/binary>>, Nexts, Buf, Opt) ->
  219. escape_string(Str, Nexts, <<Buf/binary, (2#11100000+C1), C2:16>>, Opt);
  220. escape_string(<<2#11110:5, C1:3, C2:24, Str/binary>>, Nexts, Buf, Opt) ->
  221. escape_string(Str, Nexts, <<Buf/binary, (2#11110000+C1), C2:24>>, Opt)
  222. ).
  223. -else.
  224. -define(COPY_UTF8,
  225. escape_string(<<Ch/utf8, Str/binary>>, Nexts, Buf, Opt) ->
  226. escape_string(Str, Nexts, <<Buf/binary, Ch/utf8>>, Opt)
  227. ).
  228. -endif.
  229. -spec escape_string(binary(), [next()], binary(), opt()) -> encode_result().
  230. escape_string(<<"">>, Nexts, Buf, Opt) -> next(Nexts, <<Buf/binary, $">>, Opt);
  231. escape_string(<<$", Str/binary>>, Nexts, Buf, Opt) -> escape_string(Str, Nexts, <<Buf/binary, $\\, $">>, Opt);
  232. escape_string(<<$\\, Str/binary>>, Nexts, Buf, Opt) -> escape_string(Str, Nexts, <<Buf/binary, $\\, $\\>>, Opt);
  233. escape_string(<<$\b, Str/binary>>, Nexts, Buf, Opt) -> escape_string(Str, Nexts, <<Buf/binary, $\\, $b>>, Opt);
  234. escape_string(<<$\f, Str/binary>>, Nexts, Buf, Opt) -> escape_string(Str, Nexts, <<Buf/binary, $\\, $f>>, Opt);
  235. escape_string(<<$\n, Str/binary>>, Nexts, Buf, Opt) -> escape_string(Str, Nexts, <<Buf/binary, $\\, $n>>, Opt);
  236. escape_string(<<$\r, Str/binary>>, Nexts, Buf, Opt) -> escape_string(Str, Nexts, <<Buf/binary, $\\, $r>>, Opt);
  237. escape_string(<<$\t, Str/binary>>, Nexts, Buf, Opt) -> escape_string(Str, Nexts, <<Buf/binary, $\\, $t>>, Opt);
  238. escape_string(<<$\/, Str/binary>>, Nexts, Buf, Opt) when not Opt?OPT.native_forward_slash ->
  239. escape_string(Str, Nexts, <<Buf/binary, $\\, $\/>>, Opt);
  240. escape_string(<<0:1, C:7, Str/binary>>, Nexts, Buf, Opt) ->
  241. case C < 16#20 of
  242. true -> escape_string(Str, Nexts, <<Buf/binary, "\\u00", ?H8(C)>>, Opt);
  243. false ->
  244. Len = unescaped_string_length(Str, Opt),
  245. <<Unescaped:Len/binary, Rest/binary>> = Str,
  246. escape_string(Rest, Nexts, <<Buf/binary, C, Unescaped/binary>>, Opt)
  247. end;
  248. escape_string(<<Ch/utf8, Str/binary>>, Nexts, Buf, Opt = ?OPT{native_utf8 = false}) ->
  249. if
  250. Ch =< 16#FFFF ->
  251. escape_string(Str, Nexts,<<Buf/binary, $\\, $u, ?H16(Ch)>>, Opt);
  252. true ->
  253. <<H1, H2, L1, L2>> = <<Ch/utf16>>,
  254. escape_string(Str, Nexts, <<Buf/binary, $\\, $u, ?H8(H1), ?H8(H2), $\\, $u, ?H8(L1), ?H8(L2)>>, Opt)
  255. end;
  256. ?COPY_UTF8;
  257. escape_string(Str, Nexts, Buf, Opt) ->
  258. ?ERROR(escape_string, [Str, Nexts, Buf, Opt]).
  259. -define(UNESCAPED_CHARS_WITH_SLASH,
  260. {false,false,false,false,false,false,false,false,false,false,false,false,
  261. false,false,false,false,false,false,false,false,false,false,false,
  262. false,false,false,false,false,false,false,false,false,true,true,false,
  263. true,true,true,true,true,true,true,true,true,true,true,true,true,true,
  264. true,true,true,true,true,true,true,true,true,true,true,true,true,true,
  265. true,true,true,true,true,true,true,true,true,true,true,true,true,true,
  266. true,true,true,true,true,true,true,true,true,true,true,true,true,true,
  267. true,false,true,true,true,true,true,true,true,true,true,true,true,true,
  268. true,true,true,true,true,true,true,true,true,true,true,true,true,true,
  269. true,true,true,true,true,true,true,true,false,false,false,false,false,
  270. false,false,false,false,false,false,false,false,false,false,false,
  271. false,false,false,false,false,false,false,false,false,false,false,
  272. false,false,false,false,false,false,false,false,false,false,false,
  273. false,false,false,false,false,false,false,false,false,false,false,
  274. false,false,false,false,false,false,false,false,false,false,false,
  275. false,false,false,false,false,false,false,false,false,false,false,
  276. false,false,false,false,false,false,false,false,false,false,false,
  277. false,false,false,false,false,false,false,false,false,false,false,
  278. false,false,false,false,false,false,false,false,false,false,false,
  279. false,false,false,false,false,false,false,false,false,false,false,
  280. false,false,false,false,false,false,false,false,false,false,false,
  281. false,false,false}).
  282. -define(UNESCAPED_CHARS_WITHOUT_SLASH,
  283. {false,false,false,false,false,false,false,false,false,false,false,false,
  284. false,false,false,false,false,false,false,false,false,false,false,
  285. false,false,false,false,false,false,false,false,false,true,true,false,
  286. true,true,true,true,true,true,true,true,true,true,true,true,false,true,
  287. true,true,true,true,true,true,true,true,true,true,true,true,true,true,
  288. true,true,true,true,true,true,true,true,true,true,true,true,true,true,
  289. true,true,true,true,true,true,true,true,true,true,true,true,true,true,
  290. true,false,true,true,true,true,true,true,true,true,true,true,true,true,
  291. true,true,true,true,true,true,true,true,true,true,true,true,true,true,
  292. true,true,true,true,true,true,true,true,false,false,false,false,false,
  293. false,false,false,false,false,false,false,false,false,false,false,
  294. false,false,false,false,false,false,false,false,false,false,false,
  295. false,false,false,false,false,false,false,false,false,false,false,
  296. false,false,false,false,false,false,false,false,false,false,false,
  297. false,false,false,false,false,false,false,false,false,false,false,
  298. false,false,false,false,false,false,false,false,false,false,false,
  299. false,false,false,false,false,false,false,false,false,false,false,
  300. false,false,false,false,false,false,false,false,false,false,false,
  301. false,false,false,false,false,false,false,false,false,false,false,
  302. false,false,false,false,false,false,false,false,false,false,false,
  303. false,false,false,false,false,false,false,false,false,false,false,
  304. false,false,false}).
  305. -compile({inline, [unescaped_string_length/2]}).
  306. -spec unescaped_string_length(binary(), opt()) -> non_neg_integer().
  307. unescaped_string_length(Str, ?OPT{native_forward_slash = true}) ->
  308. unescaped_string_length(Str, 0, ?UNESCAPED_CHARS_WITH_SLASH);
  309. unescaped_string_length(Str, _) ->
  310. unescaped_string_length(Str, 0, ?UNESCAPED_CHARS_WITHOUT_SLASH).
  311. -spec unescaped_string_length(binary(), non_neg_integer(), tuple()) -> non_neg_integer().
  312. unescaped_string_length(<<C, Str/binary>>, N, Table) ->
  313. case element(C + 1, Table) of
  314. true -> unescaped_string_length(Str, N + 1, Table);
  315. false -> N
  316. end;
  317. unescaped_string_length(_, N, _) ->
  318. N.
  319. -compile({inline, [hex/1]}).
  320. -spec hex(byte()) -> 0..16#FFFF.
  321. hex(X) ->
  322. element(
  323. X+1,
  324. {16#3030, 16#3031, 16#3032, 16#3033, 16#3034, 16#3035, 16#3036, 16#3037,
  325. 16#3038, 16#3039, 16#3061, 16#3062, 16#3063, 16#3064, 16#3065, 16#3066,
  326. 16#3130, 16#3131, 16#3132, 16#3133, 16#3134, 16#3135, 16#3136, 16#3137,
  327. 16#3138, 16#3139, 16#3161, 16#3162, 16#3163, 16#3164, 16#3165, 16#3166,
  328. 16#3230, 16#3231, 16#3232, 16#3233, 16#3234, 16#3235, 16#3236, 16#3237,
  329. 16#3238, 16#3239, 16#3261, 16#3262, 16#3263, 16#3264, 16#3265, 16#3266,
  330. 16#3330, 16#3331, 16#3332, 16#3333, 16#3334, 16#3335, 16#3336, 16#3337,
  331. 16#3338, 16#3339, 16#3361, 16#3362, 16#3363, 16#3364, 16#3365, 16#3366,
  332. 16#3430, 16#3431, 16#3432, 16#3433, 16#3434, 16#3435, 16#3436, 16#3437,
  333. 16#3438, 16#3439, 16#3461, 16#3462, 16#3463, 16#3464, 16#3465, 16#3466,
  334. 16#3530, 16#3531, 16#3532, 16#3533, 16#3534, 16#3535, 16#3536, 16#3537,
  335. 16#3538, 16#3539, 16#3561, 16#3562, 16#3563, 16#3564, 16#3565, 16#3566,
  336. 16#3630, 16#3631, 16#3632, 16#3633, 16#3634, 16#3635, 16#3636, 16#3637,
  337. 16#3638, 16#3639, 16#3661, 16#3662, 16#3663, 16#3664, 16#3665, 16#3666,
  338. 16#3730, 16#3731, 16#3732, 16#3733, 16#3734, 16#3735, 16#3736, 16#3737,
  339. 16#3738, 16#3739, 16#3761, 16#3762, 16#3763, 16#3764, 16#3765, 16#3766,
  340. 16#3830, 16#3831, 16#3832, 16#3833, 16#3834, 16#3835, 16#3836, 16#3837,
  341. 16#3838, 16#3839, 16#3861, 16#3862, 16#3863, 16#3864, 16#3865, 16#3866,
  342. 16#3930, 16#3931, 16#3932, 16#3933, 16#3934, 16#3935, 16#3936, 16#3937,
  343. 16#3938, 16#3939, 16#3961, 16#3962, 16#3963, 16#3964, 16#3965, 16#3966,
  344. 16#6130, 16#6131, 16#6132, 16#6133, 16#6134, 16#6135, 16#6136, 16#6137,
  345. 16#6138, 16#6139, 16#6161, 16#6162, 16#6163, 16#6164, 16#6165, 16#6166,
  346. 16#6230, 16#6231, 16#6232, 16#6233, 16#6234, 16#6235, 16#6236, 16#6237,
  347. 16#6238, 16#6239, 16#6261, 16#6262, 16#6263, 16#6264, 16#6265, 16#6266,
  348. 16#6330, 16#6331, 16#6332, 16#6333, 16#6334, 16#6335, 16#6336, 16#6337,
  349. 16#6338, 16#6339, 16#6361, 16#6362, 16#6363, 16#6364, 16#6365, 16#6366,
  350. 16#6430, 16#6431, 16#6432, 16#6433, 16#6434, 16#6435, 16#6436, 16#6437,
  351. 16#6438, 16#6439, 16#6461, 16#6462, 16#6463, 16#6464, 16#6465, 16#6466,
  352. 16#6530, 16#6531, 16#6532, 16#6533, 16#6534, 16#6535, 16#6536, 16#6537,
  353. 16#6538, 16#6539, 16#6561, 16#6562, 16#6563, 16#6564, 16#6565, 16#6566,
  354. 16#6630, 16#6631, 16#6632, 16#6633, 16#6634, 16#6635, 16#6636, 16#6637,
  355. 16#6638, 16#6639, 16#6661, 16#6662, 16#6663, 16#6664, 16#6665, 16#6666}
  356. ).
  357. -spec array(jsone:json_array(), [next()], binary(), opt()) -> encode_result().
  358. array([], Nexts, Buf, Opt) ->
  359. next(Nexts, <<Buf/binary, $[, $]>>, Opt);
  360. array(List, Nexts, Buf, Opt) ->
  361. array_values(List, Nexts, pp_newline(<<Buf/binary, $[>>, Nexts, 1, Opt), Opt).
  362. -spec array_values(jsone:json_array(), [next()], binary(), opt()) -> encode_result().
  363. array_values([], Nexts, Buf, Opt) -> next(Nexts, <<(pp_newline(Buf, Nexts, Opt))/binary, $]>>, Opt);
  364. array_values([X | Xs], Nexts, Buf, Opt) -> value(X, [{array_values, Xs} | Nexts], Buf, Opt).
  365. -spec object(jsone:json_object_members(), [next()], binary(), opt()) -> encode_result().
  366. object(Members, Nexts, Buf, ?OPT{skip_undefined = true}=Opt) ->
  367. object1(lists:filter(fun ({_, V}) -> V =/= undefined end, Members), Nexts, Buf, Opt);
  368. object(Members, Nexts, Buf, Opt) ->
  369. object1(Members, Nexts, Buf, Opt).
  370. -spec object1(jsone:json_object_members(), [next()], binary(), opt()) -> encode_result().
  371. object1([], Nexts, Buf, Opt) ->
  372. next(Nexts, <<Buf/binary, ${, $}>>, Opt);
  373. object1(Members, Nexts, Buf, ?OPT{canonical_form = true}=Opt) ->
  374. object_members(lists:sort(Members), Nexts, pp_newline(<<Buf/binary, ${>>, Nexts, 1, Opt), Opt);
  375. object1(Members, Nexts, Buf, Opt) ->
  376. object_members(Members, Nexts, pp_newline(<<Buf/binary, ${>>, Nexts, 1, Opt), Opt).
  377. -spec object_members(jsone:json_object_members(), [next()], binary(), opt()) -> encode_result().
  378. object_members([], Nexts, Buf, Opt) ->
  379. next(Nexts, <<(pp_newline(Buf, Nexts, Opt))/binary, $}>>, Opt);
  380. object_members([{Key, Value} | Xs], Nexts, Buf, Opt) ->
  381. object_key(Key, [{object_value, Value, Xs} | Nexts], Buf, Opt);
  382. object_members(Arg, Nexts, Buf, Opt) ->
  383. ?ERROR(object_members, [Arg, Nexts, Buf, Opt]).
  384. -spec object_value(jsone:json_value(), jsone:json_object_members(), [next()], binary(), opt()) -> encode_result().
  385. object_value(Value, Members, Nexts, Buf, Opt) ->
  386. value(Value, [{object_members, Members} | Nexts], Buf, Opt).
  387. -spec pp_space(binary(), opt()) -> binary().
  388. pp_space(Buf, Opt) -> padding(Buf, Opt?OPT.space).
  389. -spec pp_newline(binary(), list(), opt()) -> binary().
  390. pp_newline(Buf, Level, Opt) -> pp_newline(Buf, Level, 0, Opt).
  391. -spec pp_newline(binary(), list(), non_neg_integer(), opt()) -> binary().
  392. pp_newline(Buf, _, _, ?OPT{indent = 0}) -> Buf;
  393. pp_newline(Buf, L, Extra, ?OPT{indent = N}) -> padding(<<Buf/binary, $\n>>, Extra * N + length(L) * N).
  394. -spec pp_newline_or_space(binary(), list(), opt()) -> binary().
  395. pp_newline_or_space(Buf, _, Opt = ?OPT{indent = 0}) -> pp_space(Buf, Opt);
  396. pp_newline_or_space(Buf, L, Opt) -> pp_newline(Buf, L, Opt).
  397. -spec padding(binary(), non_neg_integer()) -> binary().
  398. padding(Buf, 0) -> Buf;
  399. padding(Buf, 1) -> <<Buf/binary, " ">>;
  400. padding(Buf, 2) -> <<Buf/binary, " ">>;
  401. padding(Buf, 3) -> <<Buf/binary, " ">>;
  402. padding(Buf, 4) -> <<Buf/binary, " ">>;
  403. padding(Buf, 5) -> <<Buf/binary, " ">>;
  404. padding(Buf, 6) -> <<Buf/binary, " ">>;
  405. padding(Buf, 7) -> <<Buf/binary, " ">>;
  406. padding(Buf, 8) -> <<Buf/binary, " ">>;
  407. padding(Buf, N) -> padding(<<Buf/binary, " ">>, N - 9).
  408. -spec parse_options([jsone:encode_option()]) -> opt().
  409. parse_options(Options) ->
  410. parse_option(Options, ?OPT{}).
  411. -spec parse_option([jsone:encode_option()], opt()) -> opt().
  412. parse_option([], Opt) -> Opt;
  413. parse_option([native_utf8|T], Opt) ->
  414. parse_option(T, Opt?OPT{native_utf8=true});
  415. parse_option([native_forward_slash|T], Opt) ->
  416. parse_option(T, Opt?OPT{native_forward_slash=true});
  417. parse_option([canonical_form|T], Opt) ->
  418. parse_option(T, Opt?OPT{canonical_form=true});
  419. parse_option([{float_format, F}|T], Opt) when is_list(F) ->
  420. parse_option(T, Opt?OPT{float_format = F});
  421. parse_option([{space, N}|T], Opt) when is_integer(N), N >= 0 ->
  422. parse_option(T, Opt?OPT{space = N});
  423. parse_option([{indent, N}|T], Opt) when is_integer(N), N >= 0 ->
  424. parse_option(T, Opt?OPT{indent = N});
  425. parse_option([{object_key_type, Type}|T], Opt) when Type =:= string; Type =:= scalar; Type =:= value ->
  426. parse_option(T, Opt?OPT{object_key_type = Type});
  427. parse_option([{datetime_format, Fmt}|T], Opt) ->
  428. case Fmt of
  429. iso8601 -> parse_option(T, Opt?OPT{datetime_format = {iso8601, 0}});
  430. {iso8601, utc} -> parse_option(T, Opt?OPT{datetime_format = {iso8601, 0}});
  431. {iso8601, local} -> parse_option(T, Opt?OPT{datetime_format = {iso8601, local_offset()}});
  432. {iso8601, N} when -86400 < N, N < 86400 -> parse_option(T, Opt?OPT{datetime_format = {iso8601, N}});
  433. _ -> error(badarg, [[{datetime_format, Fmt}|T], Opt])
  434. end;
  435. parse_option([undefined_as_null|T],Opt) ->
  436. parse_option(T, Opt?OPT{undefined_as_null = true});
  437. parse_option([skip_undefined|T],Opt) ->
  438. parse_option(T, Opt?OPT{skip_undefined = true});
  439. parse_option([{map_unknown_value, F}|T], Opt) when is_function(F, 1); F =:= undefined ->
  440. parse_option(T, Opt?OPT{map_unknown_value = F});
  441. parse_option(List, Opt) ->
  442. error(badarg, [List, Opt]).
  443. -spec local_offset() -> jsone:utc_offset_seconds().
  444. local_offset() ->
  445. UTC = {{1970, 1, 2}, {0,0,0}},
  446. Local = calendar:universal_time_to_local_time({{1970, 1, 2}, {0,0,0}}),
  447. calendar:datetime_to_gregorian_seconds(Local) - calendar:datetime_to_gregorian_seconds(UTC).