Esempio n. 1
0
 /// <summary>
 /// Encodes the reply.
 /// </summary>
 /// <param name="parameters">The parameters.</param>
 /// <param name="result">The result.</param>
 /// <returns>System.Byte[].</returns>
 public override byte[] EncodeReply(object[] parameters, object result)
 {
     byte[] body;
     using (MemoryStream ms = new MemoryStream())
     {
         using (StreamWriter sw = new StreamWriter(ms, Encoding.UTF8))
         {
             using (JsonWriter writer = new JsonTextWriter(sw))
             {
                 if (result == null)
                 {
                     serializer.Serialize(writer, null);
                 }
                 else
                 {
                     JToken token = JToken.FromObject(result, serializer);
                     JsonFormatterUtility.JTokenToSerialize(token);
                     serializer.Serialize(writer, token);
                 }
                 writer.Flush();
             }
         }
         body = ms.ToArray();
     }
     return(body);
 }
Esempio n. 2
0
 /// <summary>
 /// Encodes the parameters.
 /// </summary>
 /// <param name="parameters">The parameters.</param>
 /// <returns>System.Byte[].</returns>
 public override byte[] EncodeParameters(object[] parameters)
 {
     byte[] body;
     using (MemoryStream ms = new MemoryStream())
     {
         using (StreamWriter sw = new StreamWriter(ms, Encoding.UTF8))
         {
             using (JsonWriter writer = new JsonTextWriter(sw))
             {
                 int index = -1;
                 writer.WriteStartObject();
                 foreach (var parameter in this.OperationParameters)
                 {
                     object paramValue = parameters[++index];
                     writer.WritePropertyName(parameter.Name);
                     if (paramValue == null)
                     {
                         serializer.Serialize(writer, null);
                     }
                     else
                     {
                         JToken current = JToken.FromObject(paramValue, serializer);
                         JsonFormatterUtility.JTokenToSerialize(current);
                         serializer.Serialize(writer, current);
                     }
                 }
                 writer.WriteEndObject();
                 writer.Flush();
             }
         }
         body = ms.ToArray();
     }
     return(body);
 }
Esempio n. 3
0
 /// <summary>
 /// Decodes the reply.
 /// </summary>
 /// <param name="body">The body.</param>
 /// <param name="parameters">The parameters.</param>
 /// <returns>System.Object.</returns>
 public override object DecodeReply(byte[] body, object[] parameters)
 {
     using (MemoryStream ms = new MemoryStream(body))
     {
         using (StreamReader sr = new StreamReader(ms))
         {
             using (JsonReader reader = new JsonTextReader(sr))
             {
                 JToken token = serializer.Deserialize <JToken>(reader);
                 Type   type  = this.ServiceRegister.GetTypeByName(JsonFormatterUtility.GetTypeNameFromJObject(token as JObject), false) ?? this.OperationResult.NormalizedType;
                 object ret   = token.ToObject(type, serializer);
                 return(ret);
             }
         }
     }
 }
Esempio n. 4
0
 /// <summary>
 /// Decodes the parameters.
 /// </summary>
 /// <param name="body">The body.</param>
 /// <param name="parameters">The parameters.</param>
 public override void DecodeParameters(byte[] body, object[] parameters)
 {
     using (MemoryStream ms = new MemoryStream(body))
     {
         using (StreamReader sr = new StreamReader(ms))
         {
             using (JsonReader reader = new JsonTextReader(sr))
             {
                 JObject wrappedParameters = serializer.Deserialize <JObject>(reader);
                 int     indexParam        = -1;
                 foreach (var parameter in this.OperationParameters)
                 {
                     JProperty property = wrappedParameters.Property(parameter.Name);
                     if (property != null)
                     {
                         Type type = this.ServiceRegister.GetTypeByName(JsonFormatterUtility.GetTypeNameFromJObject(property.Value as JObject), false) ?? parameter.NormalizedType;
                         parameters[++indexParam] = property.Value.ToObject(type, serializer);
                     }
                 }
             }
         }
     }
 }