JS Hessian 二进制Web Service协议:hessian.js
JS Hessian 二进制Web Service协议,支持JavaScript与Java通信。
Hessian Serialization 1.0 和 2.0 实现,纯JavaScript实现。支持Java中的所有类型。
Hessian是Caucho公司所定义的一个remoting on http的轻量级的remoting工具。基于自定义的binary的协议。起初只有Java的实现,而现在是4面开花,.Net,C++,Ruby,PHP的实现皆有出现.而且重要的一点是,所有client和server是互通的,也就是php可以call java的server。
支持类型
8 primitive types:
- raw binary data
- boolean
- 64-bit millisecond date
- 64-bit double
- 32-bit int
- 64-bit long
- null
- UTF8-encoded string
3 recursive types:
list
for lists and arraysmap
for maps and dictionariesobject
for objects
one special contruct:
- ref for shared and circular object references
Hessian 2.0 has 3 internal reference maps:
- An object/list reference map.
- An class definition reference map.
- A type (class name) reference map.
Encoder
Simple javascript type
var hessian = require('hessian.js'); var encoder = new hessian.Encoder(); encoder.write(1); // int encoder.write(1.1); // double encoder.write(1e100); // double encoder.write(Math.pow(2, 18)); // long encoder.write(true); // boolean encoder.write(null); // null encoder.write('test'); // string // java base types encoder.write(hessian.java.long(3001010320)); // 3001010320L encoder.write(hessian.java.double(100)); // double encoder.write(hessian.java.intList([0, 1, 2])); // int[] = {0, 1, 2} var object = {}; object.prop1 = [1, 2, 3]; object.prop2 = 'string'; object.prop3 = {key: 'value'}; object.prop4 = object; // circular encoder.write(object); // object
Complex java type
var hessian = require('hessian.js'); var encoder = new hessian.Encoder(); var long = { $class: 'java.lang.Long', $: 1 } encoder.write(long); // long type var testObject = { $class: 'com.hessian.TestObject', $: { a: 1, b: 'test', c: {$class: 'java.lang.Long', $: 123} } }; encoder.write(testObject);
Decoder
var hessian = require('hessian.js'); var decoder = new hessian.Decoder(buf); decoder.read(); //return what it is decoder.readNull(); decoder.readBool(); decoder.readInt(); decoder.readLong(); decoder.readDouble(); decoder.readDate(); decoder.readObect(); decoder.readMap(); decoder.readArray(); decoder.readList(); decoder.readRef();
Simple Usage
hessian 1.0:
var hessian = require('hessian.js'); var testObject = { a: 1, b: 'string', c: true, d: 1.1, e: Math.pow(2, 40), f: [1, 2, 3, '4', true, 5], g: {a: 1, b: true, c: 'string'} }; var buf; try { buf = hessian.encode(testObject); } catch (err) { console.log('encode error: ', err.message); process.exit(1); } try { var res = hessian.decode(buf); // res.should.eql(testObject); } catch (err) { console.log('decode error: ', err.message); }
hessian 2.0:
var hessian = require('hessian.js'); var testObject = { a: 1, b: 'string', c: true, d: 1.1, e: Math.pow(2, 40), f: [1, 2, 3, '4', true, 5], g: {a: 1, b: true, c: 'string'} }; var buf; try { buf = hessian.encode(testObject, '2.0'); } catch (err) { console.log('encode error: ', err.message); process.exit(1); } try { var res = hessian.decode(buf, '2.0'); // res.should.eql(testObject); } catch (err) { console.log('decode error: ', err.message); }
本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
转载本站原创文章,请注明出处,并保留原始链接、图片水印。
本站是一个以用户分享为主的开源技术平台,欢迎各类分享!