Skip to content

Use System.Text.Json's copy ctor #21016

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ internal static SystemTextJsonOutputFormatter CreateFormatter(JsonOptions jsonOp
if (jsonSerializerOptions.Encoder is null)
{
// If the user hasn't explicitly configured the encoder, use the less strict encoder that does not encode all non-ASCII characters.
jsonSerializerOptions = jsonSerializerOptions.Copy(JavaScriptEncoder.UnsafeRelaxedJsonEscaping);
jsonSerializerOptions = new JsonSerializerOptions(jsonSerializerOptions)
{
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hopefully the new CreateForWeb option uses the right defaults and then we don't need this at all 👀

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rynowak @pranavkm It seems like you want us to include this in the new JsonSerializerOptions for web. So, the web options will have PropertyNameCaseInsensitive = true, PropertyNamingPolicy = JsonNamingPolicy.CamelCase, and Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping.

Is that correct?

I also pointed this out on dotnet/runtime#34626 (comment) in case you missed it.
cc @layomia

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also cc @terrajobst.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK, when I spoke to @GrabYourPitchforks, the recommendation was that using UnsafeRelaxedJsonEscaping is only safe if the JSON wasn't interspersed with markup. Web might not convey that this is meant for REST \ API scenarios.

}

return new SystemTextJsonOutputFormatter(jsonSerializerOptions);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.IO;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Microsoft.Extensions.Primitives;
using Microsoft.Net.Http.Headers;
using Xunit;

namespace Microsoft.AspNetCore.Mvc.Formatters
{
public class SystemTextJsonOutputFormatterTest : JsonOutputFormatterTestBase
Expand All @@ -9,5 +17,51 @@ protected override TextOutputFormatter GetOutputFormatter()
{
return SystemTextJsonOutputFormatter.CreateFormatter(new JsonOptions());
}

[Fact]
public async Task WriteResponseBodyAsync_AllowsConfiguringPreserveReferenceHandling()
{
// Arrange
var formatter = GetOutputFormatter();
((SystemTextJsonOutputFormatter)formatter).SerializerOptions.ReferenceHandling = ReferenceHandling.Preserve;
var expectedContent = "{\"$id\":\"1\",\"name\":\"Person\",\"child\":{\"$id\":\"2\",\"name\":\"Child\",\"child\":null,\"parent\":{\"$ref\":\"1\"}},\"parent\":null}";
var person = new Person
{
Name = "Person",
Child = new Person { Name = "Child", },
};
person.Child.Parent = person;

var mediaType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8");
var encoding = CreateOrGetSupportedEncoding(formatter, "utf-8", isDefaultEncoding: true);

var body = new MemoryStream();
var actionContext = GetActionContext(mediaType, body);

var outputFormatterContext = new OutputFormatterWriteContext(
actionContext.HttpContext,
new TestHttpResponseStreamWriterFactory().CreateWriter,
typeof(Person),
person)
{
ContentType = new StringSegment(mediaType.ToString()),
};

// Act
await formatter.WriteResponseBodyAsync(outputFormatterContext, Encoding.GetEncoding("utf-8"));

// Assert
var actualContent = encoding.GetString(body.ToArray());
Assert.Equal(expectedContent, actualContent);
}

private class Person
{
public string Name { get; set; }

public Person Child { get; set; }

public Person Parent { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ private static JsonSerializerOptions GetHtmlSafeSerializerOptions(JsonSerializer
return serializerOptions;
}

return serializerOptions.Copy(JavaScriptEncoder.Default);
return new JsonSerializerOptions(serializerOptions)
{
Encoder = JavaScriptEncoder.Default,
};
}
}
}