private static void ScanArrayRanks(IDataAdapter data, Type elementType, int[] lengths, int index) { if (index == lengths.Length - 1 && elementType.IsPrimitive) { string value = data.GetStringValue(); lengths[index] = ByteUtils.GetBytesInBase64(value) / ByteUtils.GetSizeOf(elementType); return; } int count = 0; IDataAdapter firstChild = null; foreach (IDataAdapter e in data.GetArray().GetChildren()) { if (firstChild == null) { firstChild = e; } count++; } lengths[index] = count; if (index < lengths.Length - 1) { ScanArrayRanks(firstChild, elementType, lengths, index + 1); } }
private void SerializeContainer(IEnumerable e, Type elementType, IDataAdapter data) { if (settings.PrimitiveAsBase64 && elementType.IsPrimitive) { int size = ByteUtils.GetSizeOf(elementType); int count = 0; // ReSharper disable once UnusedVariable foreach (object o in e) { count++; } Action <byte[], int, object> writer = ByteUtils.GetBinaryWriter(elementType); byte[] buffer = new byte[size * count]; int index = 0; foreach (object o in e) { writer(buffer, index, o); index += size; } data.SetStringValue(Convert.ToBase64String(buffer)); } else { IDataArray array = data.AddArray(); foreach (object o in e) { SerializeSubValue(elementType, o, array.AddArrayValue(), null); } } haveContainers = true; }
private void DeserializeContainer(ref object container, Type type, Type elementType, IDataAdapter data, string addMethodName, bool reverse = false) { if (container == null) { container = TypeCache.CreateParameterless(type); } Action <object, object> addAction = null; if (!TypeCache.TryGetTypeAccessor(type, ref addAction)) { addAction = InvocationHelper.CreateSetDelegate(type, elementType, addMethodName); TypeCache.AddTypeAccessor(type, addAction); } if (elementType.IsPrimitive) { byte[] buffer = Convert.FromBase64String(data.GetStringValue()); int size = ByteUtils.GetSizeOf(elementType); int count = buffer.Length / size; Func <byte[], int, object> reader = ByteUtils.GetBinaryReader(elementType); if (reverse) { for (int i = count - 1; i >= 0; i--) { addAction(container, reader(buffer, size * i)); } } else { for (int i = 0; i < count; i++) { addAction(container, reader(buffer, size * i)); } } return; } IDataArray array = data.GetArray(); if (reverse) { foreach (IDataAdapter element in array.GetChildrenReversed()) { addAction(container, DeserializeObject(elementType, element, null)); } } else { foreach (IDataAdapter element in array.GetChildren()) { addAction(container, DeserializeObject(elementType, element, null)); } } }
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()); } } }
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); } } }