| 4 | see: https://github.com/dcodeIO/PSON for details |
| 5 | */ |
| 6 | (function(k){function n(k){if(!k)throw Error("PSON requires ByteBuffer.js: Get it at https://github.com/dcodeIO/ByteBuffer.js");var e={T:{ZERO:0,MAX:239,NULL:240,TRUE:241,FALSE:242,EOBJECT:243,EARRAY:244,ESTRING:245,OBJECT:246,ARRAY:247,INTEGER:248,LONG:249,FLOAT:250,DOUBLE:251,STRING:252,STRING_ADD:253,STRING_GET:254,BINARY:255}};e.Encoder=function(f,c){var e=new f(4);e.length=4;var g=f.Long,a=function(b,d){this.dict={};this.next=0;if(b&&Array.isArray(b))for(;this.next<b.length;)this.dict[b[this.next]]= |
| 7 | this.next++;this.progressive=!!d};a.prototype.encode=function(b,d){d||(d=new f);var c=d.littleEndian;try{return this._encodeValue(b,d.LE()),d.littleEndian=c,d}catch(a){throw d.littleEndian=c,a;}};a.prototype._encodeValue=function(b,d,a){if(null===b)d.writeUint8(c.NULL);else switch(typeof b){case "function":b=b.toString();case "string":0==b.length?d.writeUint8(c.ESTRING):this.dict.hasOwnProperty(b)?(d.writeUint8(c.STRING_GET),d.writeVarint32(this.dict[b])):(d.writeUint8(c.STRING),d.writeVString(b)); |
| 8 | break;case "number":a=parseInt(b);b===a?(a=f.zigZagEncode32(b),a<=c.MAX?d.writeUint8(a):(d.writeUint8(c.INTEGER),d.writeZigZagVarint32(b))):(e.writeFloat32(b,0),b===e.readFloat32(0)?(d.writeUint8(c.FLOAT),d.writeFloat32(b)):(d.writeUint8(c.DOUBLE),d.writeFloat64(b)));break;case "boolean":d.writeUint8(b?c.TRUE:c.FALSE);break;case "object":var h;if(Array.isArray(b))if(0==b.length)d.writeUint8(c.EARRAY);else for(d.writeUint8(c.ARRAY),d.writeVarint32(b.length),h=0;h<b.length;h++)this._encodeValue(b[h], |
| 9 | d);else if(g&&b instanceof g)d.writeUint8(c.LONG),d.writeZigZagVarint64(b);else try{b=f.wrap(b),d.writeUint8(c.BINARY),d.writeVarint32(b.remaining()),d.append(b)}catch(k){var m=Object.keys(b),l=0;for(h=0;h<m.length;h++)"undefined"!==typeof b[m[h]]&&l++;if(0===l)d.writeUint8(c.EOBJECT);else for(d.writeUint8(c.OBJECT),d.writeVarint32(l),a||(a=!!b._PSON_EXCL_),h=0;h<m.length;h++)l=m[h],"undefined"!==typeof b[l]&&(this.dict.hasOwnProperty(l)?(d.writeUint8(c.STRING_GET),d.writeVarint32(this.dict[l])): |
| 10 | (this.progressive&&!a?(this.dict[l]=this.next++,d.writeUint8(c.STRING_ADD)):d.writeUint8(c.STRING),d.writeVString(l)),this._encodeValue(b[l],d))}break;case "undefined":d.writeUint8(c.NULL)}};return a}(k,e.T);e.Decoder=function(f,c){var e=f.Long,g=function(a,b){this.dict=a&&Array.isArray(a)?a:[];this.progressive=!!b};g.prototype.decode=function(a){a instanceof f||(a=f.wrap(a));var b=a.littleEndian;try{var c=this._decodeValue(a.LE());a.littleEndian=b;return c}catch(e){throw a.littleEndian=b,e;}};g.prototype._decodeValue= |