/// <summary> /// Read value from <see cref="System.Protobuf.Object"/> /// </summary> /// <param name="obj"><see cref="System.Protobuf.Object"/> to read from</param> /// <returns>Value in the correct type - null if not capable of converting</returns> public static object ToCLR(this System.Protobuf.Object obj) { var type = (Types)obj.Type; object value = null; using (var stream = new CodedInputStream(obj.Content.ToByteArray())) { switch (type) { case Types.String: value = stream.ReadString(); break; case Types.Int32: value = stream.ReadInt32(); break; case Types.Int64: value = stream.ReadInt64(); break; case Types.UInt32: value = stream.ReadUInt32(); break; case Types.UInt64: value = stream.ReadUInt64(); break; case Types.Float: value = stream.ReadFloat(); break; case Types.Double: value = stream.ReadDouble(); break; case Types.Boolean: value = stream.ReadBool(); break; case Types.DateTime: value = stream.ReadInt64().ToDateTime(); break; case Types.DateTimeOffset: value = stream.ReadInt64().ToDateTimeOffset(); break; case Types.Guid: value = new Guid(stream.ReadBytes().ToByteArray()); break; } } return(value); }
object ReadValue(CodedInputStream inputStream, object value, Type type, Type targetType, IValueConverter converter) { if (type == typeof(Guid)) { _ = inputStream.ReadLength(); var guidAsBytes = inputStream.ReadBytes(); value = new Guid(guidAsBytes.ToByteArray()); } else if (type == typeof(string)) { _ = inputStream.ReadLength(); value = inputStream.ReadString(); } else if (type == typeof(int)) { value = inputStream.ReadInt32(); } else if (type == typeof(long)) { value = inputStream.ReadInt64(); } else if (type == typeof(uint)) { value = inputStream.ReadUInt32(); } else if (type == typeof(ulong)) { value = inputStream.ReadUInt64(); } else if (type == typeof(float)) { value = inputStream.ReadFloat(); } else if (type == typeof(double)) { value = inputStream.ReadDouble(); } else if (type == typeof(bool)) { value = inputStream.ReadBool(); } else if (type == typeof(DateTimeOffset) || type == typeof(DateTime)) { value = DateTimeOffset.FromUnixTimeMilliseconds(inputStream.ReadInt64()); if (type == typeof(DateTime)) { value = ((DateTimeOffset)value).UtcDateTime; } } if (converter != null) { value = converter.ConvertFrom(targetType, value); } return(value); }
public float ParseRawFloat() { const int encodedSize = sizeof(float); CodedInputStream cis = new CodedInputStream(floatInputBuffer); float sum = 0; for (int i = 0; i < BytesToParse / encodedSize; i++) { sum += cis.ReadFloat(); } return(sum); }
public bool ReadFloat(out float value) { try { value = _stream.ReadFloat(); } catch (Exception) { value = 0; return(false); } return(true); }
public Dictionary <Type, object> ReadParam() { Dictionary <Type, object> pairs = new Dictionary <Type, object>(); foreach (var item in Params) { object o = null; Type t = Type.GetType(item.Name); if (t != null) { CodedInputStream cis = new CodedInputStream(item.Data.ToByteArray()); if (t == typeof(int)) { o = cis.ReadInt32(); } else if (t == typeof(long)) { o = cis.ReadInt64(); } else if (t == typeof(float)) { o = cis.ReadFloat(); } else if (t == typeof(double)) { o = cis.ReadDouble(); } else if (t == typeof(bool)) { o = cis.ReadBool(); } else if (t == typeof(string)) { o = cis.ReadString(); } else if (typeof(IMessage).IsAssignableFrom(t)) { o = Activator.CreateInstance(t); cis.ReadMessage((IMessage)o); } pairs.Add(t, o); } } return(pairs); }
/// <summary> /// Convert a Protocol Buffer value type, encoded as a byte string, to a C# value. /// </summary> public static object ReadValue(ByteString value, Type type) { if (value.Length == 0) { throw new ArgumentException("Value is empty"); } var stream = new CodedInputStream(value.ToByteArray()); if (type == typeof(double)) { return(stream.ReadDouble()); } else if (type == typeof(float)) { return(stream.ReadFloat()); } else if (type == typeof(int)) { return(stream.ReadInt32()); } else if (type == typeof(long)) { return(stream.ReadInt64()); } else if (type == typeof(uint)) { return(stream.ReadUInt32()); } else if (type == typeof(ulong)) { return(stream.ReadUInt64()); } else if (type == typeof(bool)) { return(stream.ReadBool()); } else if (type == typeof(string)) { return(stream.ReadString()); } else if (type == typeof(byte[])) { return(stream.ReadBytes().ToByteArray()); } throw new ArgumentException(type + " is not a Protocol Buffer value type"); }
/// <summary> /// Decode a value of the given type. /// Should not be called directly. This interface is used by service client stubs. /// </summary> public static object Decode(ByteString value, Type type, Connection client) { var stream = new CodedInputStream(value.ToByteArray()); if (type == typeof(double)) { return(stream.ReadDouble()); } else if (type == typeof(float)) { return(stream.ReadFloat()); } else if (type == typeof(int)) { return(stream.ReadInt32()); } else if (type == typeof(long)) { return(stream.ReadInt64()); } else if (type == typeof(uint)) { return(stream.ReadUInt32()); } else if (type == typeof(ulong)) { return(stream.ReadUInt64()); } else if (type == typeof(bool)) { return(stream.ReadBool()); } else if (type == typeof(string)) { return(stream.ReadString()); } else if (type == typeof(byte[])) { return(stream.ReadBytes().ToByteArray()); } else if (type.IsEnum) { return(stream.ReadInt32()); } else if (typeof(RemoteObject).IsAssignableFrom(type)) { if (client == null) { throw new ArgumentException("Client not passed when decoding remote object"); } var id = stream.ReadUInt64(); if (id == 0) { return(null); } return((RemoteObject)Activator.CreateInstance(type, client, id)); } else if (typeof(IMessage).IsAssignableFrom(type)) { IMessage message = (IMessage)Activator.CreateInstance(type); message.MergeFrom(stream); return(message); } else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IList <>)) { return(DecodeList(value, type, client)); } else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IDictionary <,>)) { return(DecodeDictionary(value, type, client)); } else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(ISet <>)) { return(DecodeSet(value, type, client)); } else if (type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(Tuple <>) || type.GetGenericTypeDefinition() == typeof(Tuple <,>) || type.GetGenericTypeDefinition() == typeof(Tuple <, ,>) || type.GetGenericTypeDefinition() == typeof(Tuple <, , ,>) || type.GetGenericTypeDefinition() == typeof(Tuple <, , , ,>) || type.GetGenericTypeDefinition() == typeof(Tuple <, , , , ,>))) { return(DecodeTuple(value, type, client)); // TODO: ugly handing of tuple types } throw new ArgumentException(type + " is not a serializable type"); }
public override float Read(CodedInputStream stream) { return(stream.ReadFloat()); }