Ejemplo n.º 1
0
        void SerializeArray(object instance, Type type, IKey inKey, ILogPrinter inLogger)
        {
            //if (string.IsNullOrEmpty(inKey.GetName()))
            //inKey.SetName("Values");

            Type declaredItemType = type.GetElementType();
            bool atomic_member    = declaredItemType.IsAtomic();

            Array multi_dim_array = instance as Array;

            CMultiArrayIndexer indexer = new CMultiArrayIndexer(multi_dim_array);

            while (indexer.MoveNext())
            {
                object value     = multi_dim_array.GetValue(indexer.Current);
                IKey   dim_child = GetOrCreateKeyByArrayIndex(inKey, indexer.Current, indexer.Lengthes, atomic_member);
                if (atomic_member)
                {
                    AddValueToKey(dim_child, value);
                }
                else
                {
                    IKey child = dim_child.CreateArrayKey();
                    Serialize(value, declaredItemType, child, 0, inLogger);
                }
            }
        }
Ejemplo n.º 2
0
        object DeserializeArray(IKey inKey, Type type, int inStructDeep, ILogPrinter inLogger)
        {
            if (inKey.IsEmpty)
            {
                return(null);
            }

            IKey key = inKey;

            Array multi_dim_array  = null;
            Type  declaredItemType = type.GetElementType();

            bool is_atomic_elems = declaredItemType.IsAtomic();
            bool is_array_elems  = declaredItemType.IsArray;

            int[] dims;
            if (is_array_elems)
            {
                dims = new int[] { key.GetChildCount() }
            }
            ;
            else
            {
                dims = FindArrayDimension(key, is_atomic_elems);
            }

            if (dims.Length == 0)
            {
                return(null);
            }

            object instance = Array.CreateInstance(declaredItemType, dims);

            multi_dim_array = instance as Array;

            CMultiArrayIndexer indexer = new CMultiArrayIndexer(multi_dim_array);

            while (indexer.MoveNext())
            {
                IKey dim_child = GetKeyByArrayIndex(key, indexer.Current);
                if (dim_child == null)
                {
                    LogError(inLogger, string.Format("Cant get value for multi array index {0}", indexer));
                }
                else
                {
                    object obj_value;
                    int    last_index = indexer.Current[indexer.Current.Length - 1];
                    if (is_atomic_elems)
                    {
                        string str_value;
                        if (dim_child.GetValuesCount() == 0)
                        {
                            str_value = string.Empty;
                        }
                        else
                        {
                            str_value = dim_child.GetValueAsString(last_index);
                        }
                        if (!ReflectionHelper.StringToAtomicValue(str_value, declaredItemType, out obj_value, _reflectionProvider, inLogger))
                        {
                            LogError(inLogger, string.Format("Key {0} [{3}] for collection with element type {1} can't convert value {2}",
                                                             key, declaredItemType.Name, str_value, key.GetPath()));
                        }
                    }
                    else
                    {
                        IKey child = dim_child.GetChild(last_index);
                        obj_value = DeserializeInternal(null, child, declaredItemType, 0, inStructDeep + 1, inLogger);
                    }

                    multi_dim_array.SetValue(obj_value, indexer.Current);
                }
            }

            return(instance);
        }