From 4a078bd44a1f67a4d962c616d20162003853a21a Mon Sep 17 00:00:00 2001 From: alisdair sullivan Date: Wed, 10 Aug 2011 01:28:25 -0700 Subject: [PATCH] move opts handling to jsx.erl for earlier failure on badopts --- src/jsx.erl | 47 +++++++++++++++++++++++++++++++++++--------- src/jsx_common.hrl | 10 ++++++++++ src/jsx_decoder.hrl | 19 +++++++----------- src/jsx_encoder.erl | 17 ++++++++-------- src/jsx_opts.hrl | 48 --------------------------------------------- 5 files changed, 63 insertions(+), 78 deletions(-) delete mode 100644 src/jsx_opts.hrl diff --git a/src/jsx.erl b/src/jsx.erl index 261409c..003fa8d 100644 --- a/src/jsx.erl +++ b/src/jsx.erl @@ -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"). diff --git a/src/jsx_common.hrl b/src/jsx_common.hrl index daf23f3..4ed0c20 100644 --- a/src/jsx_common.hrl +++ b/src/jsx_common.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), X == utf8 ; X == utf16 diff --git a/src/jsx_decoder.hrl b/src/jsx_decoder.hrl index 00bc8c0..865cf6a 100644 --- a/src/jsx_decoder.hrl +++ b/src/jsx_decoder.hrl @@ -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), []). diff --git a/src/jsx_encoder.erl b/src/jsx_encoder.erl index f27e649..e37530c 100644 --- a/src/jsx_encoder.erl +++ b/src/jsx_encoder.erl @@ -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 diff --git a/src/jsx_opts.hrl b/src/jsx_opts.hrl deleted file mode 100644 index ee301be..0000000 --- a/src/jsx_opts.hrl +++ /dev/null @@ -1,48 +0,0 @@ -%% The MIT License - -%% Copyright (c) 2010 Alisdair Sullivan - -%% 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}. \ No newline at end of file