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

ConvertToJson not escaping invalid unicode control codes #1715

Open
samcarton opened this issue Dec 14, 2023 · 1 comment
Open

ConvertToJson not escaping invalid unicode control codes #1715

samcarton opened this issue Dec 14, 2023 · 1 comment

Comments

@samcarton
Copy link

Bug description

ConvertToJson only seems to handle escaping \r \n \t \f \b - other unicode control codes come through unescaped.

Repro steps

  • referencing System.Text.Json
public class Test
{
	public string Value { get; set; }
}


void Main()
{
	// write msgpack
	var test = new Test { Value = "\u0011" };
	var testJson = System.Text.Json.JsonSerializer.Serialize(test);
	var msgpackData = MessagePack.MessagePackSerializer.ConvertFromJson(testJson);


	// read msgpack
	var msgpackJson = MessagePack.MessagePackSerializer.ConvertToJson(msgpackData);

	// throws exception: '0x11' is invalid within a JSON string. The string should be correctly escaped. Path: $.Value | LineNumber: 0 | BytePositionInLine: 10.
	var testOut = System.Text.Json.JsonSerializer.Deserialize<Test>(msgpackJson);
}

Expected behavior

  • Escape invalid unicode characters

Actual behavior

  • invalid unicode characters not escaped
  • Version used: 2.5.140
  • Runtime: .NET 7

Additional context

  • We are using json to read\write between our C# classes and messagepack because the Union attribute isn't really a good solution when there are hundreds of concrete implementations of an interface or set of interfaces.
  • To get around this currently we need to strip out invalid characters with Regex. See List of controls codes.
  • System.Text.Json does handle escaping unicode control characters, and is able to deserialize its own output back. It's just when the json comes through MessagePackSerializer.ConvertToJson the characters are not escaped.
@AArnott
Copy link
Collaborator

AArnott commented Dec 14, 2023

This isn't a JSON serialization library. The JSON support it has is merely included for debuggability so that you can see what's being serialized in what otherwise is an unreadable binary format.
So this is a low priority that we wouldn't get to. If you wanted to send a PR to address this though, we'd review it. But I can't promise we'd merge it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants