File: json.ex

package info (click to toggle)
rabbitmq-server 4.0.5-8
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 37,972 kB
  • sloc: erlang: 257,835; javascript: 22,466; sh: 3,037; makefile: 2,517; python: 1,966; xml: 646; cs: 335; java: 244; ruby: 212; php: 100; perl: 63; awk: 13
file content (100 lines) | stat: -rwxr-xr-x 2,785 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
defmodule JSONOLD do
  @moduledoc """
  Provides a RFC 7159, ECMA 404, and JSONOLDTestSuite compliant JSONOLD Encoder / Decoder
  """

  require Logger

  import JSONOLD.Logger

  alias JSONOLD.Decoder
  alias JSONOLD.Encoder

  @vsn "1.0.2"

  @doc """
  Returns a JSONOLD string representation of the Elixir term

  ## Examples

      iex> JSONOLD.encode([result: "this will be a JSONOLD result"])
      {:ok, "{\\\"result\\\":\\\"this will be a JSONOLD result\\\"}"}

  """
  @spec encode(term) :: {atom, bitstring}
  defdelegate encode(term), to: Encoder

  @doc """
  Returns a JSONOLD string representation of the Elixir term, raises errors when something bad happens

  ## Examples

      iex> JSONOLD.encode!([result: "this will be a JSONOLD result"])
      "{\\\"result\\\":\\\"this will be a JSONOLD result\\\"}"

  """
  @spec encode!(term) :: bitstring
  def encode!(term) do
    case encode(term) do
      {:ok, value} -> value
      {:error, error_info} -> raise JSONOLD.Encoder.Error, error_info: error_info
      _ -> raise JSONOLD.Encoder.Error
    end
  end

  @doc """
  Converts a valid JSONOLD string into an Elixir term

  ## Examples

      iex> JSONOLD.decode("{\\\"result\\\":\\\"this will be an Elixir result\\\"}")
      {:ok, Enum.into([{"result", "this will be an Elixir result"}], Map.new)}
  """
  @spec decode(bitstring) :: {atom, term}
  @spec decode(charlist) :: {atom, term}
  defdelegate decode(bitstring_or_char_list), to: Decoder

  @doc """
  Converts a valid JSONOLD string into an Elixir term, raises errors when something bad happens

  ## Examples

      iex> JSONOLD.decode!("{\\\"result\\\":\\\"this will be an Elixir result\\\"}")
      Enum.into([{"result", "this will be an Elixir result"}], Map.new)
  """
  @spec decode!(bitstring) :: term
  @spec decode!(charlist) :: term
  def decode!(bitstring_or_char_list) do
    case decode(bitstring_or_char_list) do
      {:ok, value} ->
        log(:debug, fn ->
          "#{__MODULE__}.decode!(#{inspect(bitstring_or_char_list)}} was sucesfull: #{
            inspect(value)
          }"
        end)

        value

      {:error, {:unexpected_token, tok}} ->
        log(:debug, fn ->
          "#{__MODULE__}.decode!(#{inspect(bitstring_or_char_list)}} unexpected token #{tok}"
        end)

        raise JSONOLD.Decoder.UnexpectedTokenError, token: tok

      {:error, :unexpected_end_of_buffer} ->
        log(:debug, fn ->
          "#{__MODULE__}.decode!(#{inspect(bitstring_or_char_list)}} end of buffer"
        end)

        raise JSONOLD.Decoder.UnexpectedEndOfBufferError

      e ->
        log(:debug, fn ->
          "#{__MODULE__}.decode!(#{inspect(bitstring_or_char_list)}} an unknown problem occurred #{
            inspect(e)
          }"
        end)
    end
  end
end