Example #1
0
 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);
 }
Example #2
0
 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);
        }
Example #4
0
        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));
        }
Example #5
0
        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));
        }
Example #6
0
 protected abstract T valueToData(object value, EzyUnmarshaller unmarshaller);
Example #7
0
 public object read(object input, EzyUnmarshaller unmarshaller)
 {
     return(valueToData(input, unmarshaller));
 }
Example #8
0
 protected abstract T arrayToObject(EzyArray array, EzyUnmarshaller unmarshaller);
Example #9
0
 public object read(object input, EzyUnmarshaller unmarshaller)
 {
     return(arrayToObject((EzyArray)input, unmarshaller));
 }
Example #10
0
 protected abstract T mapToObject(EzyObject map, EzyUnmarshaller unmarshaller);
Example #11
0
 public object read(object input, EzyUnmarshaller unmarshaller)
 {
     return(mapToObject((EzyObject)input, unmarshaller));
 }
Example #12
0
        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);
        }