move opts handling to jsx.erl for earlier failure on badopts
This commit is contained in:
parent
3fdf2b28c8
commit
4a078bd44a
5 changed files with 63 additions and 78 deletions
47
src/jsx.erl
47
src/jsx.erl
|
@ -56,13 +56,18 @@ decoder() -> decoder([]).
|
||||||
|
|
||||||
|
|
||||||
decoder(OptsList) ->
|
decoder(OptsList) ->
|
||||||
case proplists:get_value(encoding, OptsList, auto) of
|
case parse_opts(OptsList) of
|
||||||
utf8 -> jsx_utf8:decoder(OptsList)
|
{error, badarg} -> {error, badarg}
|
||||||
; utf16 -> jsx_utf16:decoder(OptsList)
|
; Opts ->
|
||||||
; utf32 -> jsx_utf32:decoder(OptsList)
|
case Opts#opts.encoding of
|
||||||
; {utf16, little} -> jsx_utf16le:decoder(OptsList)
|
utf8 -> jsx_utf8:decoder(Opts)
|
||||||
; {utf32, little} -> jsx_utf32le:decoder(OptsList)
|
; utf16 -> jsx_utf16:decoder(Opts)
|
||||||
; auto -> jsx_utils:detect_encoding(OptsList)
|
; utf32 -> jsx_utf32:decoder(Opts)
|
||||||
|
; {utf16, little} -> jsx_utf16le:decoder(Opts)
|
||||||
|
; {utf32, little} -> jsx_utf32le:decoder(Opts)
|
||||||
|
; auto -> jsx_utils:detect_encoding(Opts)
|
||||||
|
; _ -> {error, badarg}
|
||||||
|
end
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
||||||
|
@ -71,9 +76,13 @@ decoder(OptsList) ->
|
||||||
encoder() -> encoder([]).
|
encoder() -> encoder([]).
|
||||||
|
|
||||||
|
|
||||||
-spec encoder(Opts::jsx_opts()) -> jsx_encoder().
|
-spec encoder(OptsList::jsx_opts()) -> jsx_encoder().
|
||||||
|
|
||||||
encoder(Opts) -> jsx_encoder:encoder(Opts).
|
encoder(OptsList) ->
|
||||||
|
case parse_opts(OptsList) of
|
||||||
|
{error, badarg} -> {error, badarg}
|
||||||
|
; Opts -> jsx_encoder:encoder(Opts)
|
||||||
|
end.
|
||||||
|
|
||||||
|
|
||||||
-spec json_to_term(JSON::binary()) -> jsx_term().
|
-spec json_to_term(JSON::binary()) -> jsx_term().
|
||||||
|
@ -139,6 +148,26 @@ format(JSON, Opts) ->
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
parse_opts(Opts) ->
|
||||||
|
parse_opts(Opts, #opts{}).
|
||||||
|
|
||||||
|
parse_opts([], Opts) ->
|
||||||
|
Opts;
|
||||||
|
parse_opts([loose_unicode|Rest], Opts) ->
|
||||||
|
parse_opts(Rest, Opts#opts{loose_unicode=true});
|
||||||
|
parse_opts([iterate|Rest], Opts) ->
|
||||||
|
parse_opts(Rest, Opts#opts{iterate=true});
|
||||||
|
parse_opts([escape_forward_slash|Rest], Opts) ->
|
||||||
|
parse_opts(Rest, Opts#opts{escape_forward_slash=true});
|
||||||
|
parse_opts([{encoding, Encoding}|Rest], Opts)
|
||||||
|
when Encoding =:= utf8; Encoding =:= utf16; Encoding =:= utf32;
|
||||||
|
Encoding =:= {utf16,little}; Encoding =:= {utf32,little};
|
||||||
|
Encoding =:= auto ->
|
||||||
|
parse_opts(Rest, Opts#opts{encoding=Encoding});
|
||||||
|
parse_opts(_, _) ->
|
||||||
|
{error, badarg}.
|
||||||
|
|
||||||
|
|
||||||
-ifdef(TEST).
|
-ifdef(TEST).
|
||||||
-include_lib("eunit/include/eunit.hrl").
|
-include_lib("eunit/include/eunit.hrl").
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,16 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
%% opts record for decoder/encoder
|
||||||
|
-record(opts, {
|
||||||
|
loose_unicode = false,
|
||||||
|
encoding = auto,
|
||||||
|
escape_forward_slash = false, %% does nothing, used by encoder
|
||||||
|
iterate = false
|
||||||
|
}).
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-define(is_utf_encoding(X),
|
-define(is_utf_encoding(X),
|
||||||
X == utf8
|
X == utf8
|
||||||
; X == utf16
|
; X == utf16
|
||||||
|
|
|
@ -28,6 +28,7 @@
|
||||||
|
|
||||||
-export([decoder/1]).
|
-export([decoder/1]).
|
||||||
|
|
||||||
|
|
||||||
%% exported solely to facilitate stupid trick i shouldn't be using
|
%% exported solely to facilitate stupid trick i shouldn't be using
|
||||||
-export([start/4,
|
-export([start/4,
|
||||||
maybe_done/4,
|
maybe_done/4,
|
||||||
|
@ -65,19 +66,13 @@
|
||||||
]).
|
]).
|
||||||
|
|
||||||
|
|
||||||
-include("jsx_opts.hrl").
|
|
||||||
|
|
||||||
|
-spec decoder(Opts::#opts{}) -> jsx_decoder().
|
||||||
|
|
||||||
-spec decoder(OptsList::jsx_opts()) -> jsx_decoder().
|
decoder(Opts) ->
|
||||||
|
case Opts#opts.iterate of
|
||||||
decoder(OptsList) ->
|
true -> fun(JSON) -> start(JSON, iterate, [], Opts) end
|
||||||
case parse_opts(OptsList) of
|
; false -> fun(JSON) -> start(JSON, [], [], Opts) end
|
||||||
{error, badopt} -> {error, badopt}
|
|
||||||
; Opts ->
|
|
||||||
case Opts#opts.iterate of
|
|
||||||
true -> fun(JSON) -> start(JSON, iterate, [], Opts) end
|
|
||||||
; false -> fun(JSON) -> start(JSON, [], [], Opts) end
|
|
||||||
end
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
||||||
|
@ -956,7 +951,7 @@ check([H|T], Opts, Acc) ->
|
||||||
|
|
||||||
|
|
||||||
decode(JSON, Opts) ->
|
decode(JSON, Opts) ->
|
||||||
F = decoder([iterate] ++ Opts),
|
F = jsx:decoder([iterate] ++ Opts),
|
||||||
loop(F(JSON), []).
|
loop(F(JSON), []).
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -23,6 +23,10 @@
|
||||||
|
|
||||||
-module(jsx_encoder).
|
-module(jsx_encoder).
|
||||||
|
|
||||||
|
|
||||||
|
-include("jsx_common.hrl").
|
||||||
|
|
||||||
|
|
||||||
-export([start/3,
|
-export([start/3,
|
||||||
list_or_object/4,
|
list_or_object/4,
|
||||||
key/4,
|
key/4,
|
||||||
|
@ -34,14 +38,9 @@
|
||||||
-export([encoder/1]).
|
-export([encoder/1]).
|
||||||
|
|
||||||
|
|
||||||
-include("jsx_common.hrl").
|
-spec encoder(Opts::#opts{}) -> jsx_encoder().
|
||||||
-include("jsx_opts.hrl").
|
|
||||||
|
|
||||||
|
encoder(Opts) ->
|
||||||
-spec encoder(OptsList::jsx_opts()) -> jsx_encoder().
|
|
||||||
|
|
||||||
encoder(OptsList) ->
|
|
||||||
Opts = parse_opts(OptsList),
|
|
||||||
case Opts#opts.iterate of
|
case Opts#opts.iterate of
|
||||||
true ->
|
true ->
|
||||||
fun(Forms) -> start(Forms, iterate, Opts) end
|
fun(Forms) -> start(Forms, iterate, Opts) end
|
||||||
|
@ -217,7 +216,7 @@ encode(Terms) ->
|
||||||
|
|
||||||
|
|
||||||
encode_simple(Terms) ->
|
encode_simple(Terms) ->
|
||||||
case (encoder([]))(Terms) of
|
case (jsx:encoder([]))(Terms) of
|
||||||
{jsx, Terms, _} ->
|
{jsx, Terms, _} ->
|
||||||
true
|
true
|
||||||
%% matches [foo, end_json], aka naked terms
|
%% matches [foo, end_json], aka naked terms
|
||||||
|
@ -229,7 +228,7 @@ encode_simple(Terms) ->
|
||||||
|
|
||||||
|
|
||||||
encode_iterative(Terms) ->
|
encode_iterative(Terms) ->
|
||||||
case loop((encoder([iterate]))(Terms), []) of
|
case loop((jsx:encoder([iterate]))(Terms), []) of
|
||||||
{ok, Terms} ->
|
{ok, Terms} ->
|
||||||
true
|
true
|
||||||
%% matches naked terms
|
%% matches naked terms
|
||||||
|
|
|
@ -1,48 +0,0 @@
|
||||||
%% The MIT License
|
|
||||||
|
|
||||||
%% Copyright (c) 2010 Alisdair Sullivan <alisdairsullivan@yahoo.ca>
|
|
||||||
|
|
||||||
%% Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
%% of this software and associated documentation files (the "Software"), to deal
|
|
||||||
%% in the Software without restriction, including without limitation the rights
|
|
||||||
%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
%% copies of the Software, and to permit persons to whom the Software is
|
|
||||||
%% furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
%% The above copyright notice and this permission notice shall be included in
|
|
||||||
%% all copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
%% THE SOFTWARE.
|
|
||||||
|
|
||||||
|
|
||||||
%% opts record for decoder/encoder
|
|
||||||
-record(opts, {
|
|
||||||
loose_unicode = false,
|
|
||||||
encoding = auto,
|
|
||||||
escape_forward_slash = false, %% does nothing, used by encoder
|
|
||||||
iterate = false
|
|
||||||
}).
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
parse_opts(Opts) ->
|
|
||||||
parse_opts(Opts, #opts{}).
|
|
||||||
|
|
||||||
parse_opts([], Opts) ->
|
|
||||||
Opts;
|
|
||||||
parse_opts([loose_unicode|Rest], Opts) ->
|
|
||||||
parse_opts(Rest, Opts#opts{loose_unicode=true});
|
|
||||||
parse_opts([iterate|Rest], Opts) ->
|
|
||||||
parse_opts(Rest, Opts#opts{iterate=true});
|
|
||||||
parse_opts([escape_forward_slash|Rest], Opts) ->
|
|
||||||
parse_opts(Rest, Opts#opts{escape_forward_slash=true});
|
|
||||||
parse_opts([{encoding, _}|Rest], Opts) ->
|
|
||||||
parse_opts(Rest, Opts);
|
|
||||||
parse_opts(_, _) ->
|
|
||||||
{error, badarg}.
|
|
Loading…
Add table
Add a link
Reference in a new issue