network-infra-utility 0.2.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +21 -0
  3. data/CHANGELOG.md +31 -4
  4. data/Gemfile +2 -0
  5. data/Gemfile.lock +70 -0
  6. data/Rakefile +1 -1
  7. data/bin/dns-query +834 -0
  8. data/bin/geo-doc +135 -0
  9. data/bin/geo-get +1 -1
  10. data/document/ASNum/346/250/241/345/235/227/345/212/237/350/203/275/350/257/264/346/230/216.md +242 -0
  11. data/document/DNSQuery/345/267/245/345/205/267/344/275/277/347/224/250/346/226/271/346/263/225.md +248 -0
  12. data/document/Geo/345/221/275/344/273/244/345/267/245/345/205/267/344/275/277/347/224/250/346/226/271/346/263/225.md +441 -0
  13. data/document/IP/346/250/241/345/235/227/345/212/237/350/203/275/350/257/264/346/230/216.md +297 -0
  14. data/document/MAC/346/250/241/345/235/227/345/212/237/350/203/275/350/257/264/346/230/216.md +296 -0
  15. data/document/SSH/350/277/236/346/216/245/345/256/242/346/210/267/347/253/257/344/275/277/347/224/250/346/226/271/346/263/225.md +764 -0
  16. data/network-infra-utility.gemspec +4 -2
  17. data/network.rb +3 -1
  18. data/service/geodb/GeoAPI.md +1 -0
  19. data/service/geodb/geodb.rb +278 -1
  20. data/service/ssh/README.md +942 -0
  21. data/service/ssh/bin/ssh-client +198 -0
  22. data/service/ssh/design/SSH/350/277/236/346/216/245/345/256/242/346/210/267/347/253/257/345/212/237/350/203/275/351/234/200/346/261/202/346/226/207/346/241/243.md +292 -0
  23. data/service/ssh/design/SSH/350/277/236/346/216/245/345/256/242/346/210/267/347/253/257/350/257/246/347/273/206/350/256/276/350/256/241/346/226/207/346/241/243.md +1521 -0
  24. data/service/ssh/design/SSH/350/277/236/346/216/245/345/256/242/346/210/267/347/253/257/350/275/257/344/273/266/350/256/276/350/256/241/346/226/207/346/241/243.md +2493 -0
  25. data/service/ssh/ext/ssh_core/bin/ssh_core.cmd +28 -0
  26. data/service/ssh/ext/ssh_core/config/sys.config +0 -0
  27. data/service/ssh/ext/ssh_core/config/vm.args +0 -0
  28. data/service/ssh/ext/ssh_core/local_deps/jsx/CHECKSUM +1 -0
  29. data/service/ssh/ext/ssh_core/local_deps/jsx/LICENSE +21 -0
  30. data/service/ssh/ext/ssh_core/local_deps/jsx/README.md +696 -0
  31. data/service/ssh/ext/ssh_core/local_deps/jsx/VERSION +1 -0
  32. data/service/ssh/ext/ssh_core/local_deps/jsx/contents.tar.gz +0 -0
  33. data/service/ssh/ext/ssh_core/local_deps/jsx/metadata.config +15 -0
  34. data/service/ssh/ext/ssh_core/local_deps/jsx/rebar.config +17 -0
  35. data/service/ssh/ext/ssh_core/local_deps/jsx/rebar.lock +1 -0
  36. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx.app.src +10 -0
  37. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx.erl +506 -0
  38. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_config.erl +393 -0
  39. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_config.hrl +18 -0
  40. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_consult.erl +81 -0
  41. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_decoder.erl +1909 -0
  42. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_encoder.erl +116 -0
  43. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_parser.erl +1214 -0
  44. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_to_json.erl +408 -0
  45. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_to_term.erl +389 -0
  46. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_verify.erl +121 -0
  47. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx.erl +506 -0
  48. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_config.erl +393 -0
  49. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_config.hrl +18 -0
  50. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_consult.erl +81 -0
  51. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_decoder.erl +1909 -0
  52. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_encoder.erl +116 -0
  53. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_parser.erl +1214 -0
  54. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_to_json.erl +408 -0
  55. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_to_term.erl +389 -0
  56. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_verify.erl +121 -0
  57. data/service/ssh/ext/ssh_core/rebar.config +24 -0
  58. data/service/ssh/ext/ssh_core/rebar.lock +1 -0
  59. data/service/ssh/ext/ssh_core/src/ssh_auth_engine.erl +156 -0
  60. data/service/ssh/ext/ssh_core/src/ssh_channel_stm.erl +232 -0
  61. data/service/ssh/ext/ssh_core/src/ssh_codec.erl +83 -0
  62. data/service/ssh/ext/ssh_core/src/ssh_conn_sup.erl +48 -0
  63. data/service/ssh/ext/ssh_core/src/ssh_conn_worker.erl +535 -0
  64. data/service/ssh/ext/ssh_core/src/ssh_core.app.src +36 -0
  65. data/service/ssh/ext/ssh_core/src/ssh_core_app.erl +11 -0
  66. data/service/ssh/ext/ssh_core/src/ssh_core_sup.erl +46 -0
  67. data/service/ssh/ext/ssh_core/src/ssh_infra_sup.erl +104 -0
  68. data/service/ssh/ext/ssh_core/src/ssh_ipc.hrl +80 -0
  69. data/service/ssh/ext/ssh_core/src/ssh_ipc_coalesce.erl +94 -0
  70. data/service/ssh/ext/ssh_core/src/ssh_ipc_gateway.erl +467 -0
  71. data/service/ssh/ext/ssh_core/src/ssh_ipc_proto.erl +95 -0
  72. data/service/ssh/ext/ssh_core/src/ssh_jump_chain.erl +101 -0
  73. data/service/ssh/ext/ssh_core/src/ssh_keepalive_mgr.erl +222 -0
  74. data/service/ssh/ext/ssh_core/src/ssh_known_hosts_proxy.erl +67 -0
  75. data/service/ssh/ext/ssh_core/src/ssh_port_fwd.erl +225 -0
  76. data/service/ssh/ext/ssh_core/src/ssh_sftp_session.erl +250 -0
  77. data/service/ssh/ext/ssh_core/src/ssh_sftp_sup.erl +62 -0
  78. data/service/ssh/ext/ssh_core_rs/Cargo.lock +2345 -0
  79. data/service/ssh/ext/ssh_core_rs/Cargo.toml +30 -0
  80. data/service/ssh/ext/ssh_core_rs/bin/ssh_core_rs +34 -0
  81. data/service/ssh/ext/ssh_core_rs/bin/ssh_core_rs.cmd +40 -0
  82. data/service/ssh/ext/ssh_core_rs/src/channel.rs +296 -0
  83. data/service/ssh/ext/ssh_core_rs/src/coalesce.rs +143 -0
  84. data/service/ssh/ext/ssh_core_rs/src/codec.rs +71 -0
  85. data/service/ssh/ext/ssh_core_rs/src/conn.rs +628 -0
  86. data/service/ssh/ext/ssh_core_rs/src/gateway.rs +389 -0
  87. data/service/ssh/ext/ssh_core_rs/src/handler.rs +293 -0
  88. data/service/ssh/ext/ssh_core_rs/src/keepalive.rs +194 -0
  89. data/service/ssh/ext/ssh_core_rs/src/main.rs +351 -0
  90. data/service/ssh/ext/ssh_core_rs/src/portfwd.rs +378 -0
  91. data/service/ssh/ext/ssh_core_rs/src/proto.rs +198 -0
  92. data/service/ssh/ext/ssh_core_rs/src/sftp.rs +294 -0
  93. data/service/ssh/lib/network_infra_utility/ssh/automation/macro_engine.rb +213 -0
  94. data/service/ssh/lib/network_infra_utility/ssh/client.rb +257 -0
  95. data/service/ssh/lib/network_infra_utility/ssh/config/schema.rb +90 -0
  96. data/service/ssh/lib/network_infra_utility/ssh/config/settings.rb +103 -0
  97. data/service/ssh/lib/network_infra_utility/ssh/config/store.rb +90 -0
  98. data/service/ssh/lib/network_infra_utility/ssh/ipc/coalesce.rb +83 -0
  99. data/service/ssh/lib/network_infra_utility/ssh/ipc/errors.rb +36 -0
  100. data/service/ssh/lib/network_infra_utility/ssh/ipc/router.rb +212 -0
  101. data/service/ssh/lib/network_infra_utility/ssh/ipc/transport.rb +81 -0
  102. data/service/ssh/lib/network_infra_utility/ssh/security/host_key.rb +211 -0
  103. data/service/ssh/lib/network_infra_utility/ssh/security/vault.rb +211 -0
  104. data/service/ssh/lib/network_infra_utility/ssh/session/history.rb +56 -0
  105. data/service/ssh/lib/network_infra_utility/ssh/session/manager.rb +92 -0
  106. data/service/ssh/lib/network_infra_utility/ssh/session/session.rb +109 -0
  107. data/service/ssh/lib/network_infra_utility/ssh/session/tree.rb +95 -0
  108. data/service/ssh/lib/network_infra_utility/ssh/terminal/ansi_parser.rb +435 -0
  109. data/service/ssh/lib/network_infra_utility/ssh/terminal/buffer.rb +78 -0
  110. data/service/ssh/lib/network_infra_utility/ssh/terminal/emulator.rb +159 -0
  111. data/service/ssh/lib/network_infra_utility/ssh/terminal/logger.rb +195 -0
  112. data/service/ssh/lib/network_infra_utility/ssh/terminal/screen.rb +212 -0
  113. data/service/ssh/lib/network_infra_utility/ssh/terminal/theme.rb +127 -0
  114. data/service/ssh/lib/network_infra_utility/ssh/version.rb +7 -0
  115. data/service/ssh/lib/network_infra_utility/ssh.rb +44 -0
  116. data/support/basic/as_num.rb +221 -0
  117. data/support/basic/mac_address.rb +281 -0
  118. data/version.rb +1 -1
  119. metadata +138 -1
@@ -0,0 +1,1909 @@
1
+ %% The MIT License
2
+
3
+ %% Copyright (c) 2010-2013 alisdair sullivan <alisdairsullivan@yahoo.ca>
4
+
5
+ %% Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ %% of this software and associated documentation files (the "Software"), to deal
7
+ %% in the Software without restriction, including without limitation the rights
8
+ %% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ %% copies of the Software, and to permit persons to whom the Software is
10
+ %% furnished to do so, subject to the following conditions:
11
+
12
+ %% The above copyright notice and this permission notice shall be included in
13
+ %% all copies or substantial portions of the Software.
14
+
15
+ %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ %% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ %% THE SOFTWARE.
22
+
23
+
24
+ -module(jsx_decoder).
25
+
26
+ %% inline handle_event, format_number and maybe_replace
27
+ -compile({inline, [handle_event/3]}).
28
+ -compile({inline, [format_number/1]}).
29
+ -compile({inline, [maybe_replace/2]}).
30
+ -compile({inline, [doublequote/5, singlequote/5]}).
31
+
32
+ -export([decoder/3, resume/6]).
33
+
34
+
35
+ -spec decoder(Handler::module(), State::any(), Config::jsx_config:options()) -> jsx:decoder().
36
+
37
+ decoder(Handler, State, Config) ->
38
+ fun(JSON) -> start(JSON, {Handler, Handler:init(State)}, [], jsx_config:parse_config(Config)) end.
39
+
40
+
41
+ %% resume allows continuation from interrupted decoding without having to explicitly export
42
+ %% all states
43
+ -spec resume(
44
+ Rest::binary(),
45
+ State::atom(),
46
+ Handler::module(),
47
+ Acc::any(),
48
+ Stack::list(atom()),
49
+ Config::jsx:config()
50
+ ) -> jsx:decoder() | {incomplete, jsx:decoder()}.
51
+
52
+ resume(Rest, State, Handler, Acc, Stack, Config) ->
53
+ case State of
54
+ start -> start(Rest, Handler, Stack, Config);
55
+ value -> value(Rest, Handler, Stack, Config);
56
+ object -> object(Rest, Handler, Stack, Config);
57
+ array -> array(Rest, Handler, Stack, Config);
58
+ colon -> colon(Rest, Handler, Stack, Config);
59
+ key -> key(Rest, Handler, Stack, Config);
60
+ string -> string(Rest, Handler, Acc, Stack, Config);
61
+ number -> number(Rest, Handler, Acc, Stack, Config);
62
+ true -> true(Rest, Handler, Stack, Config);
63
+ false -> false(Rest, Handler, Stack, Config);
64
+ null -> null(Rest, Handler, Stack, Config);
65
+ comment -> comment(Rest, Handler, Acc, Stack, Config);
66
+ maybe_done -> maybe_done(Rest, Handler, Stack, Config);
67
+ done -> done(Rest, Handler, Stack, Config)
68
+ end.
69
+
70
+
71
+ -include("jsx_config.hrl").
72
+
73
+
74
+ %% whitespace
75
+ -define(space, 16#20).
76
+ -define(tab, 16#09).
77
+ -define(cr, 16#0D).
78
+ -define(newline, 16#0A).
79
+
80
+ %% object delimiters
81
+ -define(start_object, 16#7B).
82
+ -define(end_object, 16#7D).
83
+
84
+ %% array delimiters
85
+ -define(start_array, 16#5B).
86
+ -define(end_array, 16#5D).
87
+
88
+ %% kv seperator
89
+ -define(comma, 16#2C).
90
+ -define(doublequote, 16#22).
91
+ -define(singlequote, 16#27).
92
+ -define(colon, 16#3A).
93
+
94
+ %% string escape sequences
95
+ -define(rsolidus, 16#5C).
96
+ -define(solidus, 16#2F).
97
+
98
+ %% math
99
+ -define(zero, 16#30).
100
+ -define(decimalpoint, 16#2E).
101
+ -define(negative, 16#2D).
102
+ -define(positive, 16#2B).
103
+
104
+ %% comments
105
+ -define(star, 16#2A).
106
+
107
+
108
+ %% some useful guards
109
+ -define(is_hex(Symbol),
110
+ (Symbol >= $a andalso Symbol =< $f) orelse
111
+ (Symbol >= $A andalso Symbol =< $F) orelse
112
+ (Symbol >= $0 andalso Symbol =< $9)
113
+ ).
114
+
115
+ -define(is_nonzero(Symbol),
116
+ Symbol >= $1 andalso Symbol =< $9
117
+ ).
118
+
119
+
120
+ %% error is a macro so the stack trace shows the error site when possible
121
+ -ifndef(error).
122
+ -define(error(State, Bin, Handler, Acc, Stack, Config),
123
+ case Config#config.error_handler of
124
+ false -> erlang:error(badarg);
125
+ F -> F(Bin, {decoder, State, Handler, Acc, Stack}, jsx_config:config_to_list(Config))
126
+ end
127
+ ).
128
+ -define(error(State, Bin, Handler, Stack, Config),
129
+ ?error(State, Bin, Handler, null, Stack, Config)
130
+ ).
131
+ -endif.
132
+
133
+
134
+ incomplete(State, Rest, Handler, Stack, Config = #config{stream=false}) ->
135
+ ?error(State, Rest, Handler, Stack, Config);
136
+ incomplete(State, Rest, Handler, Stack, Config) ->
137
+ incomplete(State, Rest, Handler, unused, Stack, Config).
138
+
139
+
140
+ incomplete(State, Rest, Handler, Acc, Stack, Config = #config{stream=false}) ->
141
+ ?error(State, Rest, Handler, Acc, Stack, Config);
142
+ incomplete(State, Rest, Handler, Acc, Stack, Config = #config{incomplete_handler=false}) ->
143
+ {incomplete, fun(Stream) when is_binary(Stream) ->
144
+ resume(<<Rest/binary, Stream/binary>>, State, Handler, Acc, Stack, Config);
145
+ (End) when End == end_stream; End == end_json ->
146
+ case resume(<<Rest/binary, ?space/utf8>>, State, Handler, Acc, Stack, Config#config{stream=false}) of
147
+ {incomplete, _} -> ?error(State, Rest, Handler, Acc, Stack, Config);
148
+ Else -> Else
149
+ end
150
+ end
151
+ };
152
+ incomplete(State, Rest, Handler, Acc, Stack, Config = #config{incomplete_handler=F}) ->
153
+ F(Rest, {decoder, State, Handler, Acc, Stack}, jsx_config:config_to_list(Config)).
154
+
155
+
156
+ handle_event(Event, {Handler, State}, _Config) -> {Handler, Handler:handle_event(Event, State)}.
157
+
158
+
159
+ start(<<16#ef, 16#bb, 16#bf, Rest/binary>>, Handler, Stack, Config) ->
160
+ value(Rest, Handler, Stack, Config);
161
+ start(<<16#ef, 16#bb>>, Handler, Stack, Config) ->
162
+ incomplete(start, <<16#ef, 16#bb>>, Handler, Stack, Config);
163
+ start(<<16#ef>>, Handler, Stack, Config) ->
164
+ incomplete(start, <<16#ef>>, Handler, Stack, Config);
165
+ start(<<>>, Handler, Stack, Config) ->
166
+ incomplete(start, <<>>, Handler, Stack, Config);
167
+ start(Bin, Handler, Stack, Config) ->
168
+ value(Bin, Handler, Stack, Config).
169
+
170
+
171
+ value(<<?doublequote, Rest/binary>>, Handler, Stack, Config) ->
172
+ string(Rest, Handler, Stack, Config);
173
+ value(<<?space, Rest/binary>>, Handler, Stack, Config) ->
174
+ value(Rest, Handler, Stack, Config);
175
+ value(<<?start_object, Rest/binary>>, Handler, Stack, Config) ->
176
+ object(Rest, handle_event(start_object, Handler, Config), [key|Stack], Config);
177
+ value(<<?start_array, Rest/binary>>, Handler, Stack, Config) ->
178
+ array(Rest, handle_event(start_array, Handler, Config), [array|Stack], Config);
179
+ value(<<$t, $r, $u, $e, Rest/binary>>, Handler, Stack, Config) ->
180
+ maybe_done(Rest, handle_event({literal, true}, Handler, Config), Stack, Config);
181
+ value(<<$f, $a, $l, $s, $e, Rest/binary>>, Handler, Stack, Config) ->
182
+ maybe_done(Rest, handle_event({literal, false}, Handler, Config), Stack, Config);
183
+ value(<<$n, $u, $l, $l, Rest/binary>>, Handler, Stack, Config) ->
184
+ maybe_done(Rest, handle_event({literal, null}, Handler, Config), Stack, Config);
185
+ value(<<?zero, Rest/binary>>, Handler, Stack, Config) ->
186
+ number(Rest, Handler, [?zero], [zero|Stack], Config);
187
+ value(<<$1, Rest/binary>>, Handler, Stack, Config) ->
188
+ number(Rest, Handler, [$1], [integer|Stack], Config);
189
+ value(<<$2, Rest/binary>>, Handler, Stack, Config) ->
190
+ number(Rest, Handler, [$2], [integer|Stack], Config);
191
+ value(<<$3, Rest/binary>>, Handler, Stack, Config) ->
192
+ number(Rest, Handler, [$3], [integer|Stack], Config);
193
+ value(<<$4, Rest/binary>>, Handler, Stack, Config) ->
194
+ number(Rest, Handler, [$4], [integer|Stack], Config);
195
+ value(<<$5, Rest/binary>>, Handler, Stack, Config) ->
196
+ number(Rest, Handler, [$5], [integer|Stack], Config);
197
+ value(<<$6, Rest/binary>>, Handler, Stack, Config) ->
198
+ number(Rest, Handler, [$6], [integer|Stack], Config);
199
+ value(<<$7, Rest/binary>>, Handler, Stack, Config) ->
200
+ number(Rest, Handler, [$7], [integer|Stack], Config);
201
+ value(<<$8, Rest/binary>>, Handler, Stack, Config) ->
202
+ number(Rest, Handler, [$8], [integer|Stack], Config);
203
+ value(<<$9, Rest/binary>>, Handler, Stack, Config) ->
204
+ number(Rest, Handler, [$9], [integer|Stack], Config);
205
+ value(<<?negative, Rest/binary>>, Handler, Stack, Config) ->
206
+ number(Rest, Handler, [$-], [negative|Stack], Config);
207
+ value(<<?newline, Rest/binary>>, Handler, Stack, Config) ->
208
+ value(Rest, Handler, Stack, Config);
209
+ value(<<$t, Rest/binary>>, Handler, Stack, Config) ->
210
+ true(Rest, Handler, Stack, Config);
211
+ value(<<$f, Rest/binary>>, Handler, Stack, Config) ->
212
+ false(Rest, Handler, Stack, Config);
213
+ value(<<$n, Rest/binary>>, Handler, Stack, Config) ->
214
+ null(Rest, Handler, Stack, Config);
215
+ value(<<?tab, Rest/binary>>, Handler, Stack, Config) ->
216
+ value(Rest, Handler, Stack, Config);
217
+ value(<<?cr, Rest/binary>>, Handler, Stack, Config) ->
218
+ value(Rest, Handler, Stack, Config);
219
+ value(<<?singlequote, Rest/binary>>, Handler, Stack, Config=#config{strict_single_quotes=false}) ->
220
+ string(Rest, Handler, [singlequote|Stack], Config);
221
+ value(<<?end_array, _/binary>> = Rest, Handler, Stack, Config=#config{strict_commas=false}) ->
222
+ maybe_done(Rest, Handler, Stack, Config);
223
+ value(<<?solidus, Rest/binary>>, Handler, Stack, Config=#config{strict_comments=true}) ->
224
+ ?error(value, <<?solidus, Rest/binary>>, Handler, Stack, Config);
225
+ value(<<?solidus, ?solidus, Rest/binary>>, Handler, Stack, Config) ->
226
+ comment(Rest, Handler, value, [comment|Stack], Config);
227
+ value(<<?solidus, ?star, Rest/binary>>, Handler, Stack, Config) ->
228
+ comment(Rest, Handler, value, [multicomment|Stack], Config);
229
+ value(<<?solidus>>, Handler, Stack, Config) ->
230
+ incomplete(value, <<?solidus>>, Handler, Stack, Config);
231
+ value(<<>>, Handler, Stack, Config) ->
232
+ incomplete(value, <<>>, Handler, Stack, Config);
233
+ value(Bin, Handler, Stack, Config) ->
234
+ ?error(value, Bin, Handler, Stack, Config).
235
+
236
+
237
+ object(<<?doublequote, Rest/binary>>, Handler, Stack, Config) ->
238
+ string(Rest, Handler, Stack, Config);
239
+ object(<<?space, Rest/binary>>, Handler, Stack, Config) ->
240
+ object(Rest, Handler, Stack, Config);
241
+ object(<<?end_object, Rest/binary>>, Handler, [key|Stack], Config) ->
242
+ maybe_done(Rest, handle_event(end_object, Handler, Config), Stack, Config);
243
+ object(<<?newline, Rest/binary>>, Handler, Stack, Config) ->
244
+ object(Rest, Handler, Stack, Config);
245
+ object(<<?tab, Rest/binary>>, Handler, Stack, Config) ->
246
+ object(Rest, Handler, Stack, Config);
247
+ object(<<?cr, Rest/binary>>, Handler, Stack, Config) ->
248
+ object(Rest, Handler, Stack, Config);
249
+ object(<<?singlequote, Rest/binary>>, Handler, Stack, Config=#config{strict_single_quotes=false}) ->
250
+ string(Rest, Handler, [singlequote|Stack], Config);
251
+ object(<<?solidus, Rest/binary>>, Handler, Stack, Config=#config{strict_comments=true}) ->
252
+ ?error(object, <<?solidus, Rest/binary>>, Handler, Stack, Config);
253
+ object(<<?solidus, ?solidus, Rest/binary>>, Handler, Stack, Config) ->
254
+ comment(Rest, Handler, object, [comment|Stack], Config);
255
+ object(<<?solidus, ?star, Rest/binary>>, Handler, Stack, Config) ->
256
+ comment(Rest, Handler, object, [multicomment|Stack], Config);
257
+ object(<<?solidus>>, Handler, Stack, Config) ->
258
+ incomplete(object, <<?solidus>>, Handler, Stack, Config);
259
+ object(<<>>, Handler, Stack, Config) ->
260
+ incomplete(object, <<>>, Handler, Stack, Config);
261
+ object(Bin, Handler, Stack, Config) ->
262
+ ?error(object, Bin, Handler, Stack, Config).
263
+
264
+
265
+ array(<<?end_array, Rest/binary>>, Handler, [array|Stack], Config) ->
266
+ maybe_done(Rest, handle_event(end_array, Handler, Config), Stack, Config);
267
+ array(<<?space, Rest/binary>>, Handler, Stack, Config) ->
268
+ array(Rest, Handler, Stack, Config);
269
+ array(<<?newline, Rest/binary>>, Handler, Stack, Config) ->
270
+ array(Rest, Handler, Stack, Config);
271
+ array(<<?tab, Rest/binary>>, Handler, Stack, Config) ->
272
+ array(Rest, Handler, Stack, Config);
273
+ array(<<?cr, Rest/binary>>, Handler, Stack, Config) ->
274
+ array(Rest, Handler, Stack, Config);
275
+ array(<<?solidus, Rest/binary>>, Handler, Stack, Config=#config{strict_comments=true}) ->
276
+ value(<<?solidus, Rest/binary>>, Handler, Stack, Config);
277
+ array(<<?solidus, ?solidus, Rest/binary>>, Handler, Stack, Config) ->
278
+ comment(Rest, Handler, array, [comment|Stack], Config);
279
+ array(<<?solidus, ?star, Rest/binary>>, Handler, Stack, Config) ->
280
+ comment(Rest, Handler, array, [multicomment|Stack], Config);
281
+ array(<<?solidus>>, Handler, Stack, Config) ->
282
+ incomplete(array, <<?solidus>>, Handler, Stack, Config);
283
+ array(<<>>, Handler, Stack, Config) ->
284
+ incomplete(array, <<>>, Handler, Stack, Config);
285
+ array(Bin, Handler, Stack, Config) ->
286
+ value(Bin, Handler, Stack, Config).
287
+
288
+
289
+ colon(<<?colon, Rest/binary>>, Handler, [key|Stack], Config) ->
290
+ value(Rest, Handler, [object|Stack], Config);
291
+ colon(<<?space, Rest/binary>>, Handler, Stack, Config) ->
292
+ colon(Rest, Handler, Stack, Config);
293
+ colon(<<?newline, Rest/binary>>, Handler, Stack, Config) ->
294
+ colon(Rest, Handler, Stack, Config);
295
+ colon(<<?tab, Rest/binary>>, Handler, Stack, Config) ->
296
+ colon(Rest, Handler, Stack, Config);
297
+ colon(<<?cr, Rest/binary>>, Handler, Stack, Config) ->
298
+ colon(Rest, Handler, Stack, Config);
299
+ colon(<<?solidus, Rest/binary>>, Handler, Stack, Config=#config{strict_comments=true}) ->
300
+ ?error(colon, <<?solidus, Rest/binary>>, Handler, Stack, Config);
301
+ colon(<<?solidus, ?solidus, Rest/binary>>, Handler, Stack, Config) ->
302
+ comment(Rest, Handler, colon, [comment|Stack], Config);
303
+ colon(<<?solidus, ?star, Rest/binary>>, Handler, Stack, Config) ->
304
+ comment(Rest, Handler, colon, [multicomment|Stack], Config);
305
+ colon(<<?solidus>>, Handler, Stack, Config) ->
306
+ incomplete(colon, <<?solidus>>, Handler, Stack, Config);
307
+ colon(<<>>, Handler, Stack, Config) ->
308
+ incomplete(colon, <<>>, Handler, Stack, Config);
309
+ colon(Bin, Handler, Stack, Config) ->
310
+ ?error(colon, Bin, Handler, Stack, Config).
311
+
312
+
313
+ key(<<?doublequote, Rest/binary>>, Handler, Stack, Config) ->
314
+ string(Rest, Handler, Stack, Config);
315
+ key(<<?space, Rest/binary>>, Handler, Stack, Config) ->
316
+ key(Rest, Handler, Stack, Config);
317
+ key(<<?end_object, Rest/binary>>, Handler, [key|Stack], Config=#config{strict_commas=false}) ->
318
+ maybe_done(<<?end_object, Rest/binary>>, Handler, [object|Stack], Config);
319
+ key(<<?newline, Rest/binary>>, Handler, Stack, Config) ->
320
+ key(Rest, Handler, Stack, Config);
321
+ key(<<?tab, Rest/binary>>, Handler, Stack, Config) ->
322
+ key(Rest, Handler, Stack, Config);
323
+ key(<<?cr, Rest/binary>>, Handler, Stack, Config) ->
324
+ key(Rest, Handler, Stack, Config);
325
+ key(<<?singlequote, Rest/binary>>, Handler, Stack, Config=#config{strict_single_quotes=false}) ->
326
+ string(Rest, Handler, [singlequote|Stack], Config);
327
+ key(<<?solidus, Rest/binary>>, Handler, Stack, Config=#config{strict_comments=true}) ->
328
+ ?error(key, <<?solidus, Rest/binary>>, Handler, Stack, Config);
329
+ key(<<?solidus, ?solidus, Rest/binary>>, Handler, Stack, Config) ->
330
+ comment(Rest, Handler, key, [comment|Stack], Config);
331
+ key(<<?solidus, ?star, Rest/binary>>, Handler, Stack, Config) ->
332
+ comment(Rest, Handler, key, [multicomment|Stack], Config);
333
+ key(<<?solidus>>, Handler, Stack, Config) ->
334
+ incomplete(key, <<?solidus>>, Handler, Stack, Config);
335
+ key(<<>>, Handler, Stack, Config) ->
336
+ incomplete(key, <<>>, Handler, Stack, Config);
337
+ key(Bin, Handler, Stack, Config) ->
338
+ ?error(key, Bin, Handler, Stack, Config).
339
+
340
+
341
+ %% note that if you encounter an error from string and you can't find the clause that
342
+ %% caused it here, it might be in unescape below
343
+ string(Bin, Handler, Stack, Config) ->
344
+ string(Bin, Handler, [], Stack, Config).
345
+
346
+
347
+ string(<<?doublequote, Rest/binary>>, Handler, Acc, Stack, Config) ->
348
+ doublequote(Rest, Handler, Acc, Stack, Config);
349
+ string(<<?singlequote, Rest/binary>>, Handler, Acc, Stack, Config) ->
350
+ singlequote(Rest, Handler, Acc, Stack, Config);
351
+ string(<<?solidus, Rest/binary>>, Handler, Acc, Stack, Config) ->
352
+ string(Rest, Handler, [Acc, maybe_replace(?solidus, Config)], Stack, Config);
353
+ string(<<?rsolidus/utf8, Rest/binary>>, Handler, Acc, Stack, Config) ->
354
+ unescape(Rest, Handler, Acc, Stack, Config);
355
+ %% TODO this is pretty gross and i don't like it
356
+ string(<<X/utf8, Rest/binary>> = Bin, Handler, Acc, Stack, Config=#config{uescape=true}) ->
357
+ case X of
358
+ X when X < 16#80 -> count(Bin, Handler, Acc, Stack, Config);
359
+ X -> string(Rest, Handler, [Acc, json_escape_sequence(X)], Stack, Config)
360
+ end;
361
+ %% u+2028
362
+ string(<<226, 128, 168, Rest/binary>>, Handler, Acc, Stack, Config) ->
363
+ string(Rest, Handler, [Acc, maybe_replace(16#2028, Config)], Stack, Config);
364
+ %% u+2029
365
+ string(<<226, 128, 169, Rest/binary>>, Handler, Acc, Stack, Config) ->
366
+ string(Rest, Handler, [Acc, maybe_replace(16#2029, Config)], Stack, Config);
367
+ string(<<X/utf8, _/binary>> = Bin, Handler, Acc, Stack, Config=#config{strict_control_codes=true}) when X > 16#1f ->
368
+ count(Bin, Handler, Acc, Stack, Config);
369
+ string(<<_/utf8, _/binary>> = Bin, Handler, Acc, Stack, Config=#config{strict_control_codes=false}) ->
370
+ count(Bin, Handler, Acc, Stack, Config);
371
+ %% necessary for bytes that are badly formed utf8 that won't match in `count`
372
+ string(<<X, Rest/binary>>, Handler, Acc, Stack, Config=#config{dirty_strings=true}) ->
373
+ string(Rest, Handler, [Acc, X], Stack, Config);
374
+ %% u+fffe and u+ffff for R14BXX (subsequent runtimes will happily match with /utf8
375
+ string(<<239, 191, 190, Rest/binary>>, Handler, Acc, Stack, Config) ->
376
+ string(Rest, Handler, [Acc, <<16#fffe/utf8>>], Stack, Config);
377
+ string(<<239, 191, 191, Rest/binary>>, Handler, Acc, Stack, Config) ->
378
+ string(Rest, Handler, [Acc, <<16#ffff/utf8>>], Stack, Config);
379
+ string(<<>>, Handler, Acc, Stack, Config) ->
380
+ incomplete(string, <<>>, Handler, Acc, Stack, Config);
381
+ string(<<X>>, Handler, Acc, Stack, Config) when X >= 2#11000000 ->
382
+ incomplete(string, <<X>>, Handler, Acc, Stack, Config);
383
+ string(<<X, Y>>, Handler, Acc, Stack, Config) when X >= 2#11100000, Y >= 2#10000000 ->
384
+ incomplete(string, <<X, Y>>, Handler, Acc, Stack, Config);
385
+ string(<<X, Y, Z>>, Handler, Acc, Stack, Config)
386
+ when X >= 2#11100000, Y >= 2#10000000, Z >= 2#10000000 ->
387
+ incomplete(string, <<X, Y, Z>>, Handler, Acc, Stack, Config);
388
+ %% surrogates
389
+ string(<<237, X, _, Rest/binary>>, Handler, Acc, Stack, Config=#config{strict_utf8=false})
390
+ when X >= 160 ->
391
+ string(Rest, Handler, [Acc, <<16#fffd/utf8>>], Stack, Config);
392
+ %% overlong encodings and missing continuations of a 2 byte sequence
393
+ string(<<X, Rest/binary>>, Handler, Acc, Stack, Config=#config{strict_utf8=false})
394
+ when X >= 192, X =< 223 ->
395
+ strip_continuations(Rest, Handler, Acc, Stack, Config, 1);
396
+ %% overlong encodings and missing continuations of a 3 byte sequence
397
+ string(<<X, Rest/binary>>, Handler, Acc, Stack, Config=#config{strict_utf8=false})
398
+ when X >= 224, X =< 239 ->
399
+ strip_continuations(Rest, Handler, Acc, Stack, Config, 2);
400
+ %% overlong encodings and missing continuations of a 4 byte sequence
401
+ string(<<X, Rest/binary>>, Handler, Acc, Stack, Config=#config{strict_utf8=false})
402
+ when X >= 240, X =< 247 ->
403
+ strip_continuations(Rest, Handler, Acc, Stack, Config, 3);
404
+ %% incompletes and unexpected bytes, including orphan continuations
405
+ string(<<_, Rest/binary>>, Handler, Acc, Stack, Config=#config{strict_utf8=false}) ->
406
+ string(Rest, Handler, [Acc, <<16#fffd/utf8>>], Stack, Config);
407
+ string(Bin, Handler, Acc, Stack, Config) -> ?error(string, Bin, Handler, Acc, Stack, Config).
408
+
409
+
410
+ count(Bin, Handler, Acc, Stack, Config) ->
411
+ Size = count(Bin, 0, Config),
412
+ <<Clean:Size/binary, Rest/binary>> = Bin,
413
+ string(Rest, Handler, [Acc, Clean], Stack, Config).
414
+
415
+
416
+ %% explicitly whitelist ascii set for faster parsing. really? really. someone should
417
+ %% submit a patch that unrolls simple guards
418
+ count(<<32, Rest/binary>>, N, Config) ->
419
+ count(Rest, N + 1, Config);
420
+ count(<<33, Rest/binary>>, N, Config) ->
421
+ count(Rest, N + 1, Config);
422
+ count(<<?doublequote, _/binary>>, N, _) -> N;
423
+ count(<<35, Rest/binary>>, N, Config) ->
424
+ count(Rest, N + 1, Config);
425
+ count(<<36, Rest/binary>>, N, Config) ->
426
+ count(Rest, N + 1, Config);
427
+ count(<<37, Rest/binary>>, N, Config) ->
428
+ count(Rest, N + 1, Config);
429
+ count(<<38, Rest/binary>>, N, Config) ->
430
+ count(Rest, N + 1, Config);
431
+ count(<<?singlequote, _/binary>>, N, _) -> N;
432
+ count(<<40, Rest/binary>>, N, Config) ->
433
+ count(Rest, N + 1, Config);
434
+ count(<<41, Rest/binary>>, N, Config) ->
435
+ count(Rest, N + 1, Config);
436
+ count(<<42, Rest/binary>>, N, Config) ->
437
+ count(Rest, N + 1, Config);
438
+ count(<<43, Rest/binary>>, N, Config) ->
439
+ count(Rest, N + 1, Config);
440
+ count(<<44, Rest/binary>>, N, Config) ->
441
+ count(Rest, N + 1, Config);
442
+ count(<<45, Rest/binary>>, N, Config) ->
443
+ count(Rest, N + 1, Config);
444
+ count(<<46, Rest/binary>>, N, Config) ->
445
+ count(Rest, N + 1, Config);
446
+ count(<<?solidus, _/binary>>, N, _) -> N;
447
+ count(<<48, Rest/binary>>, N, Config) ->
448
+ count(Rest, N + 1, Config);
449
+ count(<<49, Rest/binary>>, N, Config) ->
450
+ count(Rest, N + 1, Config);
451
+ count(<<50, Rest/binary>>, N, Config) ->
452
+ count(Rest, N + 1, Config);
453
+ count(<<51, Rest/binary>>, N, Config) ->
454
+ count(Rest, N + 1, Config);
455
+ count(<<52, Rest/binary>>, N, Config) ->
456
+ count(Rest, N + 1, Config);
457
+ count(<<53, Rest/binary>>, N, Config) ->
458
+ count(Rest, N + 1, Config);
459
+ count(<<54, Rest/binary>>, N, Config) ->
460
+ count(Rest, N + 1, Config);
461
+ count(<<55, Rest/binary>>, N, Config) ->
462
+ count(Rest, N + 1, Config);
463
+ count(<<56, Rest/binary>>, N, Config) ->
464
+ count(Rest, N + 1, Config);
465
+ count(<<57, Rest/binary>>, N, Config) ->
466
+ count(Rest, N + 1, Config);
467
+ count(<<58, Rest/binary>>, N, Config) ->
468
+ count(Rest, N + 1, Config);
469
+ count(<<59, Rest/binary>>, N, Config) ->
470
+ count(Rest, N + 1, Config);
471
+ count(<<60, Rest/binary>>, N, Config) ->
472
+ count(Rest, N + 1, Config);
473
+ count(<<61, Rest/binary>>, N, Config) ->
474
+ count(Rest, N + 1, Config);
475
+ count(<<62, Rest/binary>>, N, Config) ->
476
+ count(Rest, N + 1, Config);
477
+ count(<<63, Rest/binary>>, N, Config) ->
478
+ count(Rest, N + 1, Config);
479
+ count(<<64, Rest/binary>>, N, Config) ->
480
+ count(Rest, N + 1, Config);
481
+ count(<<65, Rest/binary>>, N, Config) ->
482
+ count(Rest, N + 1, Config);
483
+ count(<<66, Rest/binary>>, N, Config) ->
484
+ count(Rest, N + 1, Config);
485
+ count(<<67, Rest/binary>>, N, Config) ->
486
+ count(Rest, N + 1, Config);
487
+ count(<<68, Rest/binary>>, N, Config) ->
488
+ count(Rest, N + 1, Config);
489
+ count(<<69, Rest/binary>>, N, Config) ->
490
+ count(Rest, N + 1, Config);
491
+ count(<<70, Rest/binary>>, N, Config) ->
492
+ count(Rest, N + 1, Config);
493
+ count(<<71, Rest/binary>>, N, Config) ->
494
+ count(Rest, N + 1, Config);
495
+ count(<<72, Rest/binary>>, N, Config) ->
496
+ count(Rest, N + 1, Config);
497
+ count(<<73, Rest/binary>>, N, Config) ->
498
+ count(Rest, N + 1, Config);
499
+ count(<<74, Rest/binary>>, N, Config) ->
500
+ count(Rest, N + 1, Config);
501
+ count(<<75, Rest/binary>>, N, Config) ->
502
+ count(Rest, N + 1, Config);
503
+ count(<<76, Rest/binary>>, N, Config) ->
504
+ count(Rest, N + 1, Config);
505
+ count(<<77, Rest/binary>>, N, Config) ->
506
+ count(Rest, N + 1, Config);
507
+ count(<<78, Rest/binary>>, N, Config) ->
508
+ count(Rest, N + 1, Config);
509
+ count(<<79, Rest/binary>>, N, Config) ->
510
+ count(Rest, N + 1, Config);
511
+ count(<<80, Rest/binary>>, N, Config) ->
512
+ count(Rest, N + 1, Config);
513
+ count(<<81, Rest/binary>>, N, Config) ->
514
+ count(Rest, N + 1, Config);
515
+ count(<<82, Rest/binary>>, N, Config) ->
516
+ count(Rest, N + 1, Config);
517
+ count(<<83, Rest/binary>>, N, Config) ->
518
+ count(Rest, N + 1, Config);
519
+ count(<<84, Rest/binary>>, N, Config) ->
520
+ count(Rest, N + 1, Config);
521
+ count(<<85, Rest/binary>>, N, Config) ->
522
+ count(Rest, N + 1, Config);
523
+ count(<<86, Rest/binary>>, N, Config) ->
524
+ count(Rest, N + 1, Config);
525
+ count(<<87, Rest/binary>>, N, Config) ->
526
+ count(Rest, N + 1, Config);
527
+ count(<<88, Rest/binary>>, N, Config) ->
528
+ count(Rest, N + 1, Config);
529
+ count(<<89, Rest/binary>>, N, Config) ->
530
+ count(Rest, N + 1, Config);
531
+ count(<<90, Rest/binary>>, N, Config) ->
532
+ count(Rest, N + 1, Config);
533
+ count(<<91, Rest/binary>>, N, Config) ->
534
+ count(Rest, N + 1, Config);
535
+ count(<<?rsolidus, _/binary>>, N, _) -> N;
536
+ count(<<93, Rest/binary>>, N, Config) ->
537
+ count(Rest, N + 1, Config);
538
+ count(<<94, Rest/binary>>, N, Config) ->
539
+ count(Rest, N + 1, Config);
540
+ count(<<95, Rest/binary>>, N, Config) ->
541
+ count(Rest, N + 1, Config);
542
+ count(<<96, Rest/binary>>, N, Config) ->
543
+ count(Rest, N + 1, Config);
544
+ count(<<97, Rest/binary>>, N, Config) ->
545
+ count(Rest, N + 1, Config);
546
+ count(<<98, Rest/binary>>, N, Config) ->
547
+ count(Rest, N + 1, Config);
548
+ count(<<99, Rest/binary>>, N, Config) ->
549
+ count(Rest, N + 1, Config);
550
+ count(<<100, Rest/binary>>, N, Config) ->
551
+ count(Rest, N + 1, Config);
552
+ count(<<101, Rest/binary>>, N, Config) ->
553
+ count(Rest, N + 1, Config);
554
+ count(<<102, Rest/binary>>, N, Config) ->
555
+ count(Rest, N + 1, Config);
556
+ count(<<103, Rest/binary>>, N, Config) ->
557
+ count(Rest, N + 1, Config);
558
+ count(<<104, Rest/binary>>, N, Config) ->
559
+ count(Rest, N + 1, Config);
560
+ count(<<105, Rest/binary>>, N, Config) ->
561
+ count(Rest, N + 1, Config);
562
+ count(<<106, Rest/binary>>, N, Config) ->
563
+ count(Rest, N + 1, Config);
564
+ count(<<107, Rest/binary>>, N, Config) ->
565
+ count(Rest, N + 1, Config);
566
+ count(<<108, Rest/binary>>, N, Config) ->
567
+ count(Rest, N + 1, Config);
568
+ count(<<109, Rest/binary>>, N, Config) ->
569
+ count(Rest, N + 1, Config);
570
+ count(<<110, Rest/binary>>, N, Config) ->
571
+ count(Rest, N + 1, Config);
572
+ count(<<111, Rest/binary>>, N, Config) ->
573
+ count(Rest, N + 1, Config);
574
+ count(<<112, Rest/binary>>, N, Config) ->
575
+ count(Rest, N + 1, Config);
576
+ count(<<113, Rest/binary>>, N, Config) ->
577
+ count(Rest, N + 1, Config);
578
+ count(<<114, Rest/binary>>, N, Config) ->
579
+ count(Rest, N + 1, Config);
580
+ count(<<115, Rest/binary>>, N, Config) ->
581
+ count(Rest, N + 1, Config);
582
+ count(<<116, Rest/binary>>, N, Config) ->
583
+ count(Rest, N + 1, Config);
584
+ count(<<117, Rest/binary>>, N, Config) ->
585
+ count(Rest, N + 1, Config);
586
+ count(<<118, Rest/binary>>, N, Config) ->
587
+ count(Rest, N + 1, Config);
588
+ count(<<119, Rest/binary>>, N, Config) ->
589
+ count(Rest, N + 1, Config);
590
+ count(<<120, Rest/binary>>, N, Config) ->
591
+ count(Rest, N + 1, Config);
592
+ count(<<121, Rest/binary>>, N, Config) ->
593
+ count(Rest, N + 1, Config);
594
+ count(<<122, Rest/binary>>, N, Config) ->
595
+ count(Rest, N + 1, Config);
596
+ count(<<123, Rest/binary>>, N, Config) ->
597
+ count(Rest, N + 1, Config);
598
+ count(<<124, Rest/binary>>, N, Config) ->
599
+ count(Rest, N + 1, Config);
600
+ count(<<125, Rest/binary>>, N, Config) ->
601
+ count(Rest, N + 1, Config);
602
+ count(<<126, Rest/binary>>, N, Config) ->
603
+ count(Rest, N + 1, Config);
604
+ count(<<127, Rest/binary>>, N, Config) ->
605
+ count(Rest, N + 1, Config);
606
+ count(<<_, Rest/binary>>, N, Config=#config{dirty_strings=true}) ->
607
+ count(Rest, N + 1, Config);
608
+ count(<<_/utf8, _/binary>>, N, #config{uescape=true}) -> N;
609
+ count(<<X/utf8, Rest/binary>>, N, Config=#config{strict_control_codes=false}) when X < 32 ->
610
+ count(Rest, N + 1, Config);
611
+ count(<<X/utf8, _/binary>>, N, #config{strict_control_codes=true}) when X < 32 -> N;
612
+ count(<<X/utf8, Rest/binary>>, N, Config) ->
613
+ case X of
614
+ X when X < 16#800 -> count(Rest, N + 2, Config);
615
+ %% jsonp escaping
616
+ 16#2028 -> N;
617
+ 16#2029 -> N;
618
+ X when X < 16#10000 -> count(Rest, N + 3, Config);
619
+ _ -> count(Rest, N + 4, Config)
620
+ end;
621
+ count(_, N, _) -> N.
622
+
623
+
624
+ doublequote(Rest, Handler, Acc, [key|_] = Stack, Config) ->
625
+ colon(Rest, handle_event({key, iolist_to_binary(Acc)}, Handler, Config), Stack, Config);
626
+ doublequote(Rest, Handler, Acc, [singlequote|_] = Stack, Config) ->
627
+ string(Rest, Handler, [Acc, maybe_replace(?doublequote, Config)], Stack, Config);
628
+ doublequote(<<>>, Handler, Acc, [singlequote|_] = Stack, Config) ->
629
+ incomplete(string, <<?doublequote>>, Handler, Acc, Stack, Config);
630
+ doublequote(Rest, Handler, Acc, Stack, Config) ->
631
+ maybe_done(Rest, handle_event({string, iolist_to_binary(Acc)}, Handler, Config), Stack, Config).
632
+
633
+
634
+ singlequote(Rest, Handler, Acc, [singlequote, key|Stack], Config) ->
635
+ colon(Rest, handle_event({key, iolist_to_binary(Acc)}, Handler, Config), [key|Stack], Config);
636
+ singlequote(Rest, Handler, Acc, [singlequote|Stack], Config) ->
637
+ maybe_done(Rest, handle_event({string, iolist_to_binary(Acc)}, Handler, Config), Stack, Config);
638
+ singlequote(Rest, Handler, Acc, Stack, Config) ->
639
+ string(Rest, Handler, [Acc, ?singlequote], Stack, Config).
640
+
641
+
642
+ %% strips continuation bytes after bad utf bytes, guards against both too short
643
+ %% and overlong sequences. N is the maximum number of bytes to strip
644
+ strip_continuations(<<Rest/binary>>, Handler, Acc, Stack, Config, 0) ->
645
+ string(Rest, Handler, [Acc, <<16#fffd/utf8>>], Stack, Config);
646
+ strip_continuations(<<X, Rest/binary>>, Handler, Acc, Stack, Config, N) when X >= 128, X =< 191 ->
647
+ strip_continuations(Rest, Handler, Acc, Stack, Config, N - 1);
648
+ %% if end of input is reached before stripping the max number of continuations
649
+ %% possible magic numbers are reinserted into the stream that get us back to
650
+ %% the same state without complicated machinery
651
+ strip_continuations(<<>>, Handler, Acc, Stack, Config, N) ->
652
+ case N of
653
+ 1 -> incomplete(string, <<192>>, Handler, Acc, Stack, Config);
654
+ 2 -> incomplete(string, <<224>>, Handler, Acc, Stack, Config);
655
+ 3 -> incomplete(string, <<240>>, Handler, Acc, Stack, Config)
656
+ end;
657
+ %% not a continuation byte, insert a replacement character for sequence thus
658
+ %% far and dispatch back to string
659
+ strip_continuations(<<Rest/binary>>, Handler, Acc, Stack, Config, _) ->
660
+ string(Rest, Handler, [Acc, <<16#fffd/utf8>>], Stack, Config).
661
+
662
+
663
+ %% this all gets really gross and should probably eventually be folded into
664
+ %% but for now it fakes being part of string on incompletes and errors
665
+ unescape(<<?rsolidus, Rest/binary>>, Handler, Acc, Stack, Config=#config{dirty_strings=true}) ->
666
+ string(<<?rsolidus, Rest/binary>>, Handler, [Acc, <<?rsolidus>>], Stack, Config);
667
+ unescape(<<C, Rest/binary>>, Handler, Acc, Stack, Config=#config{dirty_strings=true}) ->
668
+ string(Rest, Handler, [Acc, <<?rsolidus, C>>], Stack, Config);
669
+ unescape(<<$b, Rest/binary>>, Handler, Acc, Stack, Config) ->
670
+ string(Rest, Handler, [Acc, maybe_replace($\b, Config)], Stack, Config);
671
+ unescape(<<$f, Rest/binary>>, Handler, Acc, Stack, Config) ->
672
+ string(Rest, Handler, [Acc, maybe_replace($\f, Config)], Stack, Config);
673
+ unescape(<<$n, Rest/binary>>, Handler, Acc, Stack, Config) ->
674
+ string(Rest, Handler, [Acc, maybe_replace($\n, Config)], Stack, Config);
675
+ unescape(<<$r, Rest/binary>>, Handler, Acc, Stack, Config) ->
676
+ string(Rest, Handler, [Acc, maybe_replace($\r, Config)], Stack, Config);
677
+ unescape(<<$t, Rest/binary>>, Handler, Acc, Stack, Config) ->
678
+ string(Rest, Handler, [Acc, maybe_replace($\t, Config)], Stack, Config);
679
+ unescape(<<?doublequote, Rest/binary>>, Handler, Acc, Stack, Config) ->
680
+ string(Rest, Handler, [Acc, maybe_replace($\", Config)], Stack, Config);
681
+ unescape(<<?singlequote, Rest/binary>>, Handler, Acc, Stack, Config=#config{strict_single_quotes=false}) ->
682
+ string(Rest, Handler, [Acc, <<?singlequote>>], Stack, Config);
683
+ unescape(<<?rsolidus, Rest/binary>>, Handler, Acc, Stack, Config) ->
684
+ string(Rest, Handler, [Acc, maybe_replace($\\, Config)], Stack, Config);
685
+ unescape(<<?solidus, Rest/binary>>, Handler, Acc, Stack, Config) ->
686
+ string(Rest, Handler, [Acc, maybe_replace($/, Config)], Stack, Config);
687
+ unescape(<<$u, F, A, B, C, ?rsolidus, $u, G, X, Y, Z, Rest/binary>>, Handler, Acc, Stack, Config)
688
+ when (A == $8 orelse A == $9 orelse A == $a orelse A == $b orelse A == $A orelse A == $B),
689
+ (X == $c orelse X == $d orelse X == $e orelse X == $f orelse X == $C orelse X == $D orelse X == $E orelse X == $F),
690
+ (F == $d orelse F == $D),
691
+ (G == $d orelse G == $D),
692
+ ?is_hex(B), ?is_hex(C), ?is_hex(Y), ?is_hex(Z)
693
+ ->
694
+ High = erlang:list_to_integer([$d, A, B, C], 16),
695
+ Low = erlang:list_to_integer([$d, X, Y, Z], 16),
696
+ Codepoint = (High - 16#d800) * 16#400 + (Low - 16#dc00) + 16#10000,
697
+ string(Rest, Handler, [Acc, <<Codepoint/utf8>>], Stack, Config);
698
+ unescape(<<$u, F0, A, B, C, ?rsolidus, $u, W, X, Y, Z, Rest/binary>>, Handler, Acc, Stack, Config)
699
+ when (A == $8 orelse A == $9 orelse A == $a orelse A == $b orelse A == $A orelse A == $B),
700
+ (F0 == $d orelse F0 == $D),
701
+ ?is_hex(B), ?is_hex(C), ?is_hex(W), ?is_hex(X), ?is_hex(Y), ?is_hex(Z)
702
+ ->
703
+ case Config#config.strict_utf8 of
704
+ true -> ?error(string, <<$u, $d, A, B, C, ?rsolidus, $u, W, X, Y, Z, Rest/binary>>, Handler, Acc, Stack, Config);
705
+ false -> string(Rest, Handler, [Acc, <<16#fffd/utf8>>, <<16#fffd/utf8>>], Stack, Config)
706
+ end;
707
+ unescape(<<$u, F, A, B, C, ?rsolidus, Rest/binary>>, Handler, Acc, Stack, Config)
708
+ when (A == $8 orelse A == $9 orelse A == $a orelse A == $b orelse A == $A orelse A == $B),
709
+ (F == $d orelse F == $D),
710
+ ?is_hex(B), ?is_hex(C)
711
+ ->
712
+ incomplete(string, <<?rsolidus, $u, $d, A, B, C, ?rsolidus, Rest/binary>>, Handler, Acc, Stack, Config);
713
+ unescape(<<$u, F, A, B, C>>, Handler, Acc, Stack, Config)
714
+ when (A == $8 orelse A == $9 orelse A == $a orelse A == $b orelse A == $A orelse A == $B),
715
+ (F == $d orelse F == $D),
716
+ ?is_hex(B), ?is_hex(C)
717
+ ->
718
+ incomplete(string, <<?rsolidus, $u, $d, A, B, C>>, Handler, Acc, Stack, Config);
719
+ unescape(<<$u, A, B, C, D, Rest/binary>>, Handler, Acc, Stack, Config)
720
+ when ?is_hex(A), ?is_hex(B), ?is_hex(C), ?is_hex(D) ->
721
+ case erlang:list_to_integer([A, B, C, D], 16) of
722
+ Codepoint when Codepoint < 16#d800; Codepoint > 16#dfff ->
723
+ string(Rest, Handler, [Acc, maybe_replace(Codepoint, Config)], Stack, Config);
724
+ _ when Config#config.strict_utf8 ->
725
+ ?error(string, <<?rsolidus, $u, A, B, C, D, Rest/binary>>, Handler, Acc, Stack, Config);
726
+ _ -> string(Rest, Handler, [Acc, <<16#fffd/utf8>>], Stack, Config)
727
+ end;
728
+ unescape(Bin, Handler, Acc, Stack, Config) ->
729
+ case is_partial_escape(Bin) of
730
+ true -> incomplete(string, <<?rsolidus/utf8, Bin/binary>>, Handler, Acc, Stack, Config);
731
+ false -> case Config#config.strict_escapes of
732
+ true -> ?error(string, <<?rsolidus, Bin/binary>>, Handler, Acc, Stack, Config);
733
+ false -> string(Bin, Handler, [Acc, <<?rsolidus>>], Stack, Config)
734
+ end
735
+ end.
736
+
737
+
738
+ is_partial_escape(<<$u, A, B, C>>) when ?is_hex(A), ?is_hex(B), ?is_hex(C) -> true;
739
+ is_partial_escape(<<$u, A, B>>) when ?is_hex(A), ?is_hex(B) -> true;
740
+ is_partial_escape(<<$u, A>>) when ?is_hex(A) -> true;
741
+ is_partial_escape(<<$u>>) -> true;
742
+ is_partial_escape(<<>>) -> true;
743
+ is_partial_escape(_) -> false.
744
+
745
+
746
+ maybe_replace(C, #config{dirty_strings=true}) -> <<C>>;
747
+ maybe_replace($\b, #config{escaped_strings=true}) -> <<$\\, $b>>;
748
+ maybe_replace($\t, #config{escaped_strings=true}) -> <<$\\, $t>>;
749
+ maybe_replace($\n, #config{escaped_strings=true}) -> <<$\\, $n>>;
750
+ maybe_replace($\f, #config{escaped_strings=true}) -> <<$\\, $f>>;
751
+ maybe_replace($\r, #config{escaped_strings=true}) -> <<$\\, $r>>;
752
+ maybe_replace($\", #config{escaped_strings=true}) -> <<$\\, $\">>;
753
+ maybe_replace($/, Config=#config{escaped_strings=true}) ->
754
+ case Config#config.escaped_forward_slashes of
755
+ true -> <<$\\, $/>>
756
+ ; false -> <<$/>>
757
+ end;
758
+ maybe_replace($\\, #config{escaped_strings=true}) -> <<$\\, $\\>>;
759
+ maybe_replace(X, Config=#config{escaped_strings=true}) when X == 16#2028; X == 16#2029 ->
760
+ case Config#config.unescaped_jsonp of
761
+ true -> <<X/utf8>>
762
+ ; false -> json_escape_sequence(X)
763
+ end;
764
+ maybe_replace(X, #config{escaped_strings=true}) when X < 32 ->
765
+ json_escape_sequence(X);
766
+ maybe_replace(X, _Config) -> <<X/utf8>>.
767
+
768
+
769
+ %% convert a codepoint to it's \uXXXX equiv.
770
+ json_escape_sequence(X) when X < 65536 ->
771
+ <<A:4, B:4, C:4, D:4>> = <<X:16>>,
772
+ <<$\\, $u, (to_hex(A)), (to_hex(B)), (to_hex(C)), (to_hex(D))>>;
773
+ json_escape_sequence(X) ->
774
+ Adjusted = X - 16#10000,
775
+ <<A:10, B:10>> = <<Adjusted:20>>,
776
+ [json_escape_sequence(A + 16#d800), json_escape_sequence(B + 16#dc00)].
777
+
778
+
779
+ %% ascii "1" is [49], "2" is [50], etc...
780
+ to_hex(10) -> $a;
781
+ to_hex(11) -> $b;
782
+ to_hex(12) -> $c;
783
+ to_hex(13) -> $d;
784
+ to_hex(14) -> $e;
785
+ to_hex(15) -> $f;
786
+ to_hex(X) -> X + 48.
787
+
788
+
789
+ number(<<$e, Rest/binary>>, Handler, Acc, [integer|Stack], Config) ->
790
+ number(Rest, Handler, [Acc, $., $0, $e], [e|Stack], Config);
791
+ number(<<$E, Rest/binary>>, Handler, Acc, [integer|Stack], Config) ->
792
+ number(Rest, Handler, [Acc, $., $0, $e], [e|Stack], Config);
793
+ number(<<$e, Rest/binary>>, Handler, Acc, [zero|Stack], Config) ->
794
+ number(Rest, Handler, [Acc, $., $0, $e], [e|Stack], Config);
795
+ number(<<$E, Rest/binary>>, Handler, Acc, [zero|Stack], Config) ->
796
+ number(Rest, Handler, [Acc, $., $0, $e], [e|Stack], Config);
797
+ number(<<>>, Handler, Acc, [State|Stack], Config=#config{stream=false}) ->
798
+ NumType = case State of
799
+ zero -> integer;
800
+ integer -> integer;
801
+ decimal -> float;
802
+ exp -> float
803
+ end,
804
+ finish_number(<<>>, Handler, {NumType, iolist_to_binary(Acc)}, Stack, Config);
805
+ number(<<>>, Handler, Acc, Stack, Config) ->
806
+ incomplete(number, <<>>, Handler, Acc, Stack, Config);
807
+ number(Bin, Handler, Acc, [State|Stack], Config) ->
808
+ Counted = case State of
809
+ zero -> zero(Bin, 0);
810
+ integer -> integer(Bin, 0);
811
+ negative -> negative(Bin, 0);
812
+ initialdecimal -> initialdecimal(Bin, 0);
813
+ decimal -> decimal(Bin, 0);
814
+ e -> e(Bin, 0);
815
+ ex -> ex(Bin, 0);
816
+ exp -> exp(Bin, 0)
817
+ end,
818
+ case Counted of
819
+ {finish_integer, Size} ->
820
+ <<Clean:Size/binary, Rest/binary>> = Bin,
821
+ finish_number(Rest, Handler, {integer, iolist_to_binary([Acc, Clean])}, Stack, Config);
822
+ {finish_float, Size} ->
823
+ <<Clean:Size/binary, Rest/binary>> = Bin,
824
+ finish_number(Rest, Handler, {float, iolist_to_binary([Acc, Clean])}, Stack, Config);
825
+ {error, Size} ->
826
+ <<Clean:Size/binary, Rest/binary>> = Bin,
827
+ ?error(number, Rest, Handler, [Acc, Clean], Stack, Config);
828
+ {NewState, Size} ->
829
+ <<Clean:Size/binary, Rest/binary>> = Bin,
830
+ number(Rest, Handler, [Acc, Clean], [NewState|Stack], Config)
831
+ end.
832
+
833
+
834
+ zero(<<?decimalpoint, Rest/binary>>, N) -> initialdecimal(Rest, N + 1);
835
+ zero(<<$e, _/binary>>, N) -> {integer, N};
836
+ zero(<<$E, _/binary>>, N) -> {integer, N};
837
+ zero(<<>>, N) -> {zero, N};
838
+ zero(_, N) -> {finish_integer, N}.
839
+
840
+
841
+ integer(<<$0, Rest/binary>>, N) -> integer(Rest, N + 1);
842
+ integer(<<$1, Rest/binary>>, N) -> integer(Rest, N + 1);
843
+ integer(<<$2, Rest/binary>>, N) -> integer(Rest, N + 1);
844
+ integer(<<$3, Rest/binary>>, N) -> integer(Rest, N + 1);
845
+ integer(<<$4, Rest/binary>>, N) -> integer(Rest, N + 1);
846
+ integer(<<$5, Rest/binary>>, N) -> integer(Rest, N + 1);
847
+ integer(<<$6, Rest/binary>>, N) -> integer(Rest, N + 1);
848
+ integer(<<$7, Rest/binary>>, N) -> integer(Rest, N + 1);
849
+ integer(<<$8, Rest/binary>>, N) -> integer(Rest, N + 1);
850
+ integer(<<$9, Rest/binary>>, N) -> integer(Rest, N + 1);
851
+ integer(<<?decimalpoint, Rest/binary>>, N) -> initialdecimal(Rest, N + 1);
852
+ integer(<<$e, _/binary>>, N) -> {integer, N};
853
+ integer(<<$E, _/binary>>, N) -> {integer, N};
854
+ integer(<<>>, N) -> {integer, N};
855
+ integer(_, N) -> {finish_integer, N}.
856
+
857
+
858
+ negative(<<$0, Rest/binary>>, N) -> zero(Rest, N + 1);
859
+ negative(<<$1, Rest/binary>>, N) -> integer(Rest, N + 1);
860
+ negative(<<$2, Rest/binary>>, N) -> integer(Rest, N + 1);
861
+ negative(<<$3, Rest/binary>>, N) -> integer(Rest, N + 1);
862
+ negative(<<$4, Rest/binary>>, N) -> integer(Rest, N + 1);
863
+ negative(<<$5, Rest/binary>>, N) -> integer(Rest, N + 1);
864
+ negative(<<$6, Rest/binary>>, N) -> integer(Rest, N + 1);
865
+ negative(<<$7, Rest/binary>>, N) -> integer(Rest, N + 1);
866
+ negative(<<$8, Rest/binary>>, N) -> integer(Rest, N + 1);
867
+ negative(<<$9, Rest/binary>>, N) -> integer(Rest, N + 1);
868
+ negative(<<>>, N) -> {negative, N};
869
+ negative(_, N) -> {error, N}.
870
+
871
+
872
+ initialdecimal(<<$0, Rest/binary>>, N) -> decimal(Rest, N + 1);
873
+ initialdecimal(<<$1, Rest/binary>>, N) -> decimal(Rest, N + 1);
874
+ initialdecimal(<<$2, Rest/binary>>, N) -> decimal(Rest, N + 1);
875
+ initialdecimal(<<$3, Rest/binary>>, N) -> decimal(Rest, N + 1);
876
+ initialdecimal(<<$4, Rest/binary>>, N) -> decimal(Rest, N + 1);
877
+ initialdecimal(<<$5, Rest/binary>>, N) -> decimal(Rest, N + 1);
878
+ initialdecimal(<<$6, Rest/binary>>, N) -> decimal(Rest, N + 1);
879
+ initialdecimal(<<$7, Rest/binary>>, N) -> decimal(Rest, N + 1);
880
+ initialdecimal(<<$8, Rest/binary>>, N) -> decimal(Rest, N + 1);
881
+ initialdecimal(<<$9, Rest/binary>>, N) -> decimal(Rest, N + 1);
882
+ initialdecimal(<<>>, N) -> {initialdecimal, N};
883
+ initialdecimal(_, N) -> {error, N}.
884
+
885
+
886
+ decimal(<<$0, Rest/binary>>, N) -> decimal(Rest, N + 1);
887
+ decimal(<<$1, Rest/binary>>, N) -> decimal(Rest, N + 1);
888
+ decimal(<<$2, Rest/binary>>, N) -> decimal(Rest, N + 1);
889
+ decimal(<<$3, Rest/binary>>, N) -> decimal(Rest, N + 1);
890
+ decimal(<<$4, Rest/binary>>, N) -> decimal(Rest, N + 1);
891
+ decimal(<<$5, Rest/binary>>, N) -> decimal(Rest, N + 1);
892
+ decimal(<<$6, Rest/binary>>, N) -> decimal(Rest, N + 1);
893
+ decimal(<<$7, Rest/binary>>, N) -> decimal(Rest, N + 1);
894
+ decimal(<<$8, Rest/binary>>, N) -> decimal(Rest, N + 1);
895
+ decimal(<<$9, Rest/binary>>, N) -> decimal(Rest, N + 1);
896
+ decimal(<<$e, Rest/binary>>, N) -> e(Rest, N + 1);
897
+ decimal(<<$E, Rest/binary>>, N) -> e(Rest, N + 1);
898
+ decimal(<<>>, N) -> {decimal, N};
899
+ decimal(_, N) -> {finish_float, N}.
900
+
901
+
902
+ e(<<$0, Rest/binary>>, N) -> exp(Rest, N + 1);
903
+ e(<<$1, Rest/binary>>, N) -> exp(Rest, N + 1);
904
+ e(<<$2, Rest/binary>>, N) -> exp(Rest, N + 1);
905
+ e(<<$3, Rest/binary>>, N) -> exp(Rest, N + 1);
906
+ e(<<$4, Rest/binary>>, N) -> exp(Rest, N + 1);
907
+ e(<<$5, Rest/binary>>, N) -> exp(Rest, N + 1);
908
+ e(<<$6, Rest/binary>>, N) -> exp(Rest, N + 1);
909
+ e(<<$7, Rest/binary>>, N) -> exp(Rest, N + 1);
910
+ e(<<$8, Rest/binary>>, N) -> exp(Rest, N + 1);
911
+ e(<<$9, Rest/binary>>, N) -> exp(Rest, N + 1);
912
+ e(<<?positive, Rest/binary>>, N) -> ex(Rest, N + 1);
913
+ e(<<?negative, Rest/binary>>, N) -> ex(Rest, N + 1);
914
+ e(<<>>, N) -> {e, N};
915
+ e(_, N) -> {error, N}.
916
+
917
+
918
+ ex(<<$0, Rest/binary>>, N) -> exp(Rest, N + 1);
919
+ ex(<<$1, Rest/binary>>, N) -> exp(Rest, N + 1);
920
+ ex(<<$2, Rest/binary>>, N) -> exp(Rest, N + 1);
921
+ ex(<<$3, Rest/binary>>, N) -> exp(Rest, N + 1);
922
+ ex(<<$4, Rest/binary>>, N) -> exp(Rest, N + 1);
923
+ ex(<<$5, Rest/binary>>, N) -> exp(Rest, N + 1);
924
+ ex(<<$6, Rest/binary>>, N) -> exp(Rest, N + 1);
925
+ ex(<<$7, Rest/binary>>, N) -> exp(Rest, N + 1);
926
+ ex(<<$8, Rest/binary>>, N) -> exp(Rest, N + 1);
927
+ ex(<<$9, Rest/binary>>, N) -> exp(Rest, N + 1);
928
+ ex(<<>>, N) -> {ex, N};
929
+ ex(_, N) -> {error, N}.
930
+
931
+
932
+ exp(<<$0, Rest/binary>>, N) -> exp(Rest, N + 1);
933
+ exp(<<$1, Rest/binary>>, N) -> exp(Rest, N + 1);
934
+ exp(<<$2, Rest/binary>>, N) -> exp(Rest, N + 1);
935
+ exp(<<$3, Rest/binary>>, N) -> exp(Rest, N + 1);
936
+ exp(<<$4, Rest/binary>>, N) -> exp(Rest, N + 1);
937
+ exp(<<$5, Rest/binary>>, N) -> exp(Rest, N + 1);
938
+ exp(<<$6, Rest/binary>>, N) -> exp(Rest, N + 1);
939
+ exp(<<$7, Rest/binary>>, N) -> exp(Rest, N + 1);
940
+ exp(<<$8, Rest/binary>>, N) -> exp(Rest, N + 1);
941
+ exp(<<$9, Rest/binary>>, N) -> exp(Rest, N + 1);
942
+ exp(<<>>, N) -> {exp, N};
943
+ exp(_, N) -> {finish_float, N}.
944
+
945
+
946
+ finish_number(Rest, Handler, Acc, Stack, Config) ->
947
+ maybe_done(Rest, handle_event(format_number(Acc), Handler, Config), Stack, Config).
948
+
949
+ format_number({integer, Acc}) -> {integer, binary_to_integer(Acc)};
950
+ format_number({float, Acc}) -> {float, binary_to_float(Acc)}.
951
+
952
+ true(<<$r, $u, $e, Rest/binary>>, Handler, Stack, Config) ->
953
+ maybe_done(Rest, handle_event({literal, true}, Handler, Config), Stack, Config);
954
+ true(<<$r, $u>>, Handler, Stack, Config) ->
955
+ incomplete(true, <<$r, $u>>, Handler, Stack, Config);
956
+ true(<<$r>>, Handler, Stack, Config) ->
957
+ incomplete(true, <<$r>>, Handler, Stack, Config);
958
+ true(<<>>, Handler, Stack, Config) ->
959
+ incomplete(true, <<>>, Handler, Stack, Config);
960
+ true(Bin, Handler, Stack, Config) ->
961
+ ?error(true, Bin, Handler, Stack, Config).
962
+
963
+
964
+ false(<<$a, $l, $s, $e, Rest/binary>>, Handler, Stack, Config) ->
965
+ maybe_done(Rest, handle_event({literal, false}, Handler, Config), Stack, Config);
966
+ false(<<$a, $l, $s>>, Handler, Stack, Config) ->
967
+ incomplete(false, <<$a, $l, $s>>, Handler, Stack, Config);
968
+ false(<<$a, $l>>, Handler, Stack, Config) ->
969
+ incomplete(false, <<$a, $l>>, Handler, Stack, Config);
970
+ false(<<$a>>, Handler, Stack, Config) ->
971
+ incomplete(false, <<$a>>, Handler, Stack, Config);
972
+ false(<<>>, Handler, Stack, Config) ->
973
+ incomplete(false, <<>>, Handler, Stack, Config);
974
+ false(Bin, Handler, Stack, Config) ->
975
+ ?error(false, Bin, Handler, Stack, Config).
976
+
977
+
978
+ null(<<$u, $l, $l, Rest/binary>>, Handler, Stack, Config) ->
979
+ maybe_done(Rest, handle_event({literal, null}, Handler, Config), Stack, Config);
980
+ null(<<$u, $l>>, Handler, Stack, Config) ->
981
+ incomplete(null, <<$u, $l>>, Handler, Stack, Config);
982
+ null(<<$u>>, Handler, Stack, Config) ->
983
+ incomplete(null, <<$u>>, Handler, Stack, Config);
984
+ null(<<>>, Handler, Stack, Config) ->
985
+ incomplete(null, <<>>, Handler, Stack, Config);
986
+ null(Bin, Handler, Stack, Config) ->
987
+ ?error(null, Bin, Handler, Stack, Config).
988
+
989
+
990
+ comment(<<?newline, Rest/binary>>, Handler, Resume, [comment|Stack], Config) ->
991
+ resume(Rest, Resume, Handler, unused, Stack, Config);
992
+ comment(<<?solidus, ?star, Rest/binary>>, Handler, Resume, Stack, Config) ->
993
+ comment(Rest, Handler, Resume, [multicomment|Stack], Config);
994
+ comment(<<?solidus>>, Handler, Resume, [multicomment|_] = Stack, Config) ->
995
+ incomplete(comment, <<?solidus>>, Handler, Resume, Stack, Config);
996
+ comment(<<?star, ?solidus, Rest/binary>>, Handler, Resume, [multicomment|Stack], Config) ->
997
+ case Stack of
998
+ [multicomment|_] -> comment(Rest, Handler, Resume, Stack, Config);
999
+ _ -> resume(Rest, Resume, Handler, unused, Stack, Config)
1000
+ end;
1001
+ comment(<<?star>>, Handler, Resume, [multicomment|_] = Stack, Config) ->
1002
+ incomplete(comment, <<?star>>, Handler, Resume, Stack, Config);
1003
+ comment(<<_/utf8, Rest/binary>>, Handler, Resume, Stack, Config) ->
1004
+ comment(Rest, Handler, Resume, Stack, Config);
1005
+ comment(<<_, Rest/binary>>, Handler, Resume, Stack, Config=#config{strict_utf8=false}) ->
1006
+ comment(Rest, Handler, Resume, Stack, Config);
1007
+ comment(<<>>, Handler, done, [Comment], Config=#config{stream=false})
1008
+ when Comment == comment; Comment == multicomment ->
1009
+ resume(<<>>, done, Handler, unused, [], Config);
1010
+ comment(<<>>, Handler, Resume, Stack, Config) ->
1011
+ incomplete(comment, <<>>, Handler, Resume, Stack, Config);
1012
+ comment(Bin, Handler, Resume, Stack, Config) ->
1013
+ ?error(comment, Bin, Handler, Resume, Stack, Config).
1014
+
1015
+
1016
+ maybe_done(<<Rest/binary>>, Handler, [], Config) ->
1017
+ done(Rest, handle_event(end_json, Handler, Config), [], Config);
1018
+ maybe_done(<<?space, Rest/binary>>, Handler, Stack, Config) ->
1019
+ maybe_done(Rest, Handler, Stack, Config);
1020
+ maybe_done(<<?end_object, Rest/binary>>, Handler, [object|Stack], Config) ->
1021
+ maybe_done(Rest, handle_event(end_object, Handler, Config), Stack, Config);
1022
+ maybe_done(<<?end_array, Rest/binary>>, Handler, [array|Stack], Config) ->
1023
+ maybe_done(Rest, handle_event(end_array, Handler, Config), Stack, Config);
1024
+ maybe_done(<<?comma, Rest/binary>>, Handler, [object|Stack], Config) ->
1025
+ key(Rest, Handler, [key|Stack], Config);
1026
+ maybe_done(<<?comma, Rest/binary>>, Handler, [array|_] = Stack, Config) ->
1027
+ value(Rest, Handler, Stack, Config);
1028
+ maybe_done(<<?newline, Rest/binary>>, Handler, Stack, Config) ->
1029
+ maybe_done(Rest, Handler, Stack, Config);
1030
+ maybe_done(<<?tab, Rest/binary>>, Handler, Stack, Config) ->
1031
+ maybe_done(Rest, Handler, Stack, Config);
1032
+ maybe_done(<<?cr, Rest/binary>>, Handler, Stack, Config) ->
1033
+ maybe_done(Rest, Handler, Stack, Config);
1034
+ maybe_done(<<?solidus, Rest/binary>>, Handler, Stack, Config=#config{strict_comments=true}) ->
1035
+ ?error(maybe_done, <<?solidus, Rest/binary>>, Handler, Stack, Config);
1036
+ maybe_done(<<?solidus, ?solidus, Rest/binary>>, Handler, Stack, Config) ->
1037
+ comment(Rest, Handler, maybe_done, [comment|Stack], Config);
1038
+ maybe_done(<<?solidus, ?star, Rest/binary>>, Handler, Stack, Config) ->
1039
+ comment(Rest, Handler, maybe_done, [multicomment|Stack], Config);
1040
+ maybe_done(<<?solidus>>, Handler, Stack, Config) ->
1041
+ incomplete(maybe_done, <<?solidus>>, Handler, Stack, Config);
1042
+ maybe_done(<<>>, Handler, Stack, Config) when length(Stack) > 0 ->
1043
+ incomplete(maybe_done, <<>>, Handler, Stack, Config);
1044
+ maybe_done(Bin, Handler, Stack, Config) ->
1045
+ ?error(maybe_done, Bin, Handler, Stack, Config).
1046
+
1047
+
1048
+ done(<<?space, Rest/binary>>, Handler, [], Config) ->
1049
+ done(Rest, Handler, [], Config);
1050
+ done(<<?newline, Rest/binary>>, Handler, [], Config) ->
1051
+ done(Rest, Handler, [], Config);
1052
+ done(<<?tab, Rest/binary>>, Handler, [], Config) ->
1053
+ done(Rest, Handler, [], Config);
1054
+ done(<<?cr, Rest/binary>>, Handler, [], Config) ->
1055
+ done(Rest, Handler, [], Config);
1056
+ done(<<?solidus, Rest/binary>>, Handler, Stack, Config=#config{strict_comments=true}) ->
1057
+ ?error(done, <<?solidus, Rest/binary>>, Handler, Stack, Config);
1058
+ done(<<?solidus, ?solidus, Rest/binary>>, Handler, Stack, Config) ->
1059
+ comment(Rest, Handler, done, [comment|Stack], Config);
1060
+ done(<<?solidus, ?star, Rest/binary>>, Handler, Stack, Config) ->
1061
+ comment(Rest, Handler, done, [multicomment|Stack], Config);
1062
+ done(<<?solidus>>, Handler, Stack, Config) ->
1063
+ incomplete(done, <<?solidus>>, Handler, Stack, Config);
1064
+ done(Bin, {_Handler, State}, _Stack, #config{return_tail=true}) ->
1065
+ {with_tail,State, Bin};
1066
+ done(<<>>, {Handler, State}, [], Config=#config{stream=true}) ->
1067
+ incomplete(done, <<>>, {Handler, State}, [], Config);
1068
+ done(<<>>, {_Handler, State}, [], _Config) -> State;
1069
+ done(Bin, {Handler, State}, _Stack, Config=#config{multi_term=true}) ->
1070
+ value(Bin, {Handler, Handler:reset(State)}, [], Config);
1071
+ done(Bin, Handler, Stack, Config) -> ?error(done, Bin, Handler, Stack, Config).
1072
+
1073
+
1074
+
1075
+ -ifdef(TEST).
1076
+ -include_lib("eunit/include/eunit.hrl").
1077
+
1078
+
1079
+ json_to_bytes(JSON) -> json_to_bytes(JSON, []).
1080
+
1081
+ json_to_bytes(<<>>, Acc) -> [<<>>] ++ lists:reverse(Acc);
1082
+ json_to_bytes(<<X, Rest/binary>>, Acc) -> json_to_bytes(Rest, [<<X>>] ++ Acc).
1083
+
1084
+
1085
+ decode(JSON) -> decode(JSON, []).
1086
+ decode(JSON, Config) -> (decoder(jsx, [], Config))(JSON).
1087
+
1088
+
1089
+ incremental_decode(JSON) -> incremental_decode(JSON, []).
1090
+ incremental_decode(JSON, Config) ->
1091
+ Final = lists:foldl(
1092
+ fun(Byte, Decoder) -> {incomplete, F} = Decoder(Byte), F end,
1093
+ decoder(jsx, [], [stream] ++ Config),
1094
+ json_to_bytes(JSON)
1095
+ ),
1096
+ Final(end_stream).
1097
+
1098
+
1099
+ %% all these numbers have different representation in erlang than in javascript and
1100
+ %% do not roundtrip like most integers/floats
1101
+ special_number_test_() ->
1102
+ Cases = [
1103
+ % {title, test form, json, opt flags}
1104
+ {"-0", [{integer, 0}, end_json], <<"-0">>},
1105
+ {"-0.0", [{float, 0.0}, end_json], <<"-0.0">>},
1106
+ {"0e0", [{float, 0.0}, end_json], <<"0e0">>},
1107
+ {"0e4", [{float, 0.0}, end_json], <<"0e4">>},
1108
+ {"1e0", [{float, 1.0}, end_json], <<"1e0">>},
1109
+ {"-1e0", [{float, -1.0}, end_json], <<"-1e0">>},
1110
+ {"-0e0", [{float, -0.0}, end_json], <<"-0e0">>},
1111
+ {"1e4", [{float, 1.0e4}, end_json], <<"1e4">>},
1112
+ {"number terminated by whitespace",
1113
+ [start_array, {integer, 1}, end_array, end_json],
1114
+ <<"[ 1 ]">>
1115
+ },
1116
+ {"number terminated by comma",
1117
+ [start_array, {integer, 1}, {integer, 1}, end_array, end_json],
1118
+ <<"[ 1, 1 ]">>
1119
+ },
1120
+ {"number terminated by comma in object",
1121
+ [start_object, {key, <<"x">>}, {integer, 1}, {key, <<"y">>}, {integer, 1}, end_object, end_json],
1122
+ <<"{\"x\": 1, \"y\": 1}">>
1123
+ }
1124
+ ],
1125
+ [{Title, ?_assertEqual(Events, decode(JSON))}
1126
+ || {Title, Events, JSON} <- Cases
1127
+ ] ++
1128
+ [{Title ++ " (incremental)", ?_assertEqual(Events, incremental_decode(JSON))}
1129
+ || {Title, Events, JSON} <- Cases
1130
+ ].
1131
+
1132
+
1133
+ comments_test_() ->
1134
+ Cases = [
1135
+ % {title, test form, json, opt flags}
1136
+ {"preceeding // comment",
1137
+ [start_array, end_array, end_json],
1138
+ <<"// comment ", ?newline, "[]">>
1139
+ },
1140
+ {"preceeding /**/ comment",
1141
+ [start_array, end_array, end_json],
1142
+ <<"/* comment */[]">>
1143
+ },
1144
+ {"trailing // comment",
1145
+ [start_array, end_array, end_json],
1146
+ <<"[]// comment", ?newline>>
1147
+ },
1148
+ {"trailing // comment (no newline)",
1149
+ [start_array, end_array, end_json],
1150
+ <<"[]// comment">>
1151
+ },
1152
+ {"trailing /**/ comment",
1153
+ [start_array, end_array, end_json],
1154
+ <<"[] /* comment */">>
1155
+ },
1156
+ {"// comment inside array",
1157
+ [start_array, end_array, end_json],
1158
+ <<"[ // comment", ?newline, "]">>
1159
+ },
1160
+ {"/**/ comment inside array",
1161
+ [start_array, end_array, end_json],
1162
+ <<"[ /* comment */ ]">>
1163
+ },
1164
+ {"// comment at beginning of array",
1165
+ [start_array, {literal, true}, end_array, end_json],
1166
+ <<"[ // comment", ?newline, "true", ?newline, "]">>
1167
+ },
1168
+ {"/**/ comment at beginning of array",
1169
+ [start_array, {literal, true}, end_array, end_json],
1170
+ <<"[ /* comment */ true ]">>
1171
+ },
1172
+ {"// comment at end of array",
1173
+ [start_array, {literal, true}, end_array, end_json],
1174
+ <<"[ true // comment", ?newline, "]">>
1175
+ },
1176
+ {"/**/ comment at end of array",
1177
+ [start_array, {literal, true}, end_array, end_json],
1178
+ <<"[ true /* comment */ ]">>
1179
+ },
1180
+ {"// comment midarray (post comma)",
1181
+ [start_array, {literal, true}, {literal, false}, end_array, end_json],
1182
+ <<"[ true, // comment", ?newline, "false ]">>
1183
+ },
1184
+ {"/**/ comment midarray (post comma)",
1185
+ [start_array, {literal, true}, {literal, false}, end_array, end_json],
1186
+ <<"[ true, /* comment */ false ]">>
1187
+ },
1188
+ {"// comment midarray (pre comma)",
1189
+ [start_array, {literal, true}, {literal, false}, end_array, end_json],
1190
+ <<"[ true// comment", ?newline, ", false ]">>
1191
+ },
1192
+ {"/**/ comment midarray (pre comma)",
1193
+ [start_array, {literal, true}, {literal, false}, end_array, end_json],
1194
+ <<"[ true/* comment */, false ]">>
1195
+ },
1196
+ {"// comment inside object",
1197
+ [start_object, end_object, end_json],
1198
+ <<"{ // comment", ?newline, "}">>
1199
+ },
1200
+ {"/**/ comment inside object",
1201
+ [start_object, end_object, end_json],
1202
+ <<"{ /* comment */ }">>
1203
+ },
1204
+ {"// comment at beginning of object",
1205
+ [start_object, {key, <<"key">>}, {literal, true}, end_object, end_json],
1206
+ <<"{ // comment", ?newline, " \"key\": true", ?newline, "}">>
1207
+ },
1208
+ {"/**/ comment at beginning of object",
1209
+ [start_object, {key, <<"key">>}, {literal, true}, end_object, end_json],
1210
+ <<"{ /* comment */ \"key\": true }">>
1211
+ },
1212
+ {"// comment at end of object",
1213
+ [start_object, {key, <<"key">>}, {literal, true}, end_object, end_json],
1214
+ <<"{ \"key\": true // comment", ?newline, "}">>
1215
+ },
1216
+ {"/**/ comment at end of object",
1217
+ [start_object, {key, <<"key">>}, {literal, true}, end_object, end_json],
1218
+ <<"{ \"key\": true /* comment */ }">>
1219
+ },
1220
+ {"// comment midobject (post comma)",
1221
+ [
1222
+ start_object,
1223
+ {key, <<"x">>},
1224
+ {literal, true},
1225
+ {key, <<"y">>},
1226
+ {literal, false},
1227
+ end_object,
1228
+ end_json
1229
+ ],
1230
+ <<"{ \"x\": true, // comment", ?newline, "\"y\": false }">>
1231
+ },
1232
+ {"/**/ comment midobject (post comma)",
1233
+ [
1234
+ start_object,
1235
+ {key, <<"x">>},
1236
+ {literal, true},
1237
+ {key, <<"y">>},
1238
+ {literal, false},
1239
+ end_object,
1240
+ end_json
1241
+ ],
1242
+ <<"{ \"x\": true, /* comment */", ?newline, "\"y\": false }">>
1243
+ },
1244
+ {"// comment midobject (pre comma)",
1245
+ [
1246
+ start_object,
1247
+ {key, <<"x">>},
1248
+ {literal, true},
1249
+ {key, <<"y">>},
1250
+ {literal, false},
1251
+ end_object,
1252
+ end_json
1253
+ ],
1254
+ <<"{ \"x\": true// comment", ?newline, ", \"y\": false }">>
1255
+ },
1256
+ {"/**/ comment midobject (pre comma)",
1257
+ [
1258
+ start_object,
1259
+ {key, <<"x">>},
1260
+ {literal, true},
1261
+ {key, <<"y">>},
1262
+ {literal, false},
1263
+ end_object,
1264
+ end_json
1265
+ ],
1266
+ <<"{ \"x\": true/* comment */", ?newline, ", \"y\": false }">>
1267
+ },
1268
+ {"// comment precolon",
1269
+ [start_object, {key, <<"key">>}, {literal, true}, end_object, end_json],
1270
+ <<"{ \"key\" // comment", ?newline, ": true }">>
1271
+ },
1272
+ {"/**/ comment precolon",
1273
+ [start_object, {key, <<"key">>}, {literal, true}, end_object, end_json],
1274
+ <<"{ \"key\"/* comment */: true }">>
1275
+ },
1276
+ {"// comment postcolon",
1277
+ [start_object, {key, <<"key">>}, {literal, true}, end_object, end_json],
1278
+ <<"{ \"key\": // comment", ?newline, " true }">>
1279
+ },
1280
+ {"/**/ comment postcolon",
1281
+ [start_object, {key, <<"key">>}, {literal, true}, end_object, end_json],
1282
+ <<"{ \"key\":/* comment */ true }">>
1283
+ },
1284
+ {"// comment terminating zero",
1285
+ [start_array, {integer, 0}, end_array, end_json],
1286
+ <<"[ 0// comment", ?newline, "]">>
1287
+ },
1288
+ {"// comment terminating integer",
1289
+ [start_array, {integer, 1}, end_array, end_json],
1290
+ <<"[ 1// comment", ?newline, "]">>
1291
+ },
1292
+ {"// comment terminating float",
1293
+ [start_array, {float, 1.0}, end_array, end_json],
1294
+ <<"[ 1.0// comment", ?newline, "]">>
1295
+ },
1296
+ {"// comment terminating exp",
1297
+ [start_array, {float, 1.0e1}, end_array, end_json],
1298
+ <<"[ 1e1// comment", ?newline, "]">>
1299
+ },
1300
+ {"/**/ comment terminating zero",
1301
+ [start_array, {integer, 0}, end_array, end_json],
1302
+ <<"[ 0/* comment */ ]">>
1303
+ },
1304
+ {"/**/ comment terminating integer",
1305
+ [start_array, {integer, 1}, end_array, end_json],
1306
+ <<"[ 1/* comment */ ]">>
1307
+ },
1308
+ {"/**/ comment terminating float",
1309
+ [start_array, {float, 1.0}, end_array, end_json],
1310
+ <<"[ 1.0/* comment */ ]">>
1311
+ },
1312
+ {"/**/ comment terminating exp",
1313
+ [start_array, {float, 1.0e1}, end_array, end_json],
1314
+ <<"[ 1e1/* comment */ ]">>
1315
+ },
1316
+ {"/**/ comment following /**/ comment",
1317
+ [start_array, {literal, true}, end_array, end_json],
1318
+ <<"[/* comment *//* comment */true]">>
1319
+ },
1320
+ {"/**/ comment following // comment",
1321
+ [start_array, {literal, true}, end_array, end_json],
1322
+ <<"[// comment", ?newline, "/* comment */true]">>
1323
+ },
1324
+ {"// comment following /**/ comment",
1325
+ [start_array, {literal, true}, end_array, end_json],
1326
+ <<"[/* comment */// comment", ?newline, "true]">>
1327
+ },
1328
+ {"// comment following // comment",
1329
+ [start_array, {literal, true}, end_array, end_json],
1330
+ <<"[// comment", ?newline, "// comment", ?newline, "true]">>
1331
+ },
1332
+ {"/**/ comment inside /**/ comment",
1333
+ [start_array, {literal, true}, end_array, end_json],
1334
+ <<"[ /* /* comment */ */ true ]">>
1335
+ },
1336
+ {"/**/ comment with /",
1337
+ [start_array, {literal, true}, end_array, end_json],
1338
+ <<"[ /* / */ true ]">>
1339
+ },
1340
+ {"/**/ comment with *",
1341
+ [start_array, {literal, true}, end_array, end_json],
1342
+ <<"[ /* * */ true ]">>
1343
+ },
1344
+ {"// comment with badutf",
1345
+ [start_array, {literal, true}, end_array, end_json],
1346
+ <<"[ // comment ", 16#00c0, " ", ?newline, "true]">>
1347
+ },
1348
+ {"/**/ comment with badutf",
1349
+ [start_array, {literal, true}, end_array, end_json],
1350
+ <<"[ /* comment ", 16#00c0, " */ true]">>
1351
+ },
1352
+ {"/**/ comment with badutf preceeded by /",
1353
+ [start_array, {literal, true}, end_array, end_json],
1354
+ <<"[ /* comment /", 16#00c0, " */ true]">>
1355
+ }
1356
+ ],
1357
+ [{Title, ?_assertEqual(Events, decode(JSON))}
1358
+ || {Title, Events, JSON} <- Cases
1359
+ ] ++
1360
+ [{Title ++ " (incremental)", ?_assertEqual(Events, incremental_decode(JSON))}
1361
+ || {Title, Events, JSON} <- Cases
1362
+ ] ++
1363
+ % error when `{strict, [comments]}` is present
1364
+ [{Title, ?_assertError(badarg, decode(JSON, [{strict, [comments]}]))}
1365
+ || {Title, _Events, JSON} <- Cases
1366
+ ] ++
1367
+ [{Title ++ " (incremental)", ?_assertError(
1368
+ badarg,
1369
+ incremental_decode(JSON, [{strict, [comments]}])
1370
+ )} || {Title, _Events, JSON} <- Cases
1371
+ ].
1372
+
1373
+
1374
+ no_comments_test_() ->
1375
+ Cases = [
1376
+ {"// comment with badutf",
1377
+ badarg,
1378
+ <<"[ // comment ", 16#00c0, " ", ?newline, "true]">>,
1379
+ [{strict, [utf8]}]
1380
+ },
1381
+ {"/**/ comment with badutf",
1382
+ badarg,
1383
+ <<"[ /* comment ", 16#00c0, " */ true]">>,
1384
+ [{strict, [utf8]}]
1385
+ },
1386
+ {"/**/ comment with badutf preceeded by /",
1387
+ badarg,
1388
+ <<"[ /* comment /", 16#00c0, " */ true]">>,
1389
+ [{strict, [utf8]}]
1390
+ }
1391
+ ],
1392
+ [{Title, ?_assertError(Error, decode(JSON, Config))}
1393
+ || {Title, Error, JSON, Config} <- Cases
1394
+ ] ++
1395
+ [{Title ++ " (incremental)", ?_assertError(Error, incremental_decode(JSON, Config))}
1396
+ || {Title, Error, JSON, Config} <- Cases
1397
+ ].
1398
+
1399
+
1400
+ % doing the full unicode range takes foreverrrrrrr so just do boundaries
1401
+ % excludes characters that may need escaping
1402
+ codepoints() ->
1403
+ lists:seq(0, 32) ++
1404
+ [32, 33] ++
1405
+ lists:seq(35, 46) ++
1406
+ lists:seq(48, 91) ++
1407
+ lists:seq(93, 127) ++
1408
+ [16#2027, 16#202a, 16#d7ff, 16#e000] ++
1409
+ lists:seq(16#fdd0, 16#ffff) ++
1410
+ [16#10000, 16#20000, 16#30000, 16#40000, 16#50000] ++
1411
+ [16#60000, 16#70000, 16#80000, 16#90000, 16#a0000, 16#b0000] ++
1412
+ [16#c0000, 16#d0000, 16#e0000, 16#f0000, 16#100000].
1413
+
1414
+
1415
+ surrogates() -> lists:seq(16#d800, 16#dfff).
1416
+
1417
+
1418
+ %% erlang refuses to decode certain codepoints, so fake them all
1419
+ to_fake_utf8(N) when N < 16#0080 -> <<34/utf8, N:8, 34/utf8>>;
1420
+ to_fake_utf8(N) when N < 16#0800 ->
1421
+ <<0:5, Y:5, X:6>> = <<N:16>>,
1422
+ <<34/utf8, 2#110:3, Y:5, 2#10:2, X:6, 34/utf8>>;
1423
+ to_fake_utf8(N) when N < 16#10000 ->
1424
+ <<Z:4, Y:6, X:6>> = <<N:16>>,
1425
+ <<34/utf8, 2#1110:4, Z:4, 2#10:2, Y:6, 2#10:2, X:6, 34/utf8>>;
1426
+ to_fake_utf8(N) ->
1427
+ <<0:3, W:3, Z:6, Y:6, X:6>> = <<N:24>>,
1428
+ <<34/utf8, 2#11110:5, W:3, 2#10:2, Z:6, 2#10:2, Y:6, 2#10:2, X:6, 34/utf8>>.
1429
+
1430
+
1431
+ clean_string_test_() ->
1432
+ Clean = codepoints(),
1433
+ Dirty = surrogates(),
1434
+ % clean codepoints
1435
+ [{"clean u+" ++ integer_to_list(Codepoint, 16), ?_assertEqual(
1436
+ [{string, <<Codepoint/utf8>>}, end_json],
1437
+ decode(<<34/utf8, Codepoint/utf8, 34/utf8>>)
1438
+ )} || Codepoint <- Clean
1439
+ ] ++
1440
+ % bad codepoints replaced by u+FFFD
1441
+ [{"clean u+" ++ integer_to_list(Codepoint, 16), ?_assertEqual(
1442
+ [{string, <<16#fffd/utf8>>}, end_json],
1443
+ decode(to_fake_utf8(Codepoint))
1444
+ )} || Codepoint <- Dirty
1445
+ ] ++
1446
+ % bad codepoints that cause errors
1447
+ [{"dirty u+" ++ integer_to_list(Codepoint, 16), ?_assertError(
1448
+ badarg,
1449
+ decode(to_fake_utf8(Codepoint), [{strict, [utf8]}])
1450
+ )} || Codepoint <- Dirty
1451
+ ].
1452
+
1453
+
1454
+ dirty_string_test_() ->
1455
+ Cases = [
1456
+ {"dirty \\n",
1457
+ [start_array, {string, <<"\\n">>}, end_array, end_json],
1458
+ <<"[\"\\n\"]">>,
1459
+ [dirty_strings]
1460
+ },
1461
+ {"dirty \\uwxyz",
1462
+ [start_array, {string, <<"\\uwxyz">>}, end_array, end_json],
1463
+ <<"[\"\\uwxyz\"]">>,
1464
+ [dirty_strings]
1465
+ },
1466
+ {"dirty \\x23",
1467
+ [start_array, {string, <<"\\x23">>}, end_array, end_json],
1468
+ <<"[\"\\x23\"]">>,
1469
+ [dirty_strings]
1470
+ },
1471
+ {"dirty 0",
1472
+ [start_array, {string, <<0>>}, end_array, end_json],
1473
+ <<"[\"", 0, "\"]">>,
1474
+ [dirty_strings]
1475
+ },
1476
+ {"dirty 0\\\"0",
1477
+ [start_array, {string, <<0, ?rsolidus, ?doublequote, 0>>}, end_array, end_json],
1478
+ <<"[\"", 0, ?rsolidus, ?doublequote, 0, "\"]">>,
1479
+ [dirty_strings]
1480
+ },
1481
+ {"dirty 0\\\\\"0",
1482
+ [start_array, {string, <<0, ?rsolidus, ?rsolidus, ?doublequote, 0>>}, end_array, end_json],
1483
+ <<"[\"", 0, ?rsolidus, ?rsolidus, ?doublequote, 0, "\"]">>,
1484
+ [dirty_strings]
1485
+ },
1486
+ {"dirty 16#d800",
1487
+ [start_array, {string, <<237, 160, 128>>}, end_array, end_json],
1488
+ <<"[\"", 237, 160, 128, "\"]">>,
1489
+ [dirty_strings]
1490
+ },
1491
+ {"dirty /",
1492
+ [start_array, {string, <<$/>>}, end_array, end_json],
1493
+ <<"[\"", $/, "\"]">>,
1494
+ [dirty_strings, escaped_forward_slashes]
1495
+ },
1496
+ {"dirty <<194, 129>>",
1497
+ [start_array, {string, <<194, 129>>}, end_array, end_json],
1498
+ <<"[\"", 194, 129, "\"]">>,
1499
+ [dirty_strings]
1500
+ }
1501
+ ],
1502
+ [{Title, ?_assertEqual(Events, decode(JSON, Config))}
1503
+ || {Title, Events, JSON, Config} <- Cases
1504
+ ] ++
1505
+ % ensure `dirty_strings` and `strict` interact properly
1506
+ [{Title, ?_assertEqual(Events, decode(JSON, Config ++ [strict]))}
1507
+ || {Title, Events, JSON, Config} <- Cases
1508
+ ] ++
1509
+ [{Title ++ " (incremental)", ?_assertEqual(Events, incremental_decode(JSON, Config))}
1510
+ || {Title, Events, JSON, Config} <- Cases
1511
+ ].
1512
+
1513
+
1514
+ bad_utf8_test_() ->
1515
+ Cases = [
1516
+ {"orphan continuation byte u+0080", <<16#fffd/utf8>>, <<16#0080>>},
1517
+ {"orphan continuation byte u+00bf", <<16#fffd/utf8>>, <<16#00bf>>},
1518
+ {"2 continuation bytes",
1519
+ binary:copy(<<16#fffd/utf8>>, 2),
1520
+ <<(binary:copy(<<16#0080>>, 2))/binary>>
1521
+ },
1522
+ {"3 continuation bytes",
1523
+ binary:copy(<<16#fffd/utf8>>, 3),
1524
+ <<(binary:copy(<<16#0080>>, 3))/binary>>
1525
+ },
1526
+ {"4 continuation bytes",
1527
+ binary:copy(<<16#fffd/utf8>>, 4),
1528
+ <<(binary:copy(<<16#0080>>, 4))/binary>>
1529
+ },
1530
+ {"5 continuation bytes",
1531
+ binary:copy(<<16#fffd/utf8>>, 5),
1532
+ <<(binary:copy(<<16#0080>>, 5))/binary>>
1533
+ },
1534
+ {"6 continuation bytes",
1535
+ binary:copy(<<16#fffd/utf8>>, 6),
1536
+ <<(binary:copy(<<16#0080>>, 6))/binary>>
1537
+ },
1538
+ {"all continuation bytes",
1539
+ binary:copy(<<16#fffd/utf8>>, length(lists:seq(16#0080, 16#00bf))),
1540
+ <<(list_to_binary(lists:seq(16#0080, 16#00bf)))/binary>>
1541
+ },
1542
+ {"lonely start byte", <<16#fffd/utf8>>, <<16#00c0>>},
1543
+ {"lonely start bytes (2 byte)",
1544
+ <<16#fffd/utf8, 32, 16#fffd/utf8>>,
1545
+ <<16#00c0, 32, 16#00df>>
1546
+ },
1547
+ {"lonely start bytes (3 byte)",
1548
+ <<16#fffd/utf8, 32, 16#fffd/utf8>>,
1549
+ <<16#00e0, 32, 16#00ef>>
1550
+ },
1551
+ {"lonely start bytes (4 byte)",
1552
+ <<16#fffd/utf8, 32, 16#fffd/utf8>>,
1553
+ <<16#00f0, 32, 16#00f7>>
1554
+ },
1555
+ {"missing continuation byte (3 byte)", <<16#fffd/utf8, 32>>, <<224, 160, 32>>},
1556
+ {"missing continuation byte (4 byte missing one)",
1557
+ <<16#fffd/utf8, 32>>,
1558
+ <<240, 144, 128, 32>>
1559
+ },
1560
+ {"missing continuation byte (4 byte missing two)",
1561
+ <<16#fffd/utf8, 32>>,
1562
+ <<240, 144, 32>>
1563
+ },
1564
+ {"overlong encoding of u+002f (2 byte)",
1565
+ <<16#fffd/utf8, 32>>,
1566
+ <<16#c0, 16#af, 32>>
1567
+ },
1568
+ {"overlong encoding of u+002f (3 byte)",
1569
+ <<16#fffd/utf8, 32>>,
1570
+ <<16#e0, 16#80, 16#af, 32>>
1571
+ },
1572
+ {"overlong encoding of u+002f (4 byte)",
1573
+ <<16#fffd/utf8, 32>>,
1574
+ <<16#f0, 16#80, 16#80, 16#af, 32>>
1575
+ },
1576
+ {"highest overlong 2 byte sequence",
1577
+ <<16#fffd/utf8, 32>>,
1578
+ <<16#c1, 16#bf, 32>>
1579
+ },
1580
+ {"highest overlong 3 byte sequence",
1581
+ <<16#fffd/utf8, 32>>,
1582
+ <<16#e0, 16#9f, 16#bf, 32>>
1583
+ },
1584
+ {"highest overlong 4 byte sequence",
1585
+ <<16#fffd/utf8, 32>>,
1586
+ <<16#f0, 16#8f, 16#bf, 16#bf, 32>>
1587
+ }
1588
+ ],
1589
+ [{Title, ?_assertError(
1590
+ badarg,
1591
+ decode(<<34, JSON/binary, 34>>, [{strict, [utf8]}])
1592
+ )} || {Title, _, JSON} <- Cases
1593
+ ] ++
1594
+ [{Title ++ " (incremental)", ?_assertError(
1595
+ badarg,
1596
+ incremental_decode(<<34, JSON/binary, 34>>, [{strict, [utf8]}])
1597
+ )} || {Title, _, JSON} <- Cases
1598
+ ] ++
1599
+ [{Title ++ " replaced", ?_assertEqual(
1600
+ [{string, Replacement}, end_json],
1601
+ decode(<<34, JSON/binary, 34>>)
1602
+ )} || {Title, Replacement, JSON} <- Cases
1603
+ ] ++
1604
+ [{Title ++ " replaced (incremental)", ?_assertEqual(
1605
+ [{string, Replacement}, end_json],
1606
+ incremental_decode(<<34, JSON/binary, 34>>)
1607
+ )} || {Title, Replacement, JSON} <- Cases
1608
+ ].
1609
+
1610
+
1611
+ unescape_test_() ->
1612
+ Cases = [
1613
+ {"unescape backspace", <<"\b">>, <<"\\b"/utf8>>},
1614
+ {"unescape tab", <<"\t">>, <<"\\t"/utf8>>},
1615
+ {"unescape newline", <<"\n">>, <<"\\n"/utf8>>},
1616
+ {"unescape formfeed", <<"\f">>, <<"\\f"/utf8>>},
1617
+ {"unescape carriage return", <<"\r">>, <<"\\r"/utf8>>},
1618
+ {"unescape quote", <<"\"">>, <<"\\\""/utf8>>},
1619
+ {"unescape solidus", <<"/">>, <<"\\/"/utf8>>},
1620
+ {"unescape reverse solidus", <<"\\">>, <<"\\\\"/utf8>>},
1621
+ {"unescape control", <<0>>, <<"\\u0000"/utf8>>},
1622
+ {"unescape surrogate pair", <<16#10000/utf8>>, <<"\\ud800\\udc00"/utf8>>},
1623
+ {"unescape surrogate pair", <<16#10000/utf8>>, <<"\\uD800\\uDC00"/utf8>>},
1624
+ {"replace bad high surrogate", <<16#fffd/utf8>>, <<"\\udc00"/utf8>>},
1625
+ {"replace bad high surrogate", <<16#fffd/utf8>>, <<"\\uDC00"/utf8>>},
1626
+ {"replace naked high surrogate",
1627
+ <<16#fffd/utf8, "hello world">>,
1628
+ <<"\\ud800hello world"/utf8>>
1629
+ },
1630
+ {"replace naked high surrogate",
1631
+ <<16#fffd/utf8, "hello world">>,
1632
+ <<"\\uD800hello world"/utf8>>
1633
+ },
1634
+ {"replace naked low surrogate",
1635
+ <<16#fffd/utf8, "hello world">>,
1636
+ <<"\\udc00hello world"/utf8>>
1637
+ },
1638
+ {"replace naked low surrogate",
1639
+ <<16#fffd/utf8, "hello world">>,
1640
+ <<"\\uDC00hello world"/utf8>>
1641
+ },
1642
+ {"replace bad surrogate pair", <<16#fffd/utf8, 16#fffd/utf8>>, <<"\\ud800\\u0000">>},
1643
+ {"replace bad surrogate pair", <<16#fffd/utf8, 16#fffd/utf8>>, <<"\\uD800\\u0000">>}
1644
+ ],
1645
+ [{Title, ?_assertEqual([{string, Escaped}, end_json], decode(<<34, JSON/binary, 34>>))}
1646
+ || {Title, Escaped, JSON} <- Cases
1647
+ ] ++
1648
+ [{Title ++ " (incremental)", ?_assertEqual(
1649
+ [{string, Escaped}, end_json],
1650
+ incremental_decode(<<34, JSON/binary, 34>>)
1651
+ )} || {Title, Escaped, JSON} <- Cases
1652
+ ].
1653
+
1654
+
1655
+ bad_escaped_surrogate_test_() ->
1656
+ Cases = [
1657
+ {"do not unescape bad high surrogate", <<"\\udc00">>},
1658
+ {"do not unescape naked high surrogate", <<"\\ud800hello world">>},
1659
+ {"do not unescape naked low surrogate", <<"\\udc00hello world">>},
1660
+ {"do not unescape bad surrogate pair", <<"\\ud800\\u0000">>}
1661
+ ],
1662
+ [{Title, ?_assertError(badarg, decode(<<34, JSON/binary, 34>>, [{strict, [utf8]}]))}
1663
+ || {Title, JSON} <- Cases
1664
+ ].
1665
+
1666
+
1667
+ escape_test_() ->
1668
+ Cases = [
1669
+ {"backspace", <<"\b">>, <<"\\b">>},
1670
+ {"tab", <<"\t">>, <<"\\t">>},
1671
+ {"newline", <<"\n">>, <<"\\n">>},
1672
+ {"formfeed", <<"\f">>, <<"\\f">>},
1673
+ {"carriage return", <<"\r">>, <<"\\r">>},
1674
+ {"quote", <<"\"">>, <<"\\\"">>},
1675
+ {"backslash", <<"\\">>, <<"\\\\">>},
1676
+ {"control", <<0>>, <<"\\u0000">>}
1677
+ ],
1678
+ [{"escape " ++ Title, ?_assertEqual(
1679
+ [{string, Escaped}, end_json],
1680
+ decode(<<34, Escaped/binary, 34>>, [escaped_strings])
1681
+ )} || {Title, _Unescaped, Escaped} <- Cases
1682
+ ] ++
1683
+ [{"do not escape " ++ Title, ?_assertEqual(
1684
+ [{string, Unescaped}, end_json],
1685
+ decode(<<34, Escaped/binary, 34>>)
1686
+ )} || {Title, Unescaped, Escaped} <- Cases
1687
+ ].
1688
+
1689
+
1690
+ special_escape_test_() ->
1691
+ Cases = [
1692
+ {"escape forward slash", <<"\\/">>, <<"/"/utf8>>, [escaped_forward_slashes]},
1693
+ {"do not escape forward slash", <<"/">>, <<"/"/utf8>>, []},
1694
+ {"escape jsonp", <<"\\u2028">>, <<16#2028/utf8>>, []},
1695
+ {"do not escape jsonp", <<16#2028/utf8>>, <<16#2028/utf8>>, [unescaped_jsonp]}
1696
+ ],
1697
+ [{Title, ?_assertEqual(
1698
+ [{string, Expect}, end_json],
1699
+ decode(<<34, Raw/binary, 34>>, [escaped_strings] ++ Config)
1700
+ )} || {Title, Expect, Raw, Config} <- Cases
1701
+ ].
1702
+
1703
+
1704
+ uescape_test_() ->
1705
+ [
1706
+ {"\"\\u0080\"", ?_assertEqual(
1707
+ [{string, <<"\\u0080">>}, end_json],
1708
+ decode(<<34, 128/utf8, 34>>, [uescape])
1709
+ )},
1710
+ {"\"\\u8ca8\\u5481\\u3002\\u0091\\u0091\"", ?_assertEqual(
1711
+ [{string, <<"\\u8ca8\\u5481\\u3002\\u0091\\u0091">>}, end_json],
1712
+ decode(
1713
+ <<34,232,178,168,229,146,129,227,128,130,194,145,194,145,34>>,
1714
+ [uescape]
1715
+ )
1716
+ )},
1717
+ {"\"\\ud834\\udd1e\"", ?_assertEqual(
1718
+ [{string, <<"\\ud834\\udd1e">>}, end_json],
1719
+ decode(<<34, 240, 157, 132, 158, 34>>, [uescape])
1720
+ )},
1721
+ {"\"\\ud83d\\ude0a\"", ?_assertEqual(
1722
+ [{string, <<"\\ud83d\\ude0a">>}, end_json],
1723
+ decode(<<34, 240, 159, 152, 138, 34>>, [uescape])
1724
+ )}
1725
+ ].
1726
+
1727
+
1728
+ single_quoted_string_test_() ->
1729
+ Cases = [
1730
+ {"single quoted string", [{string, <<"hello world">>}, end_json], <<39, "hello world", 39>>},
1731
+ {"single quoted string with embedded double quotes",
1732
+ [{string, <<"quoth the raven, \"nevermore\"">>}, end_json],
1733
+ <<39, "quoth the raven, \"nevermore\"", 39>>
1734
+ },
1735
+ {"escaped single quote",
1736
+ [{string, <<"quoth the raven, 'nevermore'">>}, end_json],
1737
+ <<39, "quoth the raven, \\'nevermore\\'", 39>>
1738
+ },
1739
+ {"single quoted key",
1740
+ [start_object,
1741
+ {key, <<"key">>}, {string, <<"value">>},
1742
+ {key, <<"another key">>}, {string, <<"another value">>},
1743
+ end_object, end_json],
1744
+ <<"{'key':'value','another key':'another value'}">>
1745
+ }
1746
+ ],
1747
+ [{Title, ?_assertEqual(Expect, decode(Raw, []))} || {Title, Expect, Raw} <- Cases] ++
1748
+ [{Title, ?_assertError(
1749
+ badarg,
1750
+ decode(Raw, [{strict, [single_quotes]}])
1751
+ )} || {Title, _Expect, Raw} <- Cases
1752
+ ].
1753
+
1754
+
1755
+ embedded_single_quoted_string_test_() ->
1756
+ [
1757
+ {"string with embedded single quotes", ?_assertEqual(
1758
+ [{string, <<"quoth the raven, 'nevermore'">>}, end_json],
1759
+ decode(<<34, "quoth the raven, 'nevermore'", 34>>, [])
1760
+ )},
1761
+ {"string with embedded single quotes", ?_assertEqual(
1762
+ [{string, <<"quoth the raven, 'nevermore'">>}, end_json],
1763
+ decode(<<34, "quoth the raven, 'nevermore'", 34>>, [{strict, [single_quotes]}])
1764
+ )}
1765
+ ].
1766
+
1767
+
1768
+ ignored_bad_escapes_test_() ->
1769
+ [
1770
+ {"ignore unrecognized escape sequence", ?_assertEqual(
1771
+ [{string, <<"\\x25">>}, end_json],
1772
+ decode(<<"\"\\x25\"">>, [])
1773
+ )}
1774
+ ].
1775
+
1776
+
1777
+ bom_test_() ->
1778
+ [
1779
+ {"bom", ?_assertEqual(
1780
+ [start_array, end_array, end_json],
1781
+ decode(<<16#ef, 16#bb, 16#bf, "[]"/utf8>>, [])
1782
+ )}
1783
+ ].
1784
+
1785
+
1786
+ trailing_comma_test_() ->
1787
+ [
1788
+ {"trailing comma in object", ?_assertEqual(
1789
+ [start_object, {key, <<"key">>}, {literal, true}, end_object, end_json],
1790
+ decode(<<"{\"key\": true,}">>, [])
1791
+ )},
1792
+ {"strict trailing comma in object", ?_assertError(
1793
+ badarg,
1794
+ decode(<<"{\"key\": true,}">>, [{strict, [trailing_commas]}])
1795
+ )},
1796
+ {"two trailing commas in object", ?_assertError(
1797
+ badarg,
1798
+ decode(<<"{\"key\": true,,}">>, [])
1799
+ )},
1800
+ {"comma in empty object", ?_assertError(
1801
+ badarg,
1802
+ decode(<<"{,}">>, [])
1803
+ )},
1804
+ {"trailing comma in list", ?_assertEqual(
1805
+ [start_array, {literal, true}, end_array, end_json],
1806
+ decode(<<"[true,]">>, [])
1807
+ )},
1808
+ {"strict trailing comma in list", ?_assertError(
1809
+ badarg,
1810
+ decode(<<"[true,]">>, [{strict, [trailing_commas]}])
1811
+ )},
1812
+ {"two trailing commas in list", ?_assertError(
1813
+ badarg,
1814
+ decode(<<"[true,,]">>, [])
1815
+ )},
1816
+ {"comma in empty list", ?_assertError(
1817
+ badarg,
1818
+ decode(<<"[,]">>, [])
1819
+ )}
1820
+ ].
1821
+
1822
+
1823
+ incomplete_test_() ->
1824
+ [
1825
+ {"stream false", ?_assertError(
1826
+ badarg,
1827
+ decode(<<"{">>)
1828
+ )},
1829
+ {"stream true", ?_assertMatch(
1830
+ {incomplete, _},
1831
+ decode(<<"{">>, [stream])
1832
+ )},
1833
+ {"complete input", ?_assertMatch(
1834
+ {incomplete, _},
1835
+ decode(<<"{}">>, [stream])
1836
+ )}
1837
+ ].
1838
+
1839
+
1840
+ error_test_() ->
1841
+ Cases = [
1842
+ {"maybe_bom error", <<16#ef, 0>>},
1843
+ {"definitely_bom error", <<16#ef, 16#bb, 0>>},
1844
+ {"object error", <<"{"/utf8, 0>>},
1845
+ {"colon error", <<"{\"\""/utf8, 0>>},
1846
+ {"key error", <<"{\"\":1,"/utf8, 0>>},
1847
+ {"value error", <<0>>},
1848
+ {"negative error", <<"-"/utf8, 0>>},
1849
+ {"zero error", <<"0"/utf8, 0>>},
1850
+ {"integer error", <<"1"/utf8, 0>>},
1851
+ {"decimal error", <<"1.0"/utf8, 0>>},
1852
+ {"e error", <<"1e"/utf8, 0>>},
1853
+ {"ex error", <<"1e+"/utf8, 0>>},
1854
+ {"exp error", <<"1e1"/utf8, 0>>},
1855
+ {"exp error", <<"1.0e1"/utf8, 0>>},
1856
+ {"exp error", <<"1.e"/utf8>>},
1857
+ {"true error", <<"tru"/utf8, 0>>},
1858
+ {"false error", <<"fals"/utf8, 0>>},
1859
+ {"null error", <<"nul"/utf8, 0>>},
1860
+ {"maybe_done error", <<"[[]"/utf8, 0>>},
1861
+ {"done error", <<"[]"/utf8, 0>>}
1862
+ ],
1863
+ [{Title, ?_assertError(badarg, decode(State))} || {Title, State} <- Cases].
1864
+
1865
+
1866
+ custom_incomplete_handler_test_() ->
1867
+ [
1868
+ {"custom incomplete handler", ?_assertError(
1869
+ incomplete,
1870
+ decode(<<>>, [{incomplete_handler, fun(_, _, _) -> erlang:error(incomplete) end}, stream])
1871
+ )}
1872
+ ].
1873
+
1874
+
1875
+ return_tail_test_() ->
1876
+ [
1877
+ {"return_tail with tail", ?_assertEqual(
1878
+ {with_tail,#{},<<"3">>},
1879
+ jsx:decode(<<"{} 3">>, [return_tail])
1880
+ )},
1881
+ {"return_tail without tail", ?_assertEqual(
1882
+ {with_tail,#{},<<"">>},
1883
+ jsx:decode(<<"{}">>, [return_tail])
1884
+ )},
1885
+ {"return_tail with trimmed whitespace", ?_assertEqual(
1886
+ {with_tail,#{},<<"">>},
1887
+ jsx:decode(<<"{} ">>, [return_tail])
1888
+ )},
1889
+ {"return_tail and streaming", ?_assertEqual(
1890
+ {with_tail,#{},<<"3">>},
1891
+ begin
1892
+ {incomplete, F} = jsx:decode(<<"{">>, [return_tail, stream]),
1893
+ F(<<"} 3">>)
1894
+ end
1895
+ )},
1896
+ {"return_tail and streaming", ?_assertEqual(
1897
+ {with_tail,#{},<<"">>},
1898
+ begin
1899
+ %% In case of infinite stream of objects a user does not know
1900
+ %% when to call F(end_stream).
1901
+ %% So, return_tail overwrites conservative stream end.
1902
+ %% This means that we don't need to call end_stream explicitly.
1903
+ {incomplete, F} = jsx:decode(<<"{">>, [return_tail, stream]),
1904
+ F(<<"}">>)
1905
+ end
1906
+ )}
1907
+ ].
1908
+
1909
+ -endif.