public static void UpdateResponseWithError(HttpRequest request, HttpResponse response, JsonMediaTypeFormatter jsonFormatter, XmlMediaTypeFormatter xmlFormatter, string errorMessage) { response.Clear(); response.StatusCode = (int)HttpStatusCode.BadRequest; var error = new HttpError(errorMessage); var acceptTypes = request.AcceptTypes?.Select(at => at.ToLowerInvariant()) ?? new string[] { }; if (xmlFormatter != null && xmlFormatter.SupportedMediaTypes.Select(mt => mt.MediaType).Intersect(acceptTypes).Any()) { xmlFormatter.CreateXmlSerializer(error.GetType()).Serialize(response.OutputStream, error); response.AddHeader("Content-Type", "application/xml"); } // JSON is the default format else if (jsonFormatter != null) { // Use UTF8Encoding contractor that does not provide Unicode byte order mark (BOM) // that in turn causes a deserialization error performed by Newtonsoft json formatter in integration tests. jsonFormatter.WriteToStream(error.GetType(), error, response.OutputStream, new UTF8Encoding()); response.AddHeader("Content-Type", "application/json"); } else { response.Write(errorMessage); response.AddHeader("Content-Type", "text/plain"); } }
public static string SerializeObject <T>(T value) { var jsonFormatter = new JsonMediaTypeFormatter(); using (var ms = new MemoryStream()) { jsonFormatter.WriteToStream(typeof(T), value, ms, Encoding.UTF8); ms.Position = 0; return(Encoding.UTF8.GetString(ms.ToArray())); } }
private void CheckRequestContainsOnlyExpectedParameters() { jsonMediaTypeFormatter.WriteToStream(courseSearchRequest.GetType(), courseSearchRequest, ms, Encoding.Default); var request = Encoding.ASCII.GetString(ms.ToArray()); //All the properties that can be serialised foreach (PropertyInfo prop in courseSearchRequest.GetType().GetProperties()) { // Is this property expected to be send in the request if (shouldContainParameters.Contains(prop.Name)) { var type = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType; if (type == typeof(string)) { request.Should().Contain($"\"{prop.Name}\":\"{prop.GetValue(courseSearchRequest)}\""); } else if (type == typeof(int) || type == typeof(double)) { request.Should().Contain($"\"{prop.Name}\":{prop.GetValue(courseSearchRequest)}"); } else if (type == typeof(List <int>)) { var propList = prop.GetValue(courseSearchRequest) as List <int>; request.Should().Contain($"\"{prop.Name}\":[{string.Join(",", propList.Select(n => n.ToString(CultureInfo.CurrentCulture)).ToArray())}]"); } else if (type == typeof(List <StudyMode>)) { var propList = prop.GetValue(courseSearchRequest) as List <StudyMode>; request.Should().Contain($"\"{prop.Name}\":[{string.Join(",", propList.Select(n => ((int)n)).ToArray())}]"); } else if (type == typeof(List <DeliveryMode>)) { var propList = prop.GetValue(courseSearchRequest) as List <DeliveryMode>; request.Should().Contain($"\"{prop.Name}\":[{string.Join(",", propList.Select(n => ((int)n)).ToArray())}]"); } else if (type == typeof(List <string>)) { var propList = prop.GetValue(courseSearchRequest) as List <string>; request.Should().Contain($"\"{prop.Name}\":[{string.Join(",", propList.Select(n => $"\"{n}\"").ToArray())}]"); } else { Assert.True(true == false, "Type of property is not supported, amend this test"); } } else { //If the test is not expecting the property it should not be there request.Should().NotContain($"\"{prop.Name}\":"); } } }
private void WriteXHydraBinaryData(Type type, HydraBinaryData value, Stream writeStream) { var dataStream = new MemoryStream(); _jsonFormatter.WriteToStream(type, value, dataStream, Encoding.UTF8); BinaryWriter writer = new BinaryWriter(writeStream, Encoding.UTF8, true); writer.Write(5); writer.Write((int)dataStream.Length); writer.Write(value.Payload.Length); dataStream.CopyTo(writeStream); writer.Write(value.Payload); }
public static string FormatObject(object toFormat, JsonMediaTypeFormatter formatter) { string result; using (var stream = new MemoryStream()) { formatter.WriteToStream(toFormat.GetType(), toFormat, stream, new UTF8Encoding()); stream.Seek(0, SeekOrigin.Begin); result = new StreamReader(stream).ReadToEnd(); } Console.WriteLine(result); return(result); }
static void Main(string[] args) { byte[] abc = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; string test = "ttt"; HttpClient c = new HttpClient(); JsonMediaTypeFormatter f = new JsonMediaTypeFormatter(); MemoryStream ms = new MemoryStream(); f.WriteToStream(typeof(Tuple <byte[], string>), Tuple.Create(abc, test), ms, Encoding.Default); ms.Seek(0, SeekOrigin.Begin); var obj = f.ReadFromStream(typeof(Tuple <byte[], string>), ms, Encoding.Default, null); var t = (Tuple <byte[], string>)obj; var abc1 = t.Item1; StreamReader sr = new StreamReader(ms); Console.WriteLine(sr.ReadToEnd()); }