From 45b9bc1dd050a71293d3243fa085060a7b146c7f Mon Sep 17 00:00:00 2001 From: alisdair sullivan Date: Thu, 12 Aug 2010 20:08:39 -0700 Subject: [PATCH] moved function that fakes the jsx parser iterator to jsx so any client program can take advantage of it --- src/jsx.erl | 13 ++++++++++++- src/jsx_eep0018.erl | 10 +--------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/jsx.erl b/src/jsx.erl index b552dc3..0616036 100644 --- a/src/jsx.erl +++ b/src/jsx.erl @@ -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 @@ -104,7 +105,17 @@ format(JSON) -> format(JSON, []). format(JSON, Opts) -> - jsx_format: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. %% ---------------------------------------------------------------------------- diff --git a/src/jsx_eep0018.erl b/src/jsx_eep0018.erl index 19211b3..9f0021b 100644 --- a/src/jsx_eep0018.erl +++ b/src/jsx_eep0018.erl @@ -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