protected override DateTime valueToData( object value, EzyUnmarshaller unmarshaller) { if (value is Int64) { return(new DateTime(1970, 1, 1).AddMilliseconds((long)value)); } if (value is Int32) { return(new DateTime(1970, 1, 1).AddMilliseconds((Int32)value)); } if (value is string) { try { return(DateTime.ParseExact( (string)value, "yyyy-MM-dd'T'HH:mm:ss:fff", CultureInfo.InvariantCulture)); } catch (Exception e) { return(DateTime.ParseExact( (string)value, "yyyy-MM-dd'T'HH:mm:ss.fff", CultureInfo.InvariantCulture)); } } return((DateTime)value); }
public EzyBinding( IDictionary <Type, IEzyWriter> writerByInType, IDictionary <Type, IEzyReader> readerByOutType) { this.marshaller = new EzyMarshaller(writerByInType); this.unmarshaller = new EzyUnmarshaller(readerByOutType); }
protected override T arrayToObject(EzyArray array, EzyUnmarshaller unmarshaller) { T obj = (T)Activator.CreateInstance(objectType); PropertyInfo[] properties = objectType.GetProperties(); for (int i = 0; i < properties.Length; ++i) { PropertyInfo targetProperty = properties[i]; EzyValue anno = targetProperty.GetCustomAttribute <EzyValue>(); int index = anno != null ? anno.index : i; if (index >= array.size()) { continue; } Type outType = targetProperty.PropertyType; object rawValue = array.getByOutType(index, outType); if (rawValue == null) { continue; } object value = unmarshaller.unmarshallByOutType(rawValue, outType); if (targetProperty.PropertyType == value.GetType()) { targetProperty.SetValue(obj, value); } else { MethodInfo parseMethod = targetProperty.PropertyType.GetMethod( "TryParse", BindingFlags.Public | BindingFlags.Static, null, new[] { typeof(string), targetProperty.PropertyType.MakeByRefType() }, null ); if (parseMethod != null) { object[] parameters = new[] { value, null }; bool success = (bool)parseMethod.Invoke(null, parameters); if (success) { targetProperty.SetValue(obj, parameters[1]); } } } } return(obj); }
public object read(object input, EzyUnmarshaller unmarshaller) { EzyArray array = null; if (input is IList) { array = EzyEntityFactory.newArrayBuilder() .appendRawList((IList)input) .build(); } else { array = (EzyArray)input; } return(arrayToObject(array, unmarshaller)); }
public object read(object input, EzyUnmarshaller unmarshaller) { EzyObject map = null; if (input is IDictionary) { map = EzyEntityFactory.newObjectBuilder() .appendRawDict((IDictionary)input) .build(); } else { map = (EzyObject)input; } return(mapToObject(map, unmarshaller)); }
protected abstract T valueToData(object value, EzyUnmarshaller unmarshaller);
public object read(object input, EzyUnmarshaller unmarshaller) { return(valueToData(input, unmarshaller)); }
protected abstract T arrayToObject(EzyArray array, EzyUnmarshaller unmarshaller);
public object read(object input, EzyUnmarshaller unmarshaller) { return(arrayToObject((EzyArray)input, unmarshaller)); }
protected abstract T mapToObject(EzyObject map, EzyUnmarshaller unmarshaller);
public object read(object input, EzyUnmarshaller unmarshaller) { return(mapToObject((EzyObject)input, unmarshaller)); }
protected override T mapToObject(EzyObject map, EzyUnmarshaller unmarshaller) { PropertyInfo[] properties = objectType.GetProperties(); T obj = (T)Activator.CreateInstance(objectType); foreach (PropertyInfo property in properties) { Type outType = property.PropertyType; object rawValue = null; EzyValue anno = property.GetCustomAttribute <EzyValue>(); if (anno != null) { rawValue = map.getByOutType(anno.name, outType); } else { rawValue = map.getByOutType(property.Name, outType); if (rawValue == null) { string keyString = char.ToLower(property.Name[0]).ToString(); if (property.Name.Length > 1) { keyString += property.Name.Substring(1); } rawValue = map.getByOutType(keyString, outType); } } if (rawValue == null) { continue; } object value = unmarshaller.unmarshallByOutType(rawValue, outType); if (outType == value.GetType()) { property.SetValue(obj, value); } else { MethodInfo parseMethod = property.PropertyType.GetMethod( "TryParse", BindingFlags.Public | BindingFlags.Static, null, new[] { typeof(string), property.PropertyType.MakeByRefType() }, null ); if (parseMethod != null) { object[] parameters = new[] { value, null }; bool success = (bool)parseMethod.Invoke(null, parameters); if (success) { property.SetValue(obj, parameters[1]); } } } } return(obj); }