jsone.erl 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. %%% @doc JSON decoding/encoding module
  2. %%% @end
  3. %%%
  4. %%% Copyright (c) 2013-2014, 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, try_decode/1,
  33. encode/1, try_encode/1
  34. ]).
  35. -export_type([
  36. json_value/0,
  37. json_number/0,
  38. json_string/0,
  39. json_array/0,
  40. json_object/0,
  41. json_object_members/0,
  42. json_boolean/0
  43. ]).
  44. %%--------------------------------------------------------------------------------
  45. %% Types
  46. %%--------------------------------------------------------------------------------
  47. -type json_value() :: json_number() | json_string() | json_array() | json_object() | json_boolean() | null.
  48. -type json_boolean() :: boolean().
  49. -type json_number() :: number().
  50. -type json_string() :: binary().
  51. -type json_array() :: [json_value()].
  52. -type json_object() :: {json_object_members()}.
  53. -type json_object_members() :: [{json_string(), json_value()}].
  54. %%--------------------------------------------------------------------------------
  55. %% Exported Functions
  56. %%--------------------------------------------------------------------------------
  57. %% @doc JSONバイナリをデコードする.
  58. %%
  59. %% デコードに失敗した場合はエラーが送出される
  60. -spec decode(binary()) -> json_value().
  61. decode(Json) ->
  62. try
  63. {ok, Value, _} = try_decode(Json),
  64. Value
  65. catch
  66. error:{badmatch, {error, {Reason, [StackItem]}}} ->
  67. erlang:raise(error, Reason, [StackItem | erlang:get_stacktrace()])
  68. end.
  69. %% @doc JSONバイナリをデコードする.
  70. -spec try_decode(binary()) -> {ok, json_value(), Rest::binary()} | {error, {Reason::term(), [erlang:stack_item()]}}.
  71. try_decode(Json) ->
  72. jsone_decode:decode(Json).
  73. %% @doc JSON値をバイナリ形式にエンコードする.
  74. %%
  75. %% エンコードに失敗した場合はエラーが送出される
  76. -spec encode(json_value()) -> binary().
  77. encode(JsonValue) ->
  78. try
  79. {ok, Binary} = try_encode(JsonValue),
  80. Binary
  81. catch
  82. error:{badmatch, {error, {Reason, [StackItem]}}} ->
  83. erlang:raise(error, Reason, [StackItem | erlang:get_stacktrace()])
  84. end.
  85. %% @doc JSON値をバイナリ形式にエンコードする
  86. -spec try_encode(json_value()) -> {ok, binary()} | {error, {Reason::term(), [erlang:stack_item()]}}.
  87. try_encode(JsonValue) ->
  88. jsone_encode:encode(JsonValue).