Permalink
Browse files

Experimental jiffy support

  • Loading branch information...
1 parent a700255 commit a6e9dd64b4dbe9979b66be48285e56d5e9280710 @majek majek committed Feb 14, 2012
Showing with 43 additions and 1 deletion.
  1. +2 −0 rebar-cowboy.config
  2. +2 −0 rebar-misultin.config
  3. +2 −0 rebar.config
  4. +37 −1 src/sockjs_json.erl
View
@@ -12,6 +12,8 @@
]}.
{deps, [
+ {jiffy, "0.4.0",
+ {git, "https://github.com/davisp/jiffy.git", {tag, "0.4.0"}}},
{cowboy, ".*",
{git, "git://github.com/extend/cowboy.git", "e7b6e2a402922724"}}
]}.
View
@@ -12,6 +12,8 @@
]}.
{deps, [
+ {jiffy, "0.4.0",
+ {git, "https://github.com/davisp/jiffy.git", {tag, "0.4.0"}}},
{misultin, ".*",
{git, "git://github.com/ostinelli/misultin.git", "1973f5c875c1348b"}}
]}.
View
@@ -14,4 +14,6 @@
]}.
{deps, [
+ {jiffy, "0.4.0",
+ {git, "https://github.com/davisp/jiffy.git", {tag, "0.4.0"}}}
]}.
View
@@ -6,12 +6,48 @@
-spec encode(any()) -> iodata().
encode(Thing) ->
- mochijson2_fork:encode(Thing).
+ encode(encoder(), Thing).
-spec decode(iodata()) -> {ok, any()} | {error, any()}.
decode(Encoded) ->
+ decode(encoder(), Encoded).
+
+
+encoder() ->
+ case get(sockjs_json_impl) of
+ undefined ->
+ E = case application:get_env(sockjs, json_impl) of
+ {ok, Encoder} -> Encoder;
+ undefined -> jiffy %mochijson2_fork
+ end,
+ undefined = put(sockjs_json_impl, E),
+ E;
+ E -> E
+ end.
+
+%% -----
+
+-spec encode(mochijson2_fork | jiffy, any()) -> iodata().
+encode(mochijson2_fork, Thing) ->
+ mochijson2_fork:encode(Thing);
+
+encode(jiffy, Thing) ->
+ jiffy:encode(Thing).
+
+%% -----
+
+-spec decode(mochijson2_fork | jiffy, iodata()) -> {ok, any()} | {error, any()}.
+decode(mochijson2_fork, Encoded) ->
try mochijson2_fork:decode(Encoded) of
V -> {ok, V}
catch
E -> {error, E}
+ end;
+
+decode(jiffy, Encoded) ->
+ io:format("Encoded: ~p~n", [Encoded]),
+ try jiffy:decode(Encoded) of
+ V -> {ok, V}
+ catch
+ E -> E
end.

0 comments on commit a6e9dd6

Please sign in to comment.