From 39605da171c7608bbf9a1fd0ea61d50d69b7be37 Mon Sep 17 00:00:00 2001 From: alisdair sullivan Date: Wed, 10 Aug 2011 01:44:38 -0700 Subject: [PATCH] add collect function to jsx_utils for later use --- src/jsx_utils.erl | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/jsx_utils.erl b/src/jsx_utils.erl index 9efe253..28705e1 100644 --- a/src/jsx_utils.erl +++ b/src/jsx_utils.erl @@ -23,12 +23,20 @@ -module(jsx_utils). --export([nice_decimal/1, detect_encoding/1, detect_encoding/2]). +-export([nice_decimal/1, detect_encoding/1, detect_encoding/2, collect/1]). --ifdef(TEST). --include_lib("eunit/include/eunit.hrl"). --endif. +-spec collect(F::function()) -> {jsx, list(), function()}. + +collect(F) when is_function(F) -> + collect(F(), []). + +collect({error, _}, _) -> {error, badarg}; +collect({jsx, incomplete, More}, Acc) -> + {jsx, incomplete, fun(Stream) -> collect(More(Stream), Acc) end}; +collect({jsx, end_json, Next}, Acc) -> + {jsx, lists:reverse([end_json] ++ Acc), Next}; +collect({jsx, Event, Next}, Acc) -> collect(Next(), [Event] ++ Acc). @@ -269,6 +277,7 @@ detect_encoding(Bin, Opts) -> %% eunit tests -ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). nice_decimal_test_() -> [