Esempio n. 1
0
        private void SerializeArrayRank(Array array, Type elementType, int[] coords, int r, IDataAdapter data)
        {
            if (r == coords.Length - 1)
            {
                if (settings.PrimitiveAsBase64 && elementType.IsPrimitive)
                {
                    int size  = ByteUtils.GetSizeOf(elementType);
                    int count = array.GetLength(r);
                    Action <byte[], int, object> writer = ByteUtils.GetBinaryWriter(elementType);

                    byte[] buffer = new byte[size * count];
                    int    index  = 0;
                    for (int i = 0; i < count; i++)
                    {
                        coords[r] = i;
                        writer(buffer, index, array.GetValue(coords));
                        index += size;
                    }
                    data.SetStringValue(Convert.ToBase64String(buffer));
                }
                else
                {
                    IDataArray a = data.AddArray();
                    for (int i = 0; i < array.GetLength(r); i++)
                    {
                        coords[r] = i;
                        SerializeSubValue(elementType, array.GetValue(coords), a.AddArrayValue(), null);
                    }
                }
            }
            else
            {
                IDataArray a = data.AddArray();
                for (int i = 0; i < array.GetLength(r); i++)
                {
                    coords[r] = i;
                    SerializeArrayRank(array, elementType, coords, r + 1, a.AddArrayValue());
                }
            }
        }
Esempio n. 2
0
        private void DeserializeArrayRank(Array array, Type elementType, int[] coords, int r, IDataAdapter data)
        {
            if (r == coords.Length - 1)
            {
                if (elementType.IsPrimitive)
                {
                    byte[] buffer = Convert.FromBase64String(data.GetStringValue());
                    int    size   = ByteUtils.GetSizeOf(elementType);
                    int    count  = array.GetLength(r);
                    Func <byte[], int, object> reader = ByteUtils.GetBinaryReader(elementType);

                    for (int i = 0; i < count; i++)
                    {
                        coords[r] = i;
                        array.SetValue(reader(buffer, i * size), coords);
                    }
                }
                else
                {
                    int index = 0;
                    foreach (IDataAdapter element in data.GetArray().GetChildren())
                    {
                        coords[r] = index++;
                        array.SetValue(DeserializeObject(elementType, element, null), coords);
                    }
                }
            }
            else
            {
                int index = 0;
                foreach (IDataAdapter element in data.GetArray().GetChildren())
                {
                    coords[r] = index++;
                    DeserializeArrayRank(array, elementType, coords, r + 1, element);
                }
            }
        }