wbish/jsondiffpatch.net

Support for .NET 3.5

jadarnel27 opened this issue · 6 comments

This library is a great fit for a project I'm working on, but unfortunately I am "stuck" on .NET 3.5 for the time being.

I'm wondering if you'd be open to a PR adding support for .NET 3.5? I know this potentially introduces additional support burden down the road, so I understand if that's not desirable.

It looks like the only code change needed (at this time) would be here:

internal static string UrlEncode(string str)
{
str = WebUtility.UrlEncode(str);
return Regex.Replace(str, "(%[0-9A-F]{2})", encodedChar => encodedChar.Value.ToLowerInvariant());
}
internal static string UrlDecode(string str)
{
return WebUtility.UrlDecode(str);
}

This is because System.Net.WebUtility was introduce in .NET 4.0.

The required change (after adding 3.5 as a build target) would be to replace the use of WebUtility.UrlEncode and WebUtility.UrlDecode with something equivalent. I think that System.Uri.EscapeDataString and System.Uri.UnescapeDataString would do the trick for the purposes of this library.

Or rather than replacing, one could add conditional compilation like this to make this a 3.5-only change:

	internal static string UrlEncode(string str)
	{
#if NET35
		str = Uri.EscapeDataString(str);
#else
		str = WebUtility.UrlEncode(str);
#endif
		return Regex.Replace(str, "(%[0-9A-F]{2})", encodedChar => encodedChar.Value.ToLowerInvariant());
	}

	internal static string UrlDecode(string str)
	{
#if NET35
		return Uri.UnescapeDataString(str);
#else
		return WebUtility.UrlDecode(str);
#endif
	}

Let me know what you think!

I forgot to mention, but I'd be happy to actually make the change and submit the PR if you are open to the idea.

wbish commented
wbish commented

@jadarnel27 forgot to mention: I tried this once a long time ago and vaguely remember issues getting all the diffmatchpatch unit tests to pass because of some encoding problem so I abandoned the effort.

Yes, I'm running into that same issue myself 😄 It looks like the library functions in System.Web (HttpUtility.UrlEncode / UrlDecode) are more compatible - at least, all the unit tests pass. Let me know if you have any concerns about that approach (it feels a bit odd to add System.Web to this type of project).

I'm running into a separate issue where the following package isn't compatible with targeting .NET 3.5:

<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />

I assume you're using that in the CI build to run the unit tests? Looking into possible solutions for this, but wanted to keep you posted on my progress.

close the ticket please.

Adding .NET 3.5 support fell off my radar, and seems less useful as .NET 5 approaches.