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

DateParseHandling (and possibly other settings) not respected when receiving a message #227

Closed
joshlang opened this issue Jan 29, 2019 · 1 comment
Assignees
Labels
Milestone

Comments

@joshlang
Copy link

joshlang commented Jan 29, 2019

Hi.

JSON.NET has a horrendous terrible worst-idea-ever silly pretty bad feature whereby if a string looks like a date, it'll eat it up, change it to whatever it feels like, and spit it out in some other format (preferably one you're not expecting).

We can work around it by setting DateParseHandling = DateParseHandling.None in the serializer.

This works when SENDING an rpc call. But NOT when RECEIVING an rpc call.

Here's a minimal repo:


		class meow
		{
			public string d { get; set; }
		}
		static async Task Main(string[] args)
		{
			string s = @"Content-Length: 81

{""jsonrpc"":""2.0"",""method"":""asdf"",""params"":[{""d"":""2019-01-29T03:37:28.4433841Z""}]}";
			using (MemoryStream ms = new MemoryStream())
			{
				ms.Write(Encoding.ASCII.GetBytes(s));
				ms.Position = 0;

				JsonMessageFormatter jmf = new JsonMessageFormatter();
				jmf.JsonSerializer.DateParseHandling = DateParseHandling.None;

				HeaderDelimitedMessageHandler mf = new HeaderDelimitedMessageHandler(ms, jmf);
				JsonRpc rpc = new JsonRpc(mf);
				rpc.AddLocalRpcMethod("asdf", (Func<meow, Task>)(mmm =>
				{
					Console.WriteLine(mmm.d);
					return Task.CompletedTask;
				}));
				rpc.StartListening();
				await Task.Delay(1000);
			}
			Console.ReadKey();
		}

The "d" field should be the string "2019-01-29T03:37:28.4433841Z"
But ends up being the string "01/29/2019 03:37:28"

@AArnott AArnott self-assigned this Jan 29, 2019
@AArnott AArnott added the bug label Jan 29, 2019
@AArnott
Copy link
Member

AArnott commented Jan 29, 2019

StreamJsonRpc uses a two-step deserialization whereby we first read JTokens, then we later deserialize those JTokens with the user-provided JsonSerializer. Looking into this, it appears the date time parsing (which I agree, seems hazardous!) happens during parsing into JTokens. As this step doesn't take a settings or a JsonSerializer object, the only thing we can do is manually copy the relevant settings from the user-supplied JsonSerializer object onto their matching properties on the JsonTextReader. Tedious and vulnerable to new properties being added which we then have to copy over as well. But it's fixable. :)

@AArnott AArnott added this to the v2.0 milestone Jan 29, 2019
AArnott added a commit to AArnott/vs-streamjsonrpc that referenced this issue Jan 29, 2019
AArnott added a commit to AArnott/vs-streamjsonrpc that referenced this issue Jan 29, 2019
AArnott pushed a commit to AArnott/vs-streamjsonrpc that referenced this issue Jan 10, 2024
Bumps [powershell](https://github.com/PowerShell/PowerShell) from 7.3.8 to 7.3.9.
- [Release notes](https://github.com/PowerShell/PowerShell/releases)
- [Commits](PowerShell/PowerShell@v7.3.8...v7.3.9)

---
updated-dependencies:
- dependency-name: powershell
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants