Example #1
0
        /// <summary>
        /// Deserialises the specified value node into a .NET object
        /// </summary>
        /// <param name="t"></param>
        /// <param name="node"></param>
        /// <returns></returns>
        public object Deserialise(Type t, ValueNode node)
        {
            // Instantiate
            if (t.IsAbstract) throw new Exception($"Can't instantiate type '{t}'");
            object obj;
            try
            {
                // Special case for arrays
                if (t.IsArray)
                {
                    obj = Activator.CreateInstance(t, node.As<ArrayValue>().Count);
                }
                else
                {
                    obj = Activator.CreateInstance(t);
                }
            }
            catch (Exception ex)
            {
                // TODO: Handle this better
                throw ex;
            }

            // Populate
            Deserialise(obj, node);

            // Return
            return obj;
        }
Example #2
0
 public static object Deserialise(ValueNode node, Type expected = null, TranslateOptions tOpts = null)
 {
     if (tOpts == null) tOpts = TranslateOptions.Default;
     object obj;
     tOpts.Deserialise(expected, node, out obj);
     return obj;
 }
Example #3
0
 /// <summary>
 /// Deserialises the specified value node into a .NET object
 /// </summary>
 /// <param name="t"></param>
 /// <param name="node"></param>
 /// <returns></returns>
 public object Deserialise(Type t, ValueNode node)
 {
     if (t == typeof(object))
     {
         return ToBasicObject(node);
     }
     ToFro toFro;
     if (!typeMap.TryGetValue(t, out toFro)) return null;
     return toFro.FromModel(node);
 }
Example #4
0
        /// <summary>
        /// Deserialises the specified value node into a .NET object
        /// </summary>
        /// <param name="t"></param>
        /// <param name="node"></param>
        /// <returns></returns>
        public object Deserialise(Type t, ValueNode node)
        {
            if (t.IsEnum && node is EnumValue)
            {
                var enumvalue = (EnumValue)node;

                Type enumtype;

                if (EnumTypes.TryGetValue(String.Join(".", enumvalue.Value, 0, enumvalue.Value.Length - 1), out enumtype))
                {
                    var result = Enum.Parse(enumtype, enumvalue.Value.Last());
                    if (result != null)
                        return result;
                }
                else
                    throw new InvalidOperationException($"No such TaronEnumAttribute with typename of ({String.Join(".", enumvalue.Value, 0, enumvalue.Value.Length - 1)})");
            }
            return null;
        }
Example #5
0
        /// <summary>
        /// Deserialises the specified value node into a .NET object
        /// </summary>
        /// <param name="t"></param>
        /// <param name="node"></param>
        /// <returns></returns>
        public object Deserialise(Type t, ValueNode node)
        {
            Type nt = node.GetType();
            Type[] genArgs = nt.GetGenericArguments();
            if (genArgs?.Length > 0 && typeof(PrimitiveValue<>).MakeGenericType(genArgs) == nt)
            {
                return nt.GetProperty("Value").GetValue(node, null);
            }
            else if (node is EnumValue)
            {
                EnumValue enumvalue = (EnumValue)node;

                Type enumtype;

                if (EnumTranslator.EnumTypes.TryGetValue(String.Join(".", enumvalue.Value, 0, enumvalue.Value.Length - 1), out enumtype))
                    return Translate.Deserialise(enumvalue, enumtype);

                return null;
            }
            else
            {
                return null;
            }
        }
Example #6
0
 /// <summary>
 /// Populates the a .NET object with the specified value node
 /// </summary>
 /// <param name="obj"></param>
 /// <param name="node"></param>
 public void Deserialise(object obj, ValueNode node)
 {
     // We can't populate a value type, this won't work
     throw new InvalidOperationException("Can't populate value type");
 }
Example #7
0
 private static object ToBasicObject(ValueNode node)
 {
     Type t = node.GetType();
     Type[] genArgs = t.GetGenericArguments();
     if (genArgs?.Length > 0 && typeof(PrimitiveValue<>).MakeGenericType(genArgs) == t)
     {
         return t.GetProperty("Value").GetValue(node, null);
     }
     else
     {
         return null;
     }
 }
Example #8
0
        /// <summary>
        /// Populates the a .NET object with the specified value node
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="node"></param>
        public void Deserialise(object obj, ValueNode node)
        {
            // Sanity check
            if (obj == null) return;

            Type t = obj.GetType();

            // Is it a map type?
            if (typeof(IDictionary).IsAssignableFrom(t) && node is MapValue)
            {
                IDictionary dict = obj as IDictionary;
                MapValue mapVal = node.As<MapValue>();

                // Is it a generic dict?
                if (TranslateOptions.IsGenericType(typeof(IDictionary<,>), t))
                {
                    Type[] genArgs = t.GetGenericArguments();
                    if (genArgs[0] != typeof(string)) return; // TODO: Throw error?
                    foreach (var pair in mapVal)
                    {
                        object val;
                        if (translateOpts.Deserialise(genArgs[1], pair.Value, out val))
                            dict[pair.Key] = val;
                    }
                }
                else
                {
                    foreach (var pair in mapVal)
                    {
                        object val;
                        if (translateOpts.Deserialise(null, pair.Value, out val))
                            dict.Add(pair.Key, val);
                    }
                }
            }

            // Is it an array type?
            else if (t.IsArray)
            {
                Array arr = obj as Array;
                ArrayValue arrVal = node.As<ArrayValue>();
                Type elementType = t.GetElementType();
                for (int i = 0; i < Math.Min(arrVal.Count, arr.Length); i++)
                {
                    object val;
                    translateOpts.Deserialise(elementType == typeof(object) ? null : elementType, arrVal[i], out val);
                    arr.SetValue(val, i);
                }
            }
            else if (typeof(IList).IsAssignableFrom(t))
            {
                var list = obj as IList;
                ArrayValue arrVal = node.As<ArrayValue>();
                list.Clear();

                // Is it a generic list?
                if (TranslateOptions.IsGenericType(typeof(IList<>), t))
                {
                    Type elementType = t.GetGenericArguments()[0];
                    for (int i = 0; i < arrVal.Count; i++)
                    {
                        object val;
                        translateOpts.Deserialise(elementType == typeof(object) ? null : elementType, arrVal[i], out val);
                        list.Add(val);
                    }
                }
                else
                {
                    for (int i = 0; i < arrVal.Count; i++)
                    {
                        object val;
                        translateOpts.Deserialise(null, arrVal[i], out val);
                        list.Add(val);
                    }
                }
            }
            else if (typeof(ICollection).IsAssignableFrom(t))
            {
                throw new NotImplementedException("Can't deserialise into an ICollection");
            }

            MapValue mapValue = node as MapValue;
            if (mapValue == null) return; // Throw error?

            foreach (var pair in mapValue)
            {
                MemberInfo member = t.GetMember(pair.Key, BindingFlags.Public | BindingFlags.Instance)
                    .Where(m => m is FieldInfo || m is PropertyInfo)
                    .SingleOrDefault();
                if (member != null)
                {
                    FieldInfo fieldInfo = member as FieldInfo;
                    if (fieldInfo != null)
                    {
                        object val;
                        translateOpts.Deserialise(fieldInfo.FieldType, pair.Value, out val);
                        if ((val == null && !fieldInfo.FieldType.IsValueType) || fieldInfo.FieldType.IsAssignableFrom(val.GetType()))
                            fieldInfo.SetValue(obj, val);
                    }
                    PropertyInfo propertyInfo = member as PropertyInfo;
                    if (propertyInfo != null)
                    {
                        object val;
                        translateOpts.Deserialise(propertyInfo.PropertyType, pair.Value, out val);
                        if ((val == null && !propertyInfo.PropertyType.IsValueType) || propertyInfo.PropertyType.IsAssignableFrom(val.GetType()))
                            propertyInfo.SetValue(obj, val, null);
                    }
                }
            }
        }
Example #9
0
 public static void Populate(object obj, ValueNode node, TranslateOptions tOpts = null)
 {
     if (tOpts == null) tOpts = TranslateOptions.Default;
     tOpts.Populate(obj, node);
 }