jsone.erl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. %%% @doc JSON decoding/encoding module
  2. %%% @end
  3. %%%
  4. %%% Copyright (c) 2013-2015, Takeru Ohta <phjgt308@gmail.com>
  5. %%%
  6. %%% The MIT License
  7. %%%
  8. %%% Permission is hereby granted, free of charge, to any person obtaining a copy
  9. %%% of this software and associated documentation files (the "Software"), to deal
  10. %%% in the Software without restriction, including without limitation the rights
  11. %%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. %%% copies of the Software, and to permit persons to whom the Software is
  13. %%% furnished to do so, subject to the following conditions:
  14. %%%
  15. %%% The above copyright notice and this permission notice shall be included in
  16. %%% all copies or substantial portions of the Software.
  17. %%%
  18. %%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. %%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. %%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. %%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. %%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. %%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. %%% THE SOFTWARE.
  25. %%%
  26. %%%---------------------------------------------------------------------------------------
  27. -module(jsone).
  28. %%--------------------------------------------------------------------------------
  29. %% Exported API
  30. %%--------------------------------------------------------------------------------
  31. -export([
  32. decode/1, decode/2,
  33. try_decode/1, try_decode/2,
  34. encode/1, encode/2,
  35. try_encode/1, try_encode/2
  36. ]).
  37. -export_type([
  38. json_value/0,
  39. json_boolean/0,
  40. json_number/0,
  41. json_string/0,
  42. json_array/0,
  43. json_object/0,
  44. json_object_members/0,
  45. json_term/0,
  46. json_object_format_tuple/0,
  47. json_object_format_proplist/0,
  48. json_object_format_map/0,
  49. json_scalar/0,
  50. encode_option/0,
  51. decode_option/0,
  52. float_format_option/0,
  53. datetime_encode_format/0, datetime_format/0,
  54. timezone/0, utc_offset_seconds/0, stack_item/0
  55. ]).
  56. %%--------------------------------------------------------------------------------
  57. %% Types & Macros
  58. %%--------------------------------------------------------------------------------
  59. -type json_value() :: json_number() | json_string() | json_array() | json_object() | json_boolean() | null | json_term().
  60. -type json_boolean() :: boolean().
  61. -type json_number() :: number().
  62. -type json_string() :: binary() | atom() | calendar:datetime(). % NOTE: `decode/1' always returns `binary()' value
  63. -type json_array() :: [json_value()].
  64. -type json_object() :: json_object_format_tuple()
  65. | json_object_format_proplist()
  66. | json_object_format_map().
  67. -type json_object_members() :: [{json_string(), json_value()}].
  68. -type json_term() :: {{json, iolist()}} | {{json_utf8, unicode:chardata()}}.
  69. %% `json_term()' allows inline already encoded JSON value. `json' variant
  70. %% expects byte encoded utf8 data values as list members. `json_utf8' expect
  71. %% Unicode code points as list members. Binaries are copied "as is" in both
  72. %% variants except `json_utf8' will check if binary contain valid `UTF-8'
  73. %% encoded data. In short, `json' uses `erlang:iolist_to_binary/1' and
  74. %% `json_utf8' uses `unicode:chardata_to_binary/1' for encoding.
  75. %%
  76. %% A simple example is worth a thousand words.
  77. %%
  78. %% ```
  79. %% 1> S = "hélo".
  80. %% "hélo"
  81. %% 2> shell:strings(false).
  82. %% true
  83. %% 3> S.
  84. %% [104,233,108,111]
  85. %% 4> B = jsone:encode({{json, S}}). % invalid UTF-8
  86. %% <<104,233,108,111>>
  87. %% 5> B2 = jsone:encode({{json_utf8, S}}). % valid UTF-8
  88. %% <<104,195,169,108,111>>
  89. %% 6> jsone:encode({{json, B}}).
  90. %% <<104,233,108,111>>
  91. %% 7> jsone:encode({{json_utf8, B}}).
  92. %% ** exception error: {invalid_json_utf8,<<104>>,<<233,108,111>>}
  93. %% in function jsone_encode:value/4
  94. %% called as jsone_encode:value({json_utf8,<<104,233,108,111>>},
  95. %% [],<<>>,
  96. %% {encode_opt_v2,false,
  97. %% [{scientific,20}],
  98. %% {iso8601,0},
  99. %% string,0,0})
  100. %% in call from jsone:encode/2 (/home/hynek/work/altworx/jsone/_build/default/lib/jsone/src/jsone.erl, line 302)
  101. %% 8> jsone:encode({{json_utf8, B2}}).
  102. %% <<104,195,169,108,111>>
  103. %% 9> shell:strings(true).
  104. %% false
  105. %% 10> jsone:encode({{json_utf8, B2}}).
  106. %% <<"hélo"/utf8>>
  107. %% 11> jsone:encode({{json, binary_to_list(B2)}}). % UTF-8 encoded list leads to valid UTF-8
  108. %% <<"hélo"/utf8>>
  109. %% '''
  110. %%
  111. -type json_object_format_tuple() :: {json_object_members()}.
  112. -type json_object_format_proplist() :: [{}] | json_object_members().
  113. -ifdef('NO_MAP_TYPE').
  114. -opaque json_object_format_map() :: json_object_format_proplist().
  115. %% `maps' is not supported in this erts version
  116. -else.
  117. -type json_object_format_map() :: map().
  118. -endif.
  119. -type json_scalar() :: json_boolean() | json_number() | json_string().
  120. -type float_format_option() :: {scientific, Decimals :: 0..249}
  121. | {decimals, Decimals :: 0..253}
  122. | compact.
  123. %% `scientific': <br />
  124. %% - The float will be formatted using scientific notation with `Decimals' digits of precision. <br />
  125. %%
  126. %% `decimals': <br />
  127. %% - The encoded string will contain at most `Decimals' number of digits past the decimal point. <br />
  128. %% - If `compact' is provided the trailing zeros at the end of the string are truncated. <br />
  129. %%
  130. %% For more details, see <a href="http://erlang.org/doc/man/erlang.html#float_to_list-2">erlang:flaot_to_list/2</a>.
  131. %%
  132. %% ```
  133. %% > jsone:encode(1.23).
  134. %% <<"1.22999999999999998224e+00">>
  135. %%
  136. %% > jsone:encode(1.23, [{float_format, [{scientific, 4}]}]).
  137. %% <"1.2300e+00">>
  138. %%
  139. %% > jsone:encode(1.23, [{float_format, [{scientific, 1}]}]).
  140. %% <<"1.2e+00">>
  141. %%
  142. %% > jsone:encode(1.23, [{float_format, [{decimals, 4}]}]).
  143. %% <<"1.2300">>
  144. %%
  145. %% > jsone:encode(1.23, [{float_format, [{decimals, 4}, compact]}]).
  146. %% <<"1.23">>
  147. %% '''
  148. -type datetime_encode_format() :: Format::datetime_format()
  149. | {Format::datetime_format(), TimeZone::timezone()}.
  150. %% Datetime encoding format.
  151. %%
  152. %% The default value of `TimeZone' is `utc'.
  153. %%
  154. %% ```
  155. %% %
  156. %% % Universal Time
  157. %% %
  158. %% > jsone:encode({{2000, 3, 10}, {10, 3, 58}}, [{datetime_format, iso8601}]).
  159. %% <<"\"2000-03-10T10:03:58Z\"">>
  160. %%
  161. %% %
  162. %% % Local Time (JST)
  163. %% %
  164. %% > jsone:encode({{2000, 3, 10}, {10, 3, 58}}, [{datetime_format, {iso8601, local}}]).
  165. %% <<"\"2000-03-10T10:03:58+09:00\"">>
  166. %%
  167. %% %
  168. %% % Explicit TimeZone Offset
  169. %% %
  170. %% > jsone:encode({{2000, 3, 10}, {10, 3, 58}}, [{datetime_format, {iso8601, -2*60*60}}]).
  171. %% <<"\"2000-03-10T10:03:58-02:00\"">>
  172. %% '''
  173. -type datetime_format() :: iso8601.
  174. -type timezone() :: utc | local | utc_offset_seconds().
  175. -type utc_offset_seconds() :: -86399..86399.
  176. -type encode_option() :: native_utf8
  177. | {float_format, [float_format_option()]}
  178. | {datetime_format, datetime_encode_format()}
  179. | {object_key_type, string | scalar | value}
  180. | {space, non_neg_integer()}
  181. | {indent, non_neg_integer()}
  182. | undefined_as_null.
  183. %% `native_utf8': <br />
  184. %% - Encodes UTF-8 characters as a human-readable(non-escaped) string <br />
  185. %%
  186. %% `{float_format, Optoins}':
  187. %% - Encodes a `float()` value in the format which specified by `Options' <br />
  188. %% - default: `[{scientific, 20}]' <br />
  189. %%
  190. %% `{datetime_format, Format}`:
  191. %% - Encodes a `calendar:datetime()` value in the format which specified by `Format' <br />
  192. %% - default: `{iso8601, utc}' <br />
  193. %%
  194. %% `object_key_type':
  195. %% - Allowable object key type <br />
  196. %% - `string': Only string values are allowed (i.e. `json_string()' type) <br />
  197. %% - `scalar': In addition to `string', following values are allowed: nulls, booleans, numerics (i.e. `json_scalar()' type) <br />
  198. %% - `value': Any json compatible values are allowed (i.e. `json_value()' type) <br />
  199. %% - default: `string' <br />
  200. %% - NOTE: If `scalar' or `value' option is specified, non `json_string()' key will be automatically converted to a `binary()' value (e.g. `1' => `<<"1">>', `#{}' => `<<"{}">>') <br />
  201. %%
  202. %% `{space, N}': <br />
  203. %% - Inserts `N' spaces after every commna and colon <br />
  204. %% - default: `0' <br />
  205. %%
  206. %% `{indent, N}': <br />
  207. %% - Inserts a newline and `N' spaces for each level of indentation <br />
  208. %% - default: `0' <br />
  209. -type decode_option() :: {object_format, tuple | proplist | map}
  210. | {allow_ctrl_chars, boolean()}
  211. | {'keys', 'binary' | 'atom' | 'existing_atom' | 'attempt_atom'}.
  212. %% `object_format': <br />
  213. %% - Decoded JSON object format <br />
  214. %% - `tuple': An object is decoded as `{[]}' if it is empty, otherwise `{[{Key, Value}]}'. <br />
  215. %% - `proplist': An object is decoded as `[{}]' if it is empty, otherwise `[{Key, Value}]'. <br />
  216. %% - `map': An object is decoded as `#{}' if it is empty, otherwise `#{Key => Value}'. <br />
  217. %% - default: `map' if OTP version is OTP-17 or more, `tuple' otherwise <br />
  218. %%
  219. %% `allow_ctrl_chars': <br />
  220. %% - If the value is `true', strings which contain ununescaped control characters will be regarded as a legal JSON string <br />
  221. %% - default: `false'<br />
  222. %%
  223. %% `keys': <br />
  224. %% Defines way how object keys are decoded. The default value is `binary'.
  225. %% The option is compatible with `labels' option in `jsx'. <br />
  226. %% - `binary': The key is left as a string which is encoded as binary. It's default
  227. %% and backward compatible behaviour. <br />
  228. %% - `atom': The key is converted to an atom. Results in `badarg' if Key value
  229. %% regarded as UTF-8 is not a valid atom. <br />
  230. %% - `existing_atom': Returns existing atom. Any key value which is not
  231. %% existing atom raises `badarg' exception. <br />
  232. %% - `attempt_atom': Returns existing atom as `existing_atom' but returns a
  233. %% binary string if fails find one.
  234. -type stack_item() :: {Module :: module(),
  235. Function :: atom(),
  236. Arity :: arity() | (Args :: [term()]),
  237. Location :: [{file, Filename :: string()} |
  238. {line, Line :: pos_integer()}]}.
  239. %% An item in a stack back-trace.
  240. %%
  241. %% Note that the `erlang' module already defines the same `stack_item/0' type,
  242. %% but it is not exported from the module.
  243. %% So, maybe as a temporary measure, we redefine this type for passing full dialyzer analysis.
  244. %%--------------------------------------------------------------------------------
  245. %% Exported Functions
  246. %%--------------------------------------------------------------------------------
  247. %% @equiv decode(Json, [])
  248. -spec decode(binary()) -> json_value().
  249. decode(Json) ->
  250. decode(Json, []).
  251. %% @doc Decodes an erlang term from json text (a utf8 encoded binary)
  252. %%
  253. %% Raises an error exception if input is not valid json
  254. %%
  255. %% ```
  256. %% > jsone:decode(<<"1">>, []).
  257. %% 1
  258. %%
  259. %% > jsone:decode(<<"wrong json">>, []).
  260. %% ** exception error: bad argument
  261. %% in function jsone_decode:number_integer_part/4
  262. %% called as jsone_decode:number_integer_part(<<"wrong json">>,1,[],<<>>)
  263. %% in call from jsone:decode/1 (src/jsone.erl, line 71)
  264. %% '''
  265. -spec decode(binary(), [decode_option()]) -> json_value().
  266. decode(Json, Options) ->
  267. try
  268. {ok, Value, _} = try_decode(Json, Options),
  269. Value
  270. catch
  271. error:{badmatch, {error, {Reason, [StackItem]}}} ->
  272. erlang:raise(error, Reason, [StackItem | erlang:get_stacktrace()])
  273. end.
  274. %% @equiv try_decode(Json, [])
  275. -spec try_decode(binary()) -> {ok, json_value(), Remainings::binary()} | {error, {Reason::term(), [stack_item()]}}.
  276. try_decode(Json) ->
  277. try_decode(Json, []).
  278. %% @doc Decodes an erlang term from json text (a utf8 encoded binary)
  279. %%
  280. %% ```
  281. %% > jsone:try_decode(<<"[1,2,3] \"next value\"">>, []).
  282. %% {ok,[1,2,3],<<" \"next value\"">>}
  283. %%
  284. %% > jsone:try_decode(<<"wrong json">>, []).
  285. %% {error,{badarg,[{jsone_decode,number_integer_part,
  286. %% [<<"wrong json">>,1,[],<<>>],
  287. %% [{line,208}]}]}}
  288. %% '''
  289. -spec try_decode(binary(), [decode_option()]) -> {ok, json_value(), Remainings::binary()} | {error, {Reason::term(), [stack_item()]}}.
  290. try_decode(Json, Options) ->
  291. jsone_decode:decode(Json, Options).
  292. %% @equiv encode(JsonValue, [])
  293. -spec encode(json_value()) -> binary().
  294. encode(JsonValue) ->
  295. encode(JsonValue, []).
  296. %% @doc Encodes an erlang term into json text (a utf8 encoded binary)
  297. %%
  298. %% Raises an error exception if input is not an instance of type `json_value()'
  299. %%
  300. %% ```
  301. %% > jsone:encode([1, null, 2]).
  302. %% <<"[1,null,2]">>
  303. %%
  304. %% > jsone:encode([1, self(), 2]). % A pid is not a json value
  305. %% ** exception error: bad argument
  306. %% in function jsone_encode:value/3
  307. %% called as jsone_encode:value(<0,34,0>,[{array_values,[2]}],<<"[1,">>)
  308. %% in call from jsone:encode/1 (src/jsone.erl, line 97)
  309. %% '''
  310. -spec encode(json_value(), [encode_option()]) -> binary().
  311. encode(JsonValue, Options) ->
  312. try
  313. {ok, Binary} = try_encode(JsonValue, Options),
  314. Binary
  315. catch
  316. error:{badmatch, {error, {Reason, [StackItem]}}} ->
  317. erlang:raise(error, Reason, [StackItem | erlang:get_stacktrace()])
  318. end.
  319. %% @equiv try_encode(JsonValue, [])
  320. -spec try_encode(json_value()) -> {ok, binary()} | {error, {Reason::term(), [stack_item()]}}.
  321. try_encode(JsonValue) ->
  322. try_encode(JsonValue, []).
  323. %% @doc Encodes an erlang term into json text (a utf8 encoded binary)
  324. %%
  325. %% ```
  326. %% > jsone:try_encode([1, null, 2]).
  327. %% {ok,<<"[1,null,2]">>}
  328. %%
  329. %% > jsone:try_encode([1, hoge, 2]). % 'hoge' atom is not a json value
  330. %% {error,{badarg,[{jsone_encode,value,
  331. %% [hoge,[{array_values,[2]}],<<"[1,">>],
  332. %% [{line,86}]}]}}
  333. %% '''
  334. -spec try_encode(json_value(), [encode_option()]) -> {ok, binary()} | {error, {Reason::term(), [stack_item()]}}.
  335. try_encode(JsonValue, Options) ->
  336. jsone_encode:encode(JsonValue, Options).