move opts handling to jsx.erl for earlier failure on badopts

This commit is contained in:
alisdair sullivan 2011-08-10 01:28:25 -07:00
parent 3fdf2b28c8
commit 4a078bd44a
5 changed files with 63 additions and 78 deletions

View file

@ -56,13 +56,18 @@ decoder() -> decoder([]).
decoder(OptsList) ->
case proplists:get_value(encoding, OptsList, auto) of
utf8 -> jsx_utf8:decoder(OptsList)
; utf16 -> jsx_utf16:decoder(OptsList)
; utf32 -> jsx_utf32:decoder(OptsList)
; {utf16, little} -> jsx_utf16le:decoder(OptsList)
; {utf32, little} -> jsx_utf32le:decoder(OptsList)
; auto -> jsx_utils:detect_encoding(OptsList)
case parse_opts(OptsList) of
{error, badarg} -> {error, badarg}
; Opts ->
case Opts#opts.encoding of
utf8 -> jsx_utf8:decoder(Opts)
; utf16 -> jsx_utf16:decoder(Opts)
; 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.
@ -71,9 +76,13 @@ decoder(OptsList) ->
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().
@ -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).
-include_lib("eunit/include/eunit.hrl").

View file

@ -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),
X == utf8
; X == utf16

View file

@ -28,6 +28,7 @@
-export([decoder/1]).
%% exported solely to facilitate stupid trick i shouldn't be using
-export([start/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(OptsList) ->
case parse_opts(OptsList) of
{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
decoder(Opts) ->
case Opts#opts.iterate of
true -> fun(JSON) -> start(JSON, iterate, [], Opts) end
; false -> fun(JSON) -> start(JSON, [], [], Opts) end
end.
@ -956,7 +951,7 @@ check([H|T], Opts, Acc) ->
decode(JSON, Opts) ->
F = decoder([iterate] ++ Opts),
F = jsx:decoder([iterate] ++ Opts),
loop(F(JSON), []).

View file

@ -23,6 +23,10 @@
-module(jsx_encoder).
-include("jsx_common.hrl").
-export([start/3,
list_or_object/4,
key/4,
@ -34,14 +38,9 @@
-export([encoder/1]).
-include("jsx_common.hrl").
-include("jsx_opts.hrl").
-spec encoder(Opts::#opts{}) -> jsx_encoder().
-spec encoder(OptsList::jsx_opts()) -> jsx_encoder().
encoder(OptsList) ->
Opts = parse_opts(OptsList),
encoder(Opts) ->
case Opts#opts.iterate of
true ->
fun(Forms) -> start(Forms, iterate, Opts) end
@ -217,7 +216,7 @@ encode(Terms) ->
encode_simple(Terms) ->
case (encoder([]))(Terms) of
case (jsx:encoder([]))(Terms) of
{jsx, Terms, _} ->
true
%% matches [foo, end_json], aka naked terms
@ -229,7 +228,7 @@ encode_simple(Terms) ->
encode_iterative(Terms) ->
case loop((encoder([iterate]))(Terms), []) of
case loop((jsx:encoder([iterate]))(Terms), []) of
{ok, Terms} ->
true
%% matches naked terms

View file

@ -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}.