org.hisrc.jsonix.Jsonix.Model.EnumLeafInfo.js Maven / Gradle / Ivy
Jsonix.Model.EnumLeafInfo = Jsonix.Class(Jsonix.Model.TypeInfo, {
name : null,
baseTypeInfo : 'String',
entries : null,
keys : null,
values : null,
built : false,
initialize : function(mapping) {
Jsonix.Model.TypeInfo.prototype.initialize.apply(this, []);
Jsonix.Util.Ensure.ensureObject(mapping);
var n = mapping.name||mapping.n||undefined;
Jsonix.Util.Ensure.ensureString(n);
this.name = n;
var bti = mapping.baseTypeInfo||mapping.bti||'String';
this.baseTypeInfo = bti;
var vs = mapping.values||mapping.vs||undefined;
Jsonix.Util.Ensure.ensureExists(vs);
if (!(Jsonix.Util.Type.isObject(vs) || Jsonix.Util.Type.isArray(vs))) {
throw new Error('Enum values must be either an array or an object.');
}
else
{
this.entries = vs;
}
},
build : function(context, module) {
if (!this.built) {
this.baseTypeInfo = context.resolveTypeInfo(this.baseTypeInfo, module);
this.baseTypeInfo.build(context, module);
var items = this.entries;
var entries = {};
var keys = [];
var values = [];
var index = 0;
var key;
var value;
// If values is an array, process individual items
if (Jsonix.Util.Type.isArray(items))
{
// Build properties in this context
for (index = 0; index < items.length; index++) {
value = items[index];
if (Jsonix.Util.Type.isString(value)) {
key = value;
if (!(Jsonix.Util.Type.isFunction(this.baseTypeInfo.parse)))
{
throw new Error('Enum value is provided as string but the base type ['+this.baseTypeInfo.name+'] of the enum info [' + this.name + '] does not implement the parse method.');
}
// Using null as input since input is not available
value = this.baseTypeInfo.parse(value, context, null, this);
}
else
{
if (this.baseTypeInfo.isInstance(value, context, this))
{
if (!(Jsonix.Util.Type.isFunction(this.baseTypeInfo.print)))
{
throw new Error('The base type ['+this.baseTypeInfo.name+'] of the enum info [' + this.name + '] does not implement the print method, unable to produce the enum key as string.');
}
// Using null as output since output is not available at this moment
key = this.baseTypeInfo.print(value, context, null, this);
}
else
{
throw new Error('Enum value [' + value + '] is not an instance of the enum base type [' + this.baseTypeInfo.name + '].');
}
}
entries[key] = value;
keys[index] = key;
values[index] = value;
}
}
else if (Jsonix.Util.Type.isObject(items))
{
for (key in items) {
if (items.hasOwnProperty(key)) {
value = items[key];
if (Jsonix.Util.Type.isString(value)) {
if (!(Jsonix.Util.Type.isFunction(this.baseTypeInfo.parse)))
{
throw new Error('Enum value is provided as string but the base type ['+this.baseTypeInfo.name+'] of the enum info [' + this.name + '] does not implement the parse method.');
}
// Using null as input since input is not available
value = this.baseTypeInfo.parse(value, context, null, this);
}
else
{
if (!this.baseTypeInfo.isInstance(value, context, this))
{
throw new Error('Enum value [' + value + '] is not an instance of the enum base type [' + this.baseTypeInfo.name + '].');
}
}
entries[key] = value;
keys[index] = key;
values[index] = value;
index++;
}
}
}
else {
throw new Error('Enum values must be either an array or an object.');
}
this.entries = entries;
this.keys = keys;
this.values = values;
this.built = true;
}
},
unmarshal : function(context, input, scope) {
var text = input.getElementText();
return this.parse(text, context, input, scope);
},
marshal : function(value, context, output, scope) {
if (Jsonix.Util.Type.exists(value)) {
output.writeCharacters(this.reprint(value, context, output, scope));
}
},
reprint : function(value, context, output, scope) {
if (Jsonix.Util.Type.isString(value) && !this.isInstance(value, context, scope)) {
// Using null as input since input is not available
return this.print(this.parse(value, context, null, scope), context, output, scope);
} else {
return this.print(value, context, output, scope);
}
},
print : function(value, context, output, scope) {
for (var index = 0; index < this.values.length; index++)
{
if (this.values[index] === value)
{
return this.keys[index];
}
}
throw new Error('Value [' + value + '] is invalid for the enum type [' + this.name + '].');
},
parse : function(text, context, input, scope) {
Jsonix.Util.Ensure.ensureString(text);
if (this.entries.hasOwnProperty(text))
{
return this.entries[text];
}
else
{
throw new Error('Value [' + text + '] is invalid for the enum type [' + this.name + '].');
}
},
isInstance : function(value, context, scope) {
for (var index = 0; index < this.values.length; index++)
{
if (this.values[index] === value)
{
return true;
}
}
return false;
},
CLASS_NAME : 'Jsonix.Model.EnumLeafInfo'
});
© 2015 - 2024 Weber Informatics LLC | Privacy Policy