/// <summary> /// Deserializes the JSON to a .NET object. /// </summary> /// <param name="serializer">The serializer</param> /// <typeparam name="T">The <see cref="System.Type"/> of object being deserialized.</typeparam> /// <param name="jsonBuffer">The JSON buffer to deserialize</param> /// <param name="encoding">The encoding to use.</param> /// <returns>The deserialized object from the JSON string.</returns> public static T Parse <T>(this JsonSerializer serializer, ArraySegment <byte> jsonBuffer, Encoding encoding) { if (serializer == null) { throw new ArgumentNullException("serializer"); } using (var reader = new ArraySegmentTextReader(jsonBuffer, encoding)) { return((T)serializer.Deserialize(reader, typeof(T))); } }
public void OnReceived(int streamIndex, ulong id, ScaleoutMessage message) { JsonSerializer serializer = new JsonSerializer(); foreach (Message msg in message.Messages) { string msgString; using (ArraySegmentTextReader astr = new ArraySegmentTextReader(msg.Value, msg.Encoding)) { msgString = astr.ReadToEnd(); } ClientHubInvocation clientHubInvocation = serializer.Parse <ClientHubInvocation>(msgString); if (clientHubInvocation.Args != null && clientHubInvocation.Args.Length == 1) { byte[] orginalArr = Convert.FromBase64String(clientHubInvocation.Args[0].ToString()); var idArr = BitConverter.GetBytes(id); byte[] resultArr = new byte[orginalArr.Length + idArr.Length]; idArr.CopyTo(resultArr, 0); orginalArr.CopyTo(resultArr, idArr.Length); clientHubInvocation.Args[0] = Convert.ToBase64String(resultArr.ToArray()); StringBuilder sb = new StringBuilder(); using (StringWriter sr = new StringWriter(sb)) { serializer.Serialize(sr, clientHubInvocation); sr.Flush(); } msg.Value = new ArraySegment <byte>(msg.Encoding.GetBytes(sb.ToString())); } } }