/// <summary> /// Decode a serialized value /// </summary> object Decode(ProcedureSignature procedure, int i, Type type, ByteString value) { if (TypeUtils.IsAClassType(type)) { return(ObjectStore.Instance.GetInstance((ulong)ProtocolBuffers.ReadValue(value, typeof(ulong)))); } else if (TypeUtils.IsACollectionType(type)) { return(DecodeCollection(procedure, i, type, value)); } else if (ProtocolBuffers.IsAMessageType(type)) { return(ProtocolBuffers.ParseFrom(type, value)); } else if (TypeUtils.IsAnEnumType(type)) { // TODO: Assumes it's underlying type is int var enumValue = ProtocolBuffers.ReadValue(value, typeof(int)); if (!Enum.IsDefined(type, enumValue)) { throw new RPCException(procedure, "Failed to convert value " + enumValue + " to enumeration type " + type); } return(Enum.ToObject(type, enumValue)); } else { return(ProtocolBuffers.ReadValue(value, type)); } }
/// <summary> /// Encode a value /// </summary> ByteString Encode(Type type, object value) { if (TypeUtils.IsAClassType(type)) { return(ProtocolBuffers.WriteValue(ObjectStore.Instance.AddInstance(value), typeof(ulong))); } else if (TypeUtils.IsACollectionType(type)) { return(EncodeCollection(type, value)); } else if (ProtocolBuffers.IsAMessageType(type)) { return(ProtocolBuffers.WriteMessage(value as IMessage)); } else if (TypeUtils.IsAnEnumType(type)) { // TODO: Assumes it's underlying type is int return(ProtocolBuffers.WriteValue((int)value, typeof(int))); } else { return(ProtocolBuffers.WriteValue(value, type)); } }