public Delegate GenerateDecoder(Type type, Func <Type, Delegate> recurse) { return(new Func <DecodeBuffer, UInt16>(input => { return (UInt16)UnsignedVlq.Decode(input); })); }
public Delegate GenerateDecoder(Type type, Func <Type, Delegate> recurse) { // Get deserializer for sub items var valueDecoder = recurse(type.GenericTypeArguments[0]); return(new Func <DecodeBuffer, IList>(input => { // Read header var header = UnsignedVlq.Decode(input); // Handle nulls if (header == 0) { return null; } // Determine length var count = (Int32)header - 1; // Instantiate list var output = (IList)Activator.CreateInstance(type); //typeof(List<>).MakeGenericType(type.GenericTypeArguments) // Deserialize until we reach length limit for (var i = 0; i < count; i++) { // Deserialize element var element = valueDecoder.DynamicInvokeTransparent(input); // Add to output output.Add(element); } return output; })); }
public override Object Decode(DecodeBuffer input) { var v = UnsignedVlq.Decode(input); if (IsNullable) { if (v == 0) { return(null); } v--; } v *= Increment; v += Minimum; return(Convert.ChangeType(v, UnderlyingType)); }
public override Object Decode(DecodeBuffer input) { // Decode header var header = UnsignedVlq.Decode(input); // Handle nulls if (IsNullable) { if (header == 0) { return(null); } header--; } // Decode value return(Encoding.UTF8.GetString(input.Underlying, input.GetIncrementOffset((Int32)header), (Int32)header)); }
public Delegate GenerateDecoder(Type type, Func <Type, Delegate> recurse) { return(new Func <DecodeBuffer, String>(input => { // Decode header var header = UnsignedVlq.Decode(input); // Handle nulls if (header == 0) { return null; } // Determine length var length = (Int32)header - 1; // Decode value return Encoding.UTF8.GetString(input.Underlying, input.GetIncrementOffset(length), length); })); }
public override Object Decode(DecodeBuffer input) { #if DEBUG if (null == Records) { throw new InvalidOperationException("Coder has not yet been prepared."); } #endif // Read the length header var header = (Int32)UnsignedVlq.Decode(input); // Handle nulls if (IsNullable) { if (header == 0) { return(null); } header--; } // Instantiate output var output = Activator.CreateInstance(UnderlyingType); // Extract an inner buffer so that if fields are added to the class in the future we ignore them, being backwards compatible var innerInput = input.Extract(header); // Isolate bytes for body foreach (var record in Records) { // Deserialize value var v = record.Coder.Decode(innerInput); // Set it on property record.Field.SetValue(output, v); } return(output); }
public Delegate GenerateDecoder(Type type, Func <Type, Delegate> recurse) { // Get deserializer for sub items var keyDecoder = recurse(type.GenericTypeArguments[0]); var valueDecoder = recurse(type.GenericTypeArguments[1]); return(new Func <DecodeBuffer, IDictionary>(input => { // Read header var header = UnsignedVlq.Decode(input); if (header == 0) { return null; } // Get count var count = (Int32)header - 1; // Instantiate dictionary var output = (IDictionary)Activator.CreateInstance(type); // Loop through input buffer until depleted for (var i = 0; i < count; i++) { // Deserialize key var keyValue = keyDecoder.DynamicInvokeTransparent(input); // Deserialize value var valueValue = valueDecoder.DynamicInvokeTransparent(input); // Add to output output[keyValue] = valueValue; } return output; })); }
public Delegate GenerateDecoder(Type type, Func <Type, Delegate> recurse) { // Get deserializer for sub items var valueDecoder = recurse(type.GetElementType()); return(new Func <DecodeBuffer, Array>(input => { var header = UnsignedVlq.Decode(input); if (header == 0) { return null; } // Determine length var length = (Int32)header - 1; // Instantiate list var container = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(type.GetElementType())); // Deserialize until we reach length limit for (var i = 0; i < length; i++) { // Deserialize element var element = valueDecoder.DynamicInvokeTransparent(input); // Add to output container.Add(element); } // Convert to array and return var output = Array.CreateInstance(type.GetElementType(), container.Count); container.CopyTo(output, 0); return output; })); }
public Delegate GenerateDecoder(Type type, Func <Type, Delegate> recurse) { var records = new Record[Byte.MaxValue]; // Find all properties decorated with LightWeightProperty attribute var maxIndex = -1; foreach (var property in type.GetRuntimeFields()) { // Get property attribute which tells us the properties' index var attribute = (LightWeightPropertyAttribute)property.GetCustomAttribute(typeof(LightWeightPropertyAttribute), false); if (null == attribute) { // No attribute found, skip continue; } // Check for duplicate index if (null != records[attribute.Index]) { throw new DuplicateIndexException($"The index {attribute.Index} is already used and cannot be reused."); } // Note the max index used if (attribute.Index > maxIndex) { maxIndex = attribute.Index; } // Find/create encoder var decoder = recurse(property.FieldType); // Find floor, if any var transformations = property.GetCustomAttributes <TransformationAttribute>(true).ToArray(); // Store property in lookup records[attribute.Index] = new Record() { Field = property, Coder = decoder, Transformations = transformations }; } // Check that no indexes have been missed for (var i = 0; i < maxIndex; i++) { if (null == records[i]) { throw new MissingIndexException($"Indexes must not be skipped, however missing index {i}."); } } return(new Func <DecodeBuffer, Object>(input => { // Read the length header var header = (Int32)UnsignedVlq.Decode(input); // Handle nulls if (header == 0) { return null; } // Determine length var length = header - 1; // Instantiate output var output = Activator.CreateInstance(type); // Extract an inner buffer so that if fields are added to the class in the future we ignore them, being backwards compatible var innerInput = input.Extract(length); // Isolate bytes for body for (var i = 0; i <= maxIndex; i++) { var record = records[i]; // Deserialize value var v = record.Coder.DynamicInvokeTransparent(innerInput); // Apply transformations foreach (var transformation in record.Transformations) { v = transformation.RemoveEffect(v); } // Set it on property record.Field.SetValue(output, v); } return output; })); }