Skip to content
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

Consider adding Utf8JsonWriter support for writing raw property names #54005

Open
Tracked by #77019
layomia opened this issue Jun 10, 2021 · 2 comments
Open
Tracked by #77019

Consider adding Utf8JsonWriter support for writing raw property names #54005

layomia opened this issue Jun 10, 2021 · 2 comments
Labels
area-System.Text.Json source-generator Indicates an issue with a source generator feature
Milestone

Comments

@layomia
Copy link
Contributor

layomia commented Jun 10, 2021

#1784 discusses writing raw JSON values when using Utf8JsonWriter. We can build on top that to support writing raw property names. This could be useful when pre-generating JSON serialization logic and we know the runtime property names ahead of time.

From @Tornhoof in #1784 (comment):

As linked by @layomia for the source-gen code (#53212 (comment)) having a Raw Write functionality could be useful to replace the JsonEncodedText members with ReadOnlySpan<byte> and preencoded utf8 member names. At the moment, Benchmarks show that JsonEncodedText is faster (10-15%) for writing known property names in json as there is a fastpath for it. For the ROS overload it needs to check the validity of the bytes and possibly escaping. Having a Raw API to bypass the encoding check the performance, should be similar and would allow the source-gen to use ROS.

Benchmarks (also from #1784 (comment))

	public class JsonWriteBenchmark
	{
		private static readonly JsonEncodedText JetPropertyName = JsonEncodedText.Encode("message");

		private static ReadOnlySpan<byte> ROSPropertyName =>
			new byte[] {0x6D, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65};


		[Benchmark]
		public void WriteReadOnlySpanPropertyName()
		{
			using var ms = new MemoryStream();
			Utf8JsonWriter writer = new Utf8JsonWriter(ms);
			writer.WriteStartObject();
			writer.WriteString(ROSPropertyName, "Hello World");
			writer.WriteEndObject();
			writer.Flush();
		}

		[Benchmark]
		public void WriteJsonEncodedTextPropertyName()
		{
			using var ms = new MemoryStream();
			Utf8JsonWriter writer = new Utf8JsonWriter(ms);
			writer.WriteStartObject();
			writer.WriteString(JetPropertyName, "Hello World");
			writer.WriteEndObject();
			writer.Flush();
		}
	}
@layomia layomia added this to the 6.0.0 milestone Jun 10, 2021
@ghost
Copy link

ghost commented Jun 10, 2021

Tagging subscribers to this area: @eiriktsarpalis, @layomia
See info in area-owners.md if you want to be subscribed.

Issue Details

#1784 discusses writing raw property values when using Utf8JsonWriter. We can build on top that to support writing raw property names. This could be useful when pre-generating JSON serialization logic and we know the runtime property names ahead of time.

From @Tornhoof in #1784 (comment):

As linked by @layomia for the source-gen code (#53212 (comment)) having a Raw Write functionality could be useful to replace the JsonEncodedText members with ReadOnlySpan<byte> and preencoded utf8 member names. At the moment, Benchmarks show that JsonEncodedText is faster (10-15%) for writing known property names in json as there is a fastpath for it. For the ROS overload it needs to check the validity of the bytes and possibly escaping. Having a Raw API to bypass the encoding check the performance, should be similar and would allow the source-gen to use ROS.

Benchmarks (also from #1784 (comment))

	public class JsonWriteBenchmark
	{
		private static readonly JsonEncodedText JetPropertyName = JsonEncodedText.Encode("message");

		private static ReadOnlySpan<byte> ROSPropertyName =>
			new byte[] {0x6D, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65};


		[Benchmark]
		public void WriteReadOnlySpanPropertyName()
		{
			using var ms = new MemoryStream();
			Utf8JsonWriter writer = new Utf8JsonWriter(ms);
			writer.WriteStartObject();
			writer.WriteString(ROSPropertyName, "Hello World");
			writer.WriteEndObject();
			writer.Flush();
		}

		[Benchmark]
		public void WriteJsonEncodedTextPropertyName()
		{
			using var ms = new MemoryStream();
			Utf8JsonWriter writer = new Utf8JsonWriter(ms);
			writer.WriteStartObject();
			writer.WriteString(JetPropertyName, "Hello World");
			writer.WriteEndObject();
			writer.Flush();
		}
	}
Author: layomia
Assignees: -
Labels:

area-System.Text.Json

Milestone: 6.0.0

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Jun 10, 2021
@layomia layomia removed the untriaged New issue has not been triaged by the area owner label Jun 10, 2021
@layomia layomia modified the milestones: 6.0.0, 7.0.0 Jul 23, 2021
@layomia layomia added the source-generator Indicates an issue with a source generator feature label Jan 21, 2022
@jeffhandley jeffhandley modified the milestones: 7.0.0, Future Apr 6, 2022
@ahazelwood
Copy link

This is currently a blocker for us as we rely heavily in places on the JSON.Net JsonTextWriter QuoteName property, where we set the value to false to output serialized objects directly for usage within javascript on a page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Text.Json source-generator Indicates an issue with a source generator feature
Projects
None yet
Development

No branches or pull requests

3 participants