summaryrefslogtreecommitdiff
path: root/src/couchdb
diff options
context:
space:
mode:
authorAdam Kocoloski <kocolosk@apache.org>2009-11-21 17:01:50 +0000
committerAdam Kocoloski <kocolosk@apache.org>2009-11-21 17:01:50 +0000
commit3a4fad07cb5097598eefd0cd7f588c5881a61c8f (patch)
tree6945d805eb72a37a5b27ae14a7ec2d1e8690fdac /src/couchdb
parenta2ebd6155b8171befd8f55b42c48bc563bb330b0 (diff)
upgrade mochiweb to r113, use hooks instead of forking mochijson2. COUCHDB-474
git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@882941 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/couchdb')
-rw-r--r--src/couchdb/couch_db.hrl4
-rw-r--r--src/couchdb/couch_util.erl14
2 files changed, 16 insertions, 2 deletions
diff --git a/src/couchdb/couch_db.hrl b/src/couchdb/couch_db.hrl
index bd297b2a..f2ccb453 100644
--- a/src/couchdb/couch_db.hrl
+++ b/src/couchdb/couch_db.hrl
@@ -17,8 +17,8 @@
-define(MIN_STR, <<"">>).
-define(MAX_STR, <<255>>). % illegal utf string
--define(JSON_ENCODE(V), mochijson2:encode(V)).
--define(JSON_DECODE(V), mochijson2:decode(V)).
+-define(JSON_ENCODE(V), couch_util:json_encode(V)).
+-define(JSON_DECODE(V), couch_util:json_decode(V)).
-define(b2a(V), list_to_atom(binary_to_list(V))).
-define(b2l(V), binary_to_list(V)).
diff --git a/src/couchdb/couch_util.erl b/src/couchdb/couch_util.erl
index d02be8d9..da62375c 100644
--- a/src/couchdb/couch_util.erl
+++ b/src/couchdb/couch_util.erl
@@ -20,6 +20,7 @@
to_hex/1,parse_term/1, dict_find/3]).
-export([file_read_size/1, get_nested_json_value/2, json_user_ctx/1]).
-export([to_binary/1, to_integer/1, to_list/1, url_encode/1]).
+-export([json_encode/1, json_decode/1]).
-include("couch_db.hrl").
-include_lib("kernel/include/file.hrl").
@@ -406,3 +407,16 @@ url_encode([H|T]) ->
end;
url_encode([]) ->
[].
+
+json_encode(V) ->
+ Handler =
+ fun({L}) when is_list(L) ->
+ {struct,L};
+ (Bad) ->
+ exit({json_encode, {bad_term, Bad}})
+ end,
+ (mochijson2:encoder([{handler, Handler}]))(V).
+
+json_decode(V) ->
+ try (mochijson2:decoder([{object_hook, fun({struct,L}) -> {L} end}]))(V)
+ catch _:_ -> throw({invalid_json,V}) end.