moved function that fakes the jsx parser iterator to jsx so any client program can take advantage of it

This commit is contained in:
alisdair sullivan 2010-08-12 20:08:39 -07:00
parent 422f5958a0
commit 45b9bc1dd0
2 changed files with 13 additions and 10 deletions

View file

@ -30,6 +30,7 @@
-export([json_to_term/1, json_to_term/2]).
-export([is_json/1, is_json/2]).
-export([format/1, format/2]).
-export([eventify/1]).
%% types for function specifications
@ -107,6 +108,16 @@ format(JSON, Opts) ->
jsx_format:format(JSON, Opts).
-spec eventify(List::list()) -> jsx_parser_result().
%% fake the jsx api with a closure to be passed to the pretty printer
eventify([]) ->
fun() -> {incomplete, fun(end_stream) -> eventify([]) end} end;
eventify([Next|Rest]) ->
fun() -> {event, Next, eventify(Rest)} end.
%% ----------------------------------------------------------------------------
%% internal functions
%% ----------------------------------------------------------------------------

View file

@ -58,15 +58,7 @@ term_to_json(List, Opts) ->
; false -> continue
end,
Encoding = proplists:get_value(encoding, Opts, utf8),
jsx:format(event_generator(lists:reverse(term_to_events(List))), [{output_encoding, Encoding}] ++ Opts).
%% fake the jsx api with a closure to be passed to the pretty printer
event_generator([]) ->
fun() -> {event, end_json, fun() -> {incomplete, fun(end_stream) -> event_generator([]) end} end} end;
event_generator([Next|Rest]) ->
fun() -> {event, Next, event_generator(Rest)} end.
jsx:format(jsx:eventify(lists:reverse([end_json] ++ term_to_events(List))), [{output_encoding, Encoding}] ++ Opts).
%% parse opts for the decoder