-
Notifications
You must be signed in to change notification settings - Fork 39
/
default_implementations.ex
executable file
·184 lines (155 loc) · 4.78 KB
/
default_implementations.ex
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
defimpl JSON.Encoder, for: Tuple do
@doc """
Encodes an Elixir tuple into a JSON array
"""
def encode(term), do: term |> Tuple.to_list() |> JSON.Encoder.Helpers.enum_encode()
@doc """
Returns an atom that represents the JSON type for the term
"""
def typeof(_), do: :array
end
defimpl JSON.Encoder, for: HashDict do
@doc """
Encodes an Elixir HashDict into a JSON object
"""
def encode(dict), do: JSON.Encoder.Helpers.dict_encode(dict)
@doc """
Returns :object
"""
def typeof(_), do: :object
end
defimpl JSON.Encoder, for: List do
@doc """
Encodes an Elixir List into a JSON array
"""
def encode([]), do: {:ok, "[]"}
def encode(list) do
if Keyword.keyword?(list) do
JSON.Encoder.Helpers.dict_encode(list)
else
JSON.Encoder.Helpers.enum_encode(list)
end
end
@doc """
Returns an atom that represents the JSON type for the term
"""
def typeof([]), do: :array
def typeof(list) do
if Keyword.keyword?(list) do
:object
else
:array
end
end
end
defimpl JSON.Encoder, for: [Integer, Float] do
@doc """
Converts Elixir Integer and Floats into JSON Numbers
"""
# Elixir converts octal, etc into decimal when putting in strings
def encode(number), do: {:ok, "#{number}"}
@doc """
Returns an atom that represents the JSON type for the term
"""
def typeof(_), do: :number
end
defimpl JSON.Encoder, for: Atom do
@doc """
Converts Elixir Atoms into their JSON equivalents
"""
def encode(nil), do: {:ok, "null"}
def encode(false), do: {:ok, "false"}
def encode(true), do: {:ok, "true"}
def encode(atom) when is_atom(atom), do: atom |> Atom.to_string() |> JSON.Encoder.encode()
@doc """
Returns an atom that represents the JSON type for the term
"""
def typeof(boolean) when is_boolean(boolean), do: :boolean
def typeof(nil), do: :null
def typeof(atom) when is_atom(atom), do: :string
end
defimpl JSON.Encoder, for: BitString do
# 32 = ascii space, cleaner than using "? ", I think
@acii_space 32
@doc """
Converts Elixir String into JSON String
"""
def encode(bitstring), do: {:ok, <<?">> <> encode_binary_recursive(bitstring, []) <> <<?">>}
defp encode_binary_recursive(<<head::utf8, tail::binary>>, acc) do
encode_binary_recursive(tail, encode_binary_character(head, acc))
end
# stop cond
defp encode_binary_recursive(<<>>, acc), do: acc |> Enum.reverse() |> to_string
defp encode_binary_character(?", acc), do: [?", ?\\ | acc]
defp encode_binary_character(?\b, acc), do: [?b, ?\\ | acc]
defp encode_binary_character(?\f, acc), do: [?f, ?\\ | acc]
defp encode_binary_character(?\n, acc), do: [?n, ?\\ | acc]
defp encode_binary_character(?\r, acc), do: [?r, ?\\ | acc]
defp encode_binary_character(?\t, acc), do: [?t, ?\\ | acc]
defp encode_binary_character(?\\, acc), do: [?\\, ?\\ | acc]
defp encode_binary_character(char, acc) when is_number(char) and char < @acii_space do
encode_hexadecimal_unicode_control_character(char, [?u, ?\\ | acc])
end
# anything else besides these control characters, just let it through
defp encode_binary_character(char, acc) when is_number(char), do: [char | acc]
defp encode_hexadecimal_unicode_control_character(char, acc) when is_number(char) do
[
char
|> Integer.to_charlist(16)
|> zeropad_hexadecimal_unicode_control_character
|> Enum.reverse()
| acc
]
end
defp zeropad_hexadecimal_unicode_control_character([a, b, c]), do: [?0, a, b, c]
defp zeropad_hexadecimal_unicode_control_character([a, b]), do: [?0, ?0, a, b]
defp zeropad_hexadecimal_unicode_control_character([a]), do: [?0, ?0, ?0, a]
defp zeropad_hexadecimal_unicode_control_character(iolist) when is_list(iolist), do: iolist
@doc """
Returns an atom that represents the JSON type for the term
"""
def typeof(_), do: :string
end
defimpl JSON.Encoder, for: Record do
@doc """
Encodes elixir records into json objects
"""
def encode(record), do: record.to_keywords |> JSON.Encoder.Helpers.dict_encode()
@doc """
Encodes a record into a JSON object
"""
def typeof(_), do: :object
end
defimpl JSON.Encoder, for: Map do
@doc """
Encodes maps into object
"""
def encode(map), do: map |> JSON.Encoder.Helpers.dict_encode()
@doc """
Returns an atom that represents the JSON type for the term
"""
def typeof(_), do: :object
end
defimpl JSON.Encoder, for: Any do
@moduledoc """
Falllback module for encoding any other values
"""
@doc """
Encodes a map into a JSON object
"""
def encode(%{} = struct) do
struct
|> Map.to_list()
|> JSON.Encoder.Helpers.dict_encode()
end
def encode(x) do
x
|> Kernel.inspect()
|> JSON.Encoder.encode()
end
@doc """
Fallback method
"""
def typeof(struct) when is_map(struct), do: :object
def typeof(_), do: :string
end