autorest icon indicating copy to clipboard operation
autorest copied to clipboard

C# SafeJsonConvert serializes null into string "null" -- it should be string.Empty

Open matthchr opened this issue 10 years ago • 6 comments

The issue is here: public static string SerializeObject(object obj, JsonSerializerSettings settings) in SafeJsonConvert.cs -- it seems to turn a null obj parameter into a the string "null" -- it should be turned into the empty string I think.

matthchr avatar Mar 22 '16 00:03 matthchr

Actually, the title of this is a bit incorrect. The real issue is that passing a null parameter as a body parameter in C# results in the string "null"

I will submit a fix to avoid this.

matthchr avatar Mar 22 '16 21:03 matthchr

Fix is here

matthchr avatar Mar 23 '16 00:03 matthchr

This is fixed now

matthchr avatar Mar 23 '16 22:03 matthchr

@matthchr I skimmed the PR, but I just want to confirm: This was fixed by avoiding a call to SafeJsonConvert.SerializeObject(null), not by modifying SafeJsonConvert itself, correct?

bitsforthought avatar Apr 05 '16 20:04 bitsforthought

Yes, SafeJsonConvert internally called Newtonsoft's serialization method, and the default behavior of Newtonsoft's serialization method is that it serializes null to "null" - there's not an easy way to change the behavior of Newtonsoft's handling of the value null (you can change it's handling of null properties but that's a bit different).

matthchr avatar Apr 05 '16 20:04 matthchr

Closing -- bug reported fixed.

fearthecowboy avatar Jun 01 '16 22:06 fearthecowboy