From 5b39099d8368047f5d0ce7ac20b4026de19dc87c Mon Sep 17 00:00:00 2001 From: yhmtg Date: Mon, 18 Aug 2014 23:28:46 +0200 Subject: [PATCH] Messagepack support. You will need this Messagepack library: https://github.com/msgpack/msgpack-javascript Usage: --- src/com/cognitect/transit_with_msgpack.js | 35 +++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/com/cognitect/transit_with_msgpack.js diff --git a/src/com/cognitect/transit_with_msgpack.js b/src/com/cognitect/transit_with_msgpack.js new file mode 100644 index 0000000..5b49901 --- /dev/null +++ b/src/com/cognitect/transit_with_msgpack.js @@ -0,0 +1,35 @@ + +"use strict"; + +goog.require("com.cognitect.transit"); + +var transit = com.cognitect.transit, + org_reader_func = transit.reader, + org_writer_func = transit.writer; + +transit.reader = function (type, opts) { + if (type === "msgpack") { + var json_reader = org_reader_func("json", opts), + old_read_func = json_reader.read; + json_reader.read = function (bytes) { + var unpacked = msgpack.unpack(bytes), + unpacked_string = JSON.stringify(unpacked); + return old_read_func.call(json_reader, unpacked_string); + }; + return json_reader; + } + return org_reader_func(type, opts); +}; + +transit.writer = function (type, opts) { + if (type === "msgpack") { + var json_writer = opts === undefined ? org_writer_func("json") : org_writer_func("json", opts), + old_write_func = json_writer.write; + json_writer.write = function (obj, opts_i) { + var transit_json = opts_i === undefined ? old_write_func.call(json_writer, obj) : old_write_func.call(json_writer, obj, opts_i); + return msgpack.pack(JSON.parse(transit_json)); + }; + return json_writer; + } + return org_writer_func(type, opts); +}; \ No newline at end of file