EVReflection
EVReflection copied to clipboard
Cannot decode object to int id
If using propertyConverters to convert an id to an object when encoding. Upon decoding, the convert does not replace the object with an ID but instead replaces it with an empty dictionary.
For example:
var object_id : Object = Object()
...
key: "object_id"
, decodeConverter: {
if let obj = ObjectsCollection.getById($0 as! Int) {
self.object_id = obj
}
}
, encodeConverter: {
return self.object_id.id
}
In EVReflection:
// Convert the Any value to a NSObject value
var (unboxedValue, valueType, isObject) = valueForAny(theObject, key: originalKey, anyValue: value, conversionOptions: conversionOptions, isCachable: isCachable, parents: parents)
if let v = value as? EVCustomReflectable {
unboxedValue = v.toCodableValue() as AnyObject
valueType = "String"
isObject = false
}
if conversionOptions.contains(.Encoding), let ro = theObject as? EVReflectable {
unboxedValue = ro.encodePropertyValue(value: unboxedValue, key: originalKey) as AnyObject
}
isObject is set to true (because it's an object) and then below:
if isObject {
if let obj = unboxedValue as? EVReflectable {
if let json = obj.customConverter() {
unboxedValue = json as AnyObject
} else {
// sub objects will be added as a dictionary itself.
let (dict, _) = toDictionary(unboxedValue as? NSObject ?? NSObject(), conversionOptions: conversionOptions, isCachable: isCachable, parents: parents)
unboxedValue = dict
}
} else {
// sub objects will be added as a dictionary itself.
let (dict, _) = toDictionary(unboxedValue as? NSObject ?? NSObject(), conversionOptions: conversionOptions, isCachable: isCachable, parents: parents)
unboxedValue = dict
}
} else if let array = unboxedValue as? [NSObject] {
unboxedValue gets set to an empty dict rather then being set as a value further down