public async Task ChangesTo_SerializerSettings_AfterSerialization_DoNotAffectSerialization() { // Arrange var person = new User() { FullName = "John", age = 35 }; var expectedOutput = JsonConvert.SerializeObject(person, new JsonSerializerSettings()); var jsonFormatter = new TestableJsonOutputFormatter(new JsonSerializerSettings()); // This will create a serializer - which gets cached. var outputFormatterContext1 = GetOutputFormatterContext(person, typeof(User)); await jsonFormatter.WriteResponseBodyAsync(outputFormatterContext1, Encoding.UTF8); // These changes should have no effect. jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); jsonFormatter.SerializerSettings.Formatting = Formatting.Indented; var outputFormatterContext2 = GetOutputFormatterContext(person, typeof(User)); // Act await jsonFormatter.WriteResponseBodyAsync(outputFormatterContext2, Encoding.UTF8); // Assert var body = outputFormatterContext2.HttpContext.Response.Body; Assert.NotNull(body); body.Position = 0; var content = new StreamReader(body, Encoding.UTF8).ReadToEnd(); Assert.Equal(expectedOutput, content); }
public async Task NamingStrategy_DoesNotAffectSerialization_WithJsonObject(NamingStrategy strategy) { // Arrange var user = new UserWithJsonObject { age = 35, FullName = "John", }; var expected = "{\"age\":35,\"full_name\":\"John\"}"; var context = GetOutputFormatterContext(user, typeof(UserWithJsonProperty)); var settings = new JsonSerializerSettings { ContractResolver = new DefaultContractResolver { NamingStrategy = strategy, }, }; var formatter = new TestableJsonOutputFormatter(settings); // Act await formatter.WriteResponseBodyAsync(context, Encoding.UTF8); // Assert var body = context.HttpContext.Response.Body; Assert.NotNull(body); body.Position = 0; var content = new StreamReader(body, Encoding.UTF8).ReadToEnd(); Assert.Equal(expected, content); }
public async Task NamingStrategy_AffectsSerialization(NamingStrategy strategy, string expected) { // Arrange var user = new User { FullName = "John", age = 35 }; var context = GetOutputFormatterContext(user, typeof(User)); var settings = new JsonSerializerSettings { ContractResolver = new DefaultContractResolver { NamingStrategy = strategy, }, }; var formatter = new TestableJsonOutputFormatter(settings); // Act await formatter.WriteResponseBodyAsync(context, Encoding.UTF8); // Assert var body = context.HttpContext.Response.Body; Assert.NotNull(body); body.Position = 0; var content = new StreamReader(body, Encoding.UTF8).ReadToEnd(); Assert.Equal(expected, content); }
public async Task SerializingWithPreserveReferenceHandling() { // Arrange var expected = "{\"$id\":\"1\",\"fullName\":\"John\",\"age\":35}"; var user = new User { FullName = "John", age = 35 }; var settings = new JsonSerializerSettings { ContractResolver = new DefaultContractResolver { NamingStrategy = new CamelCaseNamingStrategy(), }, PreserveReferencesHandling = PreserveReferencesHandling.All, }; var formatter = new TestableJsonOutputFormatter(settings); for (var i = 0; i < 3; i++) { // Act var context = GetOutputFormatterContext(user, typeof(User)); await formatter.WriteResponseBodyAsync(context, Encoding.UTF8); // Assert var body = context.HttpContext.Response.Body; Assert.NotNull(body); body.Position = 0; var content = new StreamReader(body, Encoding.UTF8).ReadToEnd(); Assert.Equal(expected, content); } }
public async Task NamingStrategy_DoesNotAffectDictionarySerialization(NamingStrategy strategy) { // Arrange var dictionary = new Dictionary <string, int>(StringComparer.Ordinal) { { "id", 12 }, { "Id", 12 }, { "fullName", 12 }, { "full-name", 12 }, { "FullName", 12 }, { "full_Name", 12 }, }; var expected = "{\"id\":12,\"Id\":12,\"fullName\":12,\"full-name\":12,\"FullName\":12,\"full_Name\":12}"; var context = GetOutputFormatterContext(dictionary, typeof(Dictionary <string, int>)); var settings = new JsonSerializerSettings { ContractResolver = new DefaultContractResolver { NamingStrategy = strategy, }, }; var formatter = new TestableJsonOutputFormatter(settings); // Act await formatter.WriteResponseBodyAsync(context, Encoding.UTF8); // Assert var body = context.HttpContext.Response.Body; Assert.NotNull(body); body.Position = 0; var content = new StreamReader(body, Encoding.UTF8).ReadToEnd(); Assert.Equal(expected, content); }
public async Task ChangesTo_SerializerSettings_AfterSerialization_DoNotAffectSerialization() { // Arrange var person = new User() { FullName = "John", age = 35 }; var expectedOutput = JsonConvert.SerializeObject(person, new JsonSerializerSettings()); var jsonFormatter = new TestableJsonOutputFormatter(new JsonSerializerSettings()); // This will create a serializer - which gets cached. var outputFormatterContext1 = GetOutputFormatterContext(person, typeof(User)); await jsonFormatter.WriteResponseBodyAsync(outputFormatterContext1, Encoding.UTF8); // These changes should have no effect. jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); jsonFormatter.SerializerSettings.Formatting = Formatting.Indented; var outputFormatterContext2 = GetOutputFormatterContext(person, typeof(User)); // Act await jsonFormatter.WriteResponseBodyAsync(outputFormatterContext2, Encoding.UTF8); // Assert var body = outputFormatterContext2.HttpContext.Response.Body; Assert.NotNull(body); body.Position = 0; var content = new StreamReader(body, Encoding.UTF8).ReadToEnd(); Assert.Equal(expectedOutput, content); }
public async Task NamingStrategy_DoesNotAffectSerialization_WithJsonObject(NamingStrategy strategy) { // Arrange var user = new UserWithJsonObject { age = 35, FullName = "John", }; var expected = "{\"age\":35,\"full_name\":\"John\"}"; var context = GetOutputFormatterContext(user, typeof(UserWithJsonProperty)); var settings = new JsonSerializerSettings { ContractResolver = new DefaultContractResolver { NamingStrategy = strategy, }, }; var formatter = new TestableJsonOutputFormatter(settings); // Act await formatter.WriteResponseBodyAsync(context, Encoding.UTF8); // Assert var body = context.HttpContext.Response.Body; Assert.NotNull(body); body.Position = 0; var content = new StreamReader(body, Encoding.UTF8).ReadToEnd(); Assert.Equal(expected, content); }
public async Task NamingStrategy_DoesNotAffectDictionarySerialization(NamingStrategy strategy) { // Arrange var dictionary = new Dictionary<string, int>(StringComparer.Ordinal) { { "id", 12 }, { "Id", 12 }, { "fullName", 12 }, { "full-name", 12 }, { "FullName", 12 }, { "full_Name", 12 }, }; var expected = "{\"id\":12,\"Id\":12,\"fullName\":12,\"full-name\":12,\"FullName\":12,\"full_Name\":12}"; var context = GetOutputFormatterContext(dictionary, typeof(Dictionary<string, int>)); var settings = new JsonSerializerSettings { ContractResolver = new DefaultContractResolver { NamingStrategy = strategy, }, }; var formatter = new TestableJsonOutputFormatter(settings); // Act await formatter.WriteResponseBodyAsync(context, Encoding.UTF8); // Assert var body = context.HttpContext.Response.Body; Assert.NotNull(body); body.Position = 0; var content = new StreamReader(body, Encoding.UTF8).ReadToEnd(); Assert.Equal(expected, content); }
public async Task NamingStrategy_AffectsSerialization(NamingStrategy strategy, string expected) { // Arrange var user = new User { FullName = "John", age = 35 }; var context = GetOutputFormatterContext(user, typeof(User)); var settings = new JsonSerializerSettings { ContractResolver = new DefaultContractResolver { NamingStrategy = strategy, }, }; var formatter = new TestableJsonOutputFormatter(settings); // Act await formatter.WriteResponseBodyAsync(context, Encoding.UTF8); // Assert var body = context.HttpContext.Response.Body; Assert.NotNull(body); body.Position = 0; var content = new StreamReader(body, Encoding.UTF8).ReadToEnd(); Assert.Equal(expected, content); }