public async ValueTask Read(Deserializable deserializable, CancellationToken cancellationToken) { if (byteStream.CanRead) { await deserializable.Read(primitiveReader, cancellationToken); } }
static void Main(string[] args) { Investor_acc investor = new Investor_acc(); Operation op; Console.WriteLine("Press 1 to read txt file"); Console.WriteLine("Press 2 to deserialize dat file"); Console.WriteLine("Press 3 to deserialize json file"); Console.WriteLine("Press 4 to deserialize xml file"); op = (Operation)Enum.Parse(typeof(Operation), Console.ReadLine()); switch (op) { case Operation.get_txt: using (StreamReader fileIn = new StreamReader("input.txt", Encoding.GetEncoding(1251))) { string[] str = fileIn.ReadToEnd().Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); foreach (string str_elem in str) { string[] sec = str_elem.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (sec.Length == 3) { investor.Add(new Stock(sec[0], double.Parse(sec[1]), bool.Parse(sec[2]))); } else if (sec.Length == 4) { investor.Add(new Bond(sec[0], double.Parse(sec[1]), double.Parse(sec[2]), bool.Parse(sec[3]))); } } } Serializable.Serializable_Binary(investor.GetSecurities); Serializable.Serializable_XML(investor.GetSecurities); Serializable.Serializable_Json(investor.GetSecurities); Console.WriteLine(); break; case Operation.d_dat: Deserializable.Binary(investor); break; case Operation.d_json: Deserializable.Json(investor); break; case Operation.d_xml: Deserializable.XML(investor); break; } foreach (Security_paper elem in investor.GetSecurities) { Console.WriteLine(elem); } }
public virtual MessageResult Deserialize(string message, DateTime messageTimestamp = default) { try { Deserializable msg = JsonConvert.DeserializeObject <Deserializable>(message, this.serializerSettings); Meta <Lazy <IMessageDeserializer> > deserializer = this.deserializers.FirstOrDefault(x => x.Metadata[nameof(IMessageMetadata.Type)] as string == msg.Type && (x.Metadata[nameof(IMessageMetadata.VersionRange)] as Range).IsSatisfied(msg.Version)); if (deserializer != null) { return(new MessageResult((deserializer.Value.Value as IMessageDeserializer <BaseMessage>) .DeserializeMessagePayload( msg.Payload.Value as string, this.serializerSettings, msg.CorrelationId, msg.UserId, msg.RequestTimestamp ?? messageTimestamp, msg.EventTimestamp ?? messageTimestamp))); } return(new MessageResult($"Deserializer for message type {msg.Type} and version {msg.Version} is not found.")); } catch (Exception ex) { return(new MessageResult(ex)); } }
public void Handle(ulong remoteMessageId, IPeer peer, IReadStream readStream) { if (peer == null) { Logger.Error($"{nameof(peer)} 为 null"); return; } var method = RpcResponseManager.GetRpcMethod(MethodId); if (method == null) { Logger.Warn($"{MethodId}未注册!"); return; } var length = readStream.ShiftRight <byte>(); Logger.Debug($"MethodId:{MethodId} 参数个数:{length}"); var deserializable = new Deserializable(readStream); var values = deserializable.Deserialize(new object[length + 2], length); values[values.Length - 2] = peer; Action temp = () => { }; Action <Action> action = callback => temp += callback; values[values.Length - 1] = action; ResponseCallback responseCallback = (rm, sd) => { temp?.Invoke(); }; object result = null; var lateBoundMethod = RpcResponseManager.GetRpcMethod(MethodId); var exception = ""; if (lateBoundMethod == null) { exception = $"没有方法:{MethodId}"; Logger.Warn(exception); } else { var stopwatch = Stopwatch.StartNew(); try { result = lateBoundMethod.Invoke(values); } catch (Exception e) { Logger.Error($"方法:[{MethodId}] " + e); exception = $"方法:[{MethodId}] 发生异常!"; } finally { stopwatch.Stop(); } Logger.Debug($"当前[{MethodId}]方法执行时间:{stopwatch.ElapsedMilliseconds}毫秒"); } var rpcResponse = new RpcResponse(exception, MethodId, remoteMessageId); using (var serializable = new Serializable(null)) { serializable.SerializableObject(result); RpcProxy.Invoke(MessageType.RpcResponse, rpcResponse, serializable.WriteStream, peer, responseCallback); } }