Comparing version 0.4.1 to 0.4.2
{ | ||
"name": "jjjrmi", | ||
"version": "0.4.1", | ||
"version": "0.4.2", | ||
"keywords": [ | ||
@@ -5,0 +5,0 @@ "util", |
@@ -36,3 +36,3 @@ let Constants = require("./Constants"); | ||
} else if (typeof newInstance.jjjDecode === "function") { | ||
newInstance.decode(this); | ||
newInstance.jjjDecode(this); | ||
} else { | ||
@@ -47,4 +47,4 @@ for (let field in this.json.get(Constants.FieldsParam)) { | ||
} | ||
decodeField(name) { | ||
return this.translator.decode(this.fields[name]); | ||
decodeField(name, callback) { | ||
new Decoder(new EncodedJSON(this.fields[name]), this.translator).decode(r=>callback(r)); | ||
} | ||
@@ -51,0 +51,0 @@ getJavaField(aClass, name) { |
class EncodedJSON{ | ||
constructor(json){ | ||
if (json === null) throw new Error("JSON object is null."); | ||
if (typeof json === "undefined") throw new Error("JSON object is undefined."); | ||
this.json = json; | ||
@@ -4,0 +6,0 @@ } |
@@ -6,2 +6,17 @@ /* global Constants */ | ||
constructor(object, translator, keys) { | ||
if (object !== null && typeof object === "object" && !object instanceof Array) { | ||
if (typeof object.constructor === "undefined") { | ||
throw new Error(`Object missing constructor.`); | ||
} | ||
if (typeof object.constructor.__isTransient !== "function") { | ||
throw new Error(`Class "${object.constructor.name}" missing method "__isTransient".`); | ||
} | ||
if (typeof object.constructor.__isEnum !== "function") { | ||
throw new Error(`Class "${object.constructor.name}" missing method "__isEnum".`); | ||
} | ||
if (typeof object.constructor.__getClass !== "function") { | ||
throw new Error(`Class "${object.constructor.name}" missing method "__getClass".`); | ||
} | ||
} | ||
this.object = object; | ||
@@ -28,2 +43,6 @@ this.translator = translator; | ||
} | ||
/* set to null skips encoding all together */ | ||
else if (this.object["jjjEncode"] === null) { | ||
return null; | ||
} | ||
/* is Enum */ | ||
@@ -34,3 +53,3 @@ else if (this.object.constructor.__isEnum()) { | ||
/* handler has been registered */ | ||
else if (this.translator.hasHandler(this.object.constructor)){ | ||
else if (this.translator.hasHandler(this.object.constructor)) { | ||
let handler = this.translator.getHandler(this.object.constructor); | ||
@@ -42,5 +61,5 @@ let encodedObject = new EncodedObject(this.object, this.translator, this.keys); | ||
/* object handles it's self */ | ||
else if (typeof this.object["jjjEncode"] === "function"){ | ||
else if (typeof this.object["jjjEncode"] === "function") { | ||
let encodedObject = new EncodedObject(this.object, this.translator, this.keys); | ||
this.object.encode(encodedObject); | ||
this.object.jjjEncode(encodedObject); | ||
return encodedObject.toJSON(); | ||
@@ -103,3 +122,4 @@ } | ||
let element = current[i]; | ||
parent.push(new Encoder(element, this.translator, this.keys).encode()); | ||
let value = new Encoder(element, this.translator, this.keys).encode(); | ||
if (value !== null) parent.push(value); | ||
} | ||
@@ -149,3 +169,3 @@ } | ||
encode(){ | ||
encode() { | ||
for (let field in this.object) { | ||
@@ -158,3 +178,4 @@ this.setField(field, this.object[field]); | ||
setField(name, value) { | ||
this.json[Constants.FieldsParam][name] = new Encoder(value, this.translator, this.keys).encode(); | ||
let encodedValue = new Encoder(value, this.translator, this.keys).encode(); | ||
if (encodedValue !== null) this.json[Constants.FieldsParam][name] = encodedValue; | ||
} | ||
@@ -161,0 +182,0 @@ |
@@ -159,3 +159,3 @@ /* global Symbol */ | ||
__decode(resObj) { | ||
jjjDecode(resObj) { | ||
let keys = null; | ||
@@ -179,3 +179,3 @@ let values = null; | ||
__encode(encodedObject) { | ||
jjjEncode(encodedObject) { | ||
let keys = []; | ||
@@ -182,0 +182,0 @@ let values = []; |
@@ -19,2 +19,3 @@ /* global JJJMessageType */ | ||
this.translator.addEncodeListener(obj=>obj.__jjjWebsocket = this); | ||
this.jjjEncode = null; | ||
} | ||
@@ -21,0 +22,0 @@ |
@@ -11,3 +11,3 @@ let Encoder = require("./Encoder"); | ||
} | ||
clear(){ | ||
clear() { | ||
this.objectMap.clear(); | ||
@@ -54,8 +54,9 @@ this.reverseMap.clear(); | ||
class ClassMap{ | ||
constructor(){ | ||
class ClassMap { | ||
constructor() { | ||
this.classmap = new Map(); | ||
} | ||
registerPackage(pkg){ | ||
for (let aClass in pkg) this.registerClass(pkg[aClass]); | ||
registerPackage(pkg) { | ||
for (let aClass in pkg) | ||
this.registerClass(pkg[aClass]); | ||
} | ||
@@ -69,3 +70,3 @@ | ||
getClass(classname){ | ||
getClass(classname) { | ||
if (!this.classmap.has(classname)) throw new Error(`Class ${classname} not registered.`); | ||
@@ -75,4 +76,4 @@ return this.classmap.get(classname); | ||
copyFrom(map){ | ||
for (let classname of map.keys()){ | ||
copyFrom(map) { | ||
for (let classname of map.keys()) { | ||
this.classmap.set(classname, map.get(classname)); | ||
@@ -83,106 +84,112 @@ } | ||
class Translator extends ClassMap{ | ||
constructor() { | ||
class Translator extends ClassMap { | ||
constructor() { | ||
super(); | ||
this.handlers = new Map(); | ||
this.encodeListeners = new ArrayList(); | ||
this.decodeListeners = new ArrayList(); | ||
this.deferred = new ArrayList(); | ||
this.objectMap = new BiMap(); | ||
this.tempReferences = new ArrayList(); | ||
this.nextKey = 0; | ||
} | ||
addDecodeListener(lst) { | ||
this.decodeListeners.add(lst); | ||
} | ||
addEncodeListener(lst) { | ||
this.encodeListeners.add(lst); | ||
} | ||
addReference(reference, object) { | ||
this.objectMap.put(reference, object); | ||
} | ||
addTempReference(reference, object) { | ||
this.objectMap.put(reference, object); | ||
this.tempReferences.add(reference); | ||
} | ||
allocNextKey() { | ||
return "C" + this.nextKey++; | ||
} | ||
clear() { | ||
this.objectMap.clear(); | ||
this.tempReferences.clear(); | ||
} | ||
clearTempReferences() { | ||
for(let ref of this.tempReferences){ | ||
this.removeByKey(ref); | ||
} | ||
this.tempReferences.clear(); | ||
} | ||
decode(json) { | ||
this.handlers = new Map(); | ||
this.encodeListeners = new ArrayList(); | ||
this.decodeListeners = new ArrayList(); | ||
this.deferred = new ArrayList(); | ||
this.objectMap = new BiMap(); | ||
this.tempReferences = new ArrayList(); | ||
this.nextKey = 0; | ||
} | ||
addDecodeListener(lst) { | ||
this.decodeListeners.add(lst); | ||
} | ||
addEncodeListener(lst) { | ||
this.encodeListeners.add(lst); | ||
} | ||
addReference(reference, object) { | ||
this.objectMap.put(reference, object); | ||
} | ||
addTempReference(reference, object) { | ||
this.objectMap.put(reference, object); | ||
this.tempReferences.add(reference); | ||
} | ||
allocNextKey() { | ||
return "C" + this.nextKey++; | ||
} | ||
clear() { | ||
this.objectMap.clear(); | ||
this.tempReferences.clear(); | ||
} | ||
clearTempReferences() { | ||
for (let ref of this.tempReferences) { | ||
this.removeByKey(ref); | ||
} | ||
this.tempReferences.clear(); | ||
} | ||
decode(json) { | ||
if (json === null) throw new Error("JSON object is null."); | ||
if (typeof json === "undefined") throw new Error("JSON object is undefined."); | ||
if (typeof json === "string") json = JSON.parse(json); | ||
let rvalue = null; | ||
let rvalue = null; | ||
let eson = new EncodedJSON(json); | ||
new Decoder(eson, this, null).decode(r =>{ | ||
while (!this.deferred.isEmpty()) this.deferred.remove(0).resume(); | ||
new Decoder(eson, this, null).decode(r => { | ||
while (!this.deferred.isEmpty()) | ||
this.deferred.remove(0).resume(); | ||
this.clearTempReferences(); | ||
rvalue = r; | ||
}); | ||
return rvalue; | ||
} | ||
deferDecoding(decoder) { | ||
this.deferred.add(decoder); | ||
} | ||
encode(object) { | ||
let toJSON = new Encoder(object, this).encode(); | ||
this.clearTempReferences(); | ||
return toJSON; | ||
} | ||
getAllReferredObjects() { | ||
let values = this.objectMap.values(); | ||
return new ArrayList(values); | ||
} | ||
getHandler(aClass) { | ||
return this.handlers.get(aClass.__getClass()); | ||
} | ||
hasHandler(aClass) { | ||
return this.handlers.has(aClass.__getClass()); | ||
} | ||
setHandler(aClass, handler) { | ||
this.handlers.set(aClass.__getClass(), handler); | ||
} | ||
return rvalue; | ||
} | ||
deferDecoding(decoder) { | ||
this.deferred.add(decoder); | ||
} | ||
encode(object) { | ||
let toJSON = new Encoder(object, this).encode(); | ||
this.clearTempReferences(); | ||
return toJSON; | ||
} | ||
getAllReferredObjects() { | ||
let values = this.objectMap.values(); | ||
return new ArrayList(values); | ||
} | ||
getHandler(aClass) { | ||
return this.handlers.get(aClass.__getClass()); | ||
} | ||
hasHandler(aClass) { | ||
return this.handlers.has(aClass.__getClass()); | ||
} | ||
setHandler(aClass, handler) { | ||
this.handlers.set(aClass.__getClass(), handler); | ||
} | ||
getReference(object) { | ||
return this.objectMap.getKey(object); | ||
} | ||
getReferredObject(reference) { | ||
return this.objectMap.get(reference); | ||
} | ||
hasReference(reference) { | ||
return this.objectMap.containsKey(reference); | ||
} | ||
hasReferredObject(object) { | ||
return this.objectMap.containsValue(object); | ||
} | ||
notifyDecode(object) { | ||
for(let decodeListener of this.decodeListeners){ | ||
decodeListener(object); | ||
} | ||
} | ||
notifyEncode(object) { | ||
for(let encodeListener of this.encodeListeners){ | ||
encodeListener(object); | ||
} | ||
} | ||
removeByKey(key) { | ||
if (!this.objectMap.containsKey(key))return false; | ||
this.objectMap.removeByKey(key); | ||
return true; | ||
} | ||
removeByValue(obj) { | ||
if (!this.objectMap.containsValue(obj))return false; | ||
return this.objectMap.getKey(object); | ||
} | ||
getReferredObject(reference) { | ||
return this.objectMap.get(reference); | ||
} | ||
hasReference(reference) { | ||
return this.objectMap.containsKey(reference); | ||
} | ||
hasReferredObject(object) { | ||
return this.objectMap.containsValue(object); | ||
} | ||
notifyDecode(object) { | ||
for (let decodeListener of this.decodeListeners) { | ||
decodeListener(object); | ||
} | ||
} | ||
notifyEncode(object) { | ||
for (let encodeListener of this.encodeListeners) { | ||
encodeListener(object); | ||
} | ||
} | ||
removeByKey(key) { | ||
if (!this.objectMap.containsKey(key)) | ||
return false; | ||
this.objectMap.removeByKey(key); | ||
return true; | ||
} | ||
removeByValue(obj) { | ||
if (!this.objectMap.containsValue(obj)) | ||
return false; | ||
this.objectMap.remove(this.objectMap.getKey(obj)); | ||
return true; | ||
} | ||
}; | ||
this.objectMap.remove(this.objectMap.getKey(obj)); | ||
return true; | ||
} | ||
} | ||
; | ||
module.exports = Translator; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
41598
1182
18