示例#1
0
        public static Array FillArray(
            Array arr,
            Type elementType,
            CBORObject cbor,
            CBORTypeMapper mapper,
            PODOptions options,
            int depth)
        {
            int rank = arr.Rank;

            if (rank == 0)
            {
                return(arr);
            }
            if (rank == 1)
            {
                int len = arr.GetLength(0);
                for (var i = 0; i < len; ++i)
                {
                    object item = cbor[i].ToObject(
                        elementType,
                        mapper,
                        options,
                        depth + 1);
                    arr.SetValue(
                        item,
                        i);
                }
                return(arr);
            }
            var index      = new int[rank];
            var dimensions = new int[rank];

            for (var i = 0; i < rank; ++i)
            {
                dimensions[i] = arr.GetLength(i);
            }
            if (!FirstElement(dimensions))
            {
                return(arr);
            }
            do
            {
                object item = GetCBORObject(
                    cbor,
                    index).ToObject(
                    elementType,
                    mapper,
                    options,
                    depth + 1);
                arr.SetValue(
                    item,
                    index);
            } while (NextElement(index, dimensions));
            return(arr);
        }
示例#2
0
        public static CBORObject FromArray(
            Object arrObj,
            PODOptions options,
            CBORTypeMapper mapper,
            int depth)
        {
            var arr  = (Array)arrObj;
            int rank = arr.Rank;

            if (rank == 0)
            {
                return(CBORObject.NewArray());
            }
            CBORObject obj = null;

            if (rank == 1)
            {
                // Most common case: the array is one-dimensional
                obj = CBORObject.NewArray();
                int len = arr.GetLength(0);
                for (var i = 0; i < len; ++i)
                {
                    obj.Add(
                        CBORObject.FromObject(
                            arr.GetValue(i),
                            options,
                            mapper,
                            depth + 1));
                }
                return(obj);
            }
            var index      = new int[rank];
            var dimensions = new int[rank];

            for (var i = 0; i < rank; ++i)
            {
                dimensions[i] = arr.GetLength(i);
            }
            if (!FirstElement(dimensions))
            {
                return(obj);
            }
            obj = BuildCBORArray(dimensions);
            do
            {
                CBORObject o = CBORObject.FromObject(
                    arr.GetValue(index),
                    options,
                    mapper,
                    depth + 1);
                SetCBORObject(obj, index, o);
            } while (NextElement(index, dimensions));
            return(obj);
        }
示例#3
0
 public static object ObjectWithProperties(
     Type t,
     IEnumerable <KeyValuePair <string, CBORObject> > keysValues,
     CBORTypeMapper mapper,
     PODOptions options,
     int depth)
 {
     try {
         object o    = Activator.CreateInstance(t);
         var    dict = new Dictionary <string, CBORObject>();
         foreach (var kv in keysValues)
         {
             var name = kv.Key;
             dict[name] = kv.Value;
         }
         foreach (PropertyData key in GetPropertyList(o.GetType()))
         {
             if (!key.HasUsableSetter() || !key.HasUsableGetter())
             {
                 // Require properties to have both a setter and
                 // a getter to be eligible for setting
                 continue;
             }
             var name = key.GetAdjustedName(options != null ?
                                            options.UseCamelCase : true);
             if (dict.ContainsKey(name))
             {
                 object dobj = dict[name].ToObject(
                     key.PropertyType,
                     mapper,
                     options,
                     depth + 1);
                 key.SetValue(
                     o,
                     dobj);
             }
         }
         return(o);
     } catch (Exception ex) {
         throw new CBORException(ex.Message, ex);
     }
 }
示例#4
0
 /// <summary>
 /// <para>Converts this CBOR object to an object of an arbitrary type.
 /// See
 /// <see cref='PeterO.Cbor.CBORObject.ToObject(System.Type)'/> for
 /// further information.</para></summary>
 /// <param name='mapper'>This parameter controls which data types are
 /// eligible for Plain-Old-Data deserialization and includes custom
 /// converters from CBOR objects to certain data types.</param>
 /// <param name='options'>Specifies options for controlling
 /// deserialization of CBOR objects.</param>
 /// <typeparam name='T'>The type, class, or interface that this
 /// method's return value will belong to. <b>Note:</b> For security
 /// reasons, an application should not base this parameter on user
 /// input or other externally supplied data. Whenever possible, this
 /// parameter should be either a type specially handled by this method
 /// (such as <c>int</c> or <c>String</c> ) or a plain-old-data type
 /// (POCO or POJO type) within the control of the application. If the
 /// plain-old-data type references other data types, those types should
 /// likewise meet either criterion above.</typeparam>
 /// <returns>The converted object.</returns>
 /// <exception cref='NotSupportedException'>The given type "T", or this
 /// object's CBOR type, is not supported.</exception>
 public T ToObject <T>(CBORTypeMapper mapper, PODOptions options)
 {
     return((T)this.ToObject(typeof(T), mapper, options));
 }
示例#5
0
 /// <summary>
 /// <para>Converts this CBOR object to an object of an arbitrary type.
 /// See
 /// <see cref='PeterO.Cbor.CBORObject.ToObject(System.Type)'/> for
 /// further information.</para></summary>
 /// <param name='mapper'>This parameter controls which data types are
 /// eligible for Plain-Old-Data deserialization and includes custom
 /// converters from CBOR objects to certain data types.</param>
 /// <typeparam name='T'>The type, class, or interface that this
 /// method's return value will belong to. <b>Note:</b> For security
 /// reasons, an application should not base this parameter on user
 /// input or other externally supplied data. Whenever possible, this
 /// parameter should be either a type specially handled by this method
 /// (such as <c>int</c> or <c>String</c> ) or a plain-old-data type
 /// (POCO or POJO type) within the control of the application. If the
 /// plain-old-data type references other data types, those types should
 /// likewise meet either criterion above.</typeparam>
 /// <returns>The converted object.</returns>
 /// <exception cref='NotSupportedException'>The given type "T", or this
 /// object's CBOR type, is not supported.</exception>
 public T ToObject <T>(CBORTypeMapper mapper)
 {
     return((T)this.ToObject(typeof(T), mapper));
 }
示例#6
0
        public static object TypeToObject(
            CBORObject objThis,
            Type t,
            CBORTypeMapper mapper,
            PODOptions options,
            int depth)
        {
            if (t.Equals(typeof(int)))
            {
                return(objThis.AsInt32());
            }
            if (t.Equals(typeof(short)))
            {
                return(objThis.AsNumber().ToInt16Checked());
            }
            if (t.Equals(typeof(ushort)))
            {
                return(objThis.AsUInt16Legacy());
            }
            if (t.Equals(typeof(byte)))
            {
                return(objThis.AsByteLegacy());
            }
            if (t.Equals(typeof(sbyte)))
            {
                return(objThis.AsSByteLegacy());
            }
            if (t.Equals(typeof(long)))
            {
                return(objThis.AsNumber().ToInt64Checked());
            }
            if (t.Equals(typeof(uint)))
            {
                return(objThis.AsUInt32Legacy());
            }
            if (t.Equals(typeof(ulong)))
            {
                return(objThis.AsUInt64Legacy());
            }
            if (t.Equals(typeof(double)))
            {
                return(objThis.AsDouble());
            }
            if (t.Equals(typeof(decimal)))
            {
                return(objThis.AsDecimal());
            }
            if (t.Equals(typeof(float)))
            {
                return(objThis.AsSingle());
            }
            if (t.Equals(typeof(bool)))
            {
                return(objThis.AsBoolean());
            }
            if (t.Equals(typeof(char)))
            {
                if (objThis.Type == CBORType.TextString)
                {
                    string s = objThis.AsString();
                    if (s.Length != 1)
                    {
                        throw new CBORException("Can't convert to char");
                    }
                    return(s[0]);
                }
                if (objThis.IsNumber && objThis.AsNumber().CanFitInInt32())
                {
                    int c = objThis.AsNumber().ToInt32IfExact();
                    if (c < 0 || c >= 0x10000)
                    {
                        throw new CBORException("Can't convert to char");
                    }
                    return((char)c);
                }
                throw new CBORException("Can't convert to char");
            }
            if (t.Equals(typeof(DateTime)))
            {
                return(new CBORDateConverter().FromCBORObject(objThis));
            }
            if (t.Equals(typeof(Guid)))
            {
                return(new CBORUuidConverter().FromCBORObject(objThis));
            }
            if (t.Equals(typeof(Uri)))
            {
                return(new CBORUriConverter().FromCBORObject(objThis));
            }
            if (IsAssignableFrom(typeof(Enum), t))
            {
                return(ObjectToEnum(objThis, t));
            }
            if (IsGenericType(t))
            {
                Type td = t.GetGenericTypeDefinition();
                // Nullable types
                if (td.Equals(typeof(Nullable <>)))
                {
                    Type nullableType = Nullable.GetUnderlyingType(t);
                    if (objThis.IsNull)
                    {
                        return(Activator.CreateInstance(t));
                    }
                    else
                    {
                        object wrappedObj = objThis.ToObject(
                            nullableType,
                            mapper,
                            options,
                            depth + 1);
                        return(Activator.CreateInstance(
                                   t,
                                   wrappedObj));
                    }
                }
            }
            if (objThis.Type == CBORType.ByteString)
            {
                if (t.Equals(typeof(byte[])))
                {
                    byte[] bytes   = objThis.GetByteString();
                    var    byteret = new byte[bytes.Length];
                    Array.Copy(bytes, 0, byteret, 0, byteret.Length);
                    return(byteret);
                }
            }
            if (objThis.Type == CBORType.Array)
            {
                Type   objectType = typeof(object);
                var    isList     = false;
                object listObject = null;
        #if NET40 || NET20
                if (IsAssignableFrom(typeof(Array), t))
                {
                    Type  elementType = t.GetElementType();
                    Array array       = Array.CreateInstance(
                        elementType,
                        GetDimensions(objThis));
                    return(FillArray(
                               array,
                               elementType,
                               objThis,
                               mapper,
                               options,
                               depth));
                }
                if (t.IsGenericType)
                {
                    Type td = t.GetGenericTypeDefinition();
                    isList = td.Equals(typeof(List <>)) || td.Equals(typeof(IList <>)) ||
                             td.Equals(typeof(ICollection <>)) ||
                             td.Equals(typeof(IEnumerable <>));
                }
                isList = isList && t.GetGenericArguments().Length == 1;
                if (isList)
                {
                    objectType = t.GetGenericArguments()[0];
                    Type listType = typeof(List <>).MakeGenericType(objectType);
                    listObject = Activator.CreateInstance(listType);
                }
        #else
                if (IsAssignableFrom(typeof(Array), t))
                {
                    Type  elementType = t.GetElementType();
                    Array array       = Array.CreateInstance(
                        elementType,
                        GetDimensions(objThis));
                    return(FillArray(
                               array,
                               elementType,
                               objThis,
                               mapper,
                               options,
                               depth));
                }
                if (t.GetTypeInfo().IsGenericType)
                {
                    Type td = t.GetGenericTypeDefinition();
                    isList = td.Equals(typeof(List <>)) || td.Equals(typeof(IList <>)) ||
                             td.Equals(typeof(ICollection <>)) ||

                             td.Equals(typeof(IEnumerable <>));
                }
                isList = isList && t.GenericTypeArguments.Length == 1;
                if (isList)
                {
                    objectType = t.GenericTypeArguments[0];
                    Type listType = typeof(List <>).MakeGenericType(objectType);
                    listObject = Activator.CreateInstance(listType);
                }
        #endif
                if (listObject == null)
                {
                    if (t.Equals(typeof(IList)) ||
                        t.Equals(typeof(ICollection)) || t.Equals(typeof(IEnumerable)))
                    {
                        listObject = new List <object>();
                        objectType = typeof(object);
                    }
                }
                if (listObject != null)
                {
                    System.Collections.IList ie = (System.Collections.IList)listObject;
                    foreach (CBORObject value in objThis.Values)
                    {
                        ie.Add(value.ToObject(objectType, mapper, options, depth + 1));
                    }
                    return(listObject);
                }
            }
            if (objThis.Type == CBORType.Map)
            {
                var    isDict     = false;
                Type   keyType    = null;
                Type   valueType  = null;
                object dictObject = null;
        #if NET40 || NET20
                isDict = t.IsGenericType;
                if (t.IsGenericType)
                {
                    Type td = t.GetGenericTypeDefinition();
                    isDict = td.Equals(typeof(Dictionary <,>)) ||
                             td.Equals(typeof(IDictionary <,>));
                }
                // DebugUtility.Log("list=" + isDict);
                isDict = isDict && t.GetGenericArguments().Length == 2;
                // DebugUtility.Log("list=" + isDict);
                if (isDict)
                {
                    keyType   = t.GetGenericArguments()[0];
                    valueType = t.GetGenericArguments()[1];
                    Type listType = typeof(Dictionary <,>).MakeGenericType(
                        keyType,
                        valueType);
                    dictObject = Activator.CreateInstance(listType);
                }
        #else
                isDict = t.GetTypeInfo().IsGenericType;
                if (t.GetTypeInfo().IsGenericType)
                {
                    Type td = t.GetGenericTypeDefinition();
                    isDict = td.Equals(typeof(Dictionary <,>)) ||
                             td.Equals(typeof(IDictionary <,>));
                }
                // DebugUtility.Log("list=" + isDict);
                isDict = isDict && t.GenericTypeArguments.Length == 2;
                // DebugUtility.Log("list=" + isDict);
                if (isDict)
                {
                    keyType   = t.GenericTypeArguments[0];
                    valueType = t.GenericTypeArguments[1];
                    Type listType = typeof(Dictionary <,>).MakeGenericType(
                        keyType,
                        valueType);
                    dictObject = Activator.CreateInstance(listType);
                }
        #endif
                if (dictObject == null)
                {
                    if (t.Equals(typeof(IDictionary)))
                    {
                        dictObject = new Dictionary <object, object>();
                        keyType    = typeof(object);
                        valueType  = typeof(object);
                    }
                }
                if (dictObject != null)
                {
                    System.Collections.IDictionary idic =
                        (System.Collections.IDictionary)dictObject;
                    foreach (CBORObject key in objThis.Keys)
                    {
                        CBORObject value = objThis[key];
                        idic.Add(
                            key.ToObject(keyType, mapper, options, depth + 1),
                            value.ToObject(valueType, mapper, options, depth + 1));
                    }
                    return(dictObject);
                }
                if (mapper != null)
                {
                    if (!mapper.FilterTypeName(t.FullName))
                    {
                        throw new CBORException("Type " + t.FullName +
                                                " not supported");
                    }
                }
                else
                {
                    if (t.FullName != null && (
                            StartsWith(t.FullName, "Microsoft.Win32.") ||
                            StartsWith(t.FullName, "System.IO.")))
                    {
                        throw new CBORException("Type " + t.FullName +
                                                " not supported");
                    }
                    if (StartsWith(t.FullName, "System.") &&
                        !HasCustomAttribute(t, "System.SerializableAttribute"))
                    {
                        throw new CBORException("Type " + t.FullName +
                                                " not supported");
                    }
                }
                var values    = new List <KeyValuePair <string, CBORObject> >();
                var propNames = PropertyMap.GetPropertyNames(
                    t,
                    options != null ? options.UseCamelCase : true);
                foreach (string key in propNames)
                {
                    if (objThis.ContainsKey(key))
                    {
                        CBORObject cborValue = objThis[key];
                        var        dict      = new KeyValuePair <string, CBORObject>(
                            key,
                            cborValue);
                        values.Add(dict);
                    }
                }
                return(PropertyMap.ObjectWithProperties(
                           t,
                           values,
                           mapper,
                           options,
                           depth));
            }
            else
            {
                throw new CBORException();
            }
        }