0

I want to serialize a object that has a string property with value: " if(_inputs.loopInput == \"2\"){\n return true\n }\n else{\n return false;\n }"

The issue is that, when I do serialize it, the value transforms it self in: " if(_inputs.loopInput == \\u00222\\u0022){\\n return true\\n }\\n else{\\n return false;\\n }\"

I would like to get rid of this \u00222\u0022 in the escaped "2".

Does any one know why this happens?

Before this, I was using Newtonsoft, and when I invoke the serialize method, I pass this parameters:

JsonConvert.SerializeObject(_data,
                            Formatting.None,
                            settings ?? new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore });

Getting this result:

"{\"code\":\"    if(_inputs.loopInput == \\\"2\\\"){\\n        return true\\n    }\\n    else{\\n        return false;\\n    }\"

I would like to get the same result, but using System.Text.Json.

This is how I am using:

JsonSerializer.Serialize(lambdaCallBody);

Summing up...

Desired serialize result (using NewtonSoft):

"{\"code\":\"    if(_inputs.loopInput == \\\"2\\\"){\\n        return true\\n    }\\n    else{\\n        return false;\\n    }\",\"inputs\":{\"loopInput\":\"2\"},\"execInfo\":{\"tenant\":\"__TEST__agentFlowV3\",\"flowId\":\"5e9736ff554defb30152a9a2\",\"jobId\":\"1586968416402\"}}"

What I am getting (using System.Text.Json):

"{\"code\":\"    if(_inputs.loopInput == \\u00222\\u0022){\\n        return true\\n    }\\n    else{\\n        return false;\\n    }\",\"inputs\":{\"loopInput\":\"\\u00222\\u0022\"},\"execInfo\":{\"tenant\":\"__TEST__agentFlowV3\",\"flowId\":\"5e9736ff554defb30152a9a2\",\"jobId\":\"1586968416402\",\"mappingFunction\":null,\"functionReferences\":null}}"
Gui Oliveira
  • 155
  • 2
  • 9
  • `\u0022` is an encoded version of `"` – Tim Apr 21 '20 at 22:11
  • What is settings? is it defined somewhere else? – Oguz Ozgul Apr 21 '20 at 22:13
  • In the case that it works it is null, so the value used is new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }) – Gui Oliveira Apr 21 '20 at 22:14
  • @OguzOzgul But this was when I was using NewtonSoft, now that I am using System.Text.Json, I do not pass any parameters. So its like: JsonSerializer.Serialize(lambdaCallBody) – Gui Oliveira Apr 21 '20 at 22:15
  • Can you please show desired JSON output? Note that you can't really have unescaped quotes in JSON - make sure desired sample shows valid JSON. – Alexei Levenkov Apr 21 '20 at 22:20
  • @AlexeiLevenkov Oh sorry, I just adited posting the Json returned using NewtonSoft. Notice that it just escapes one more time, insted of adding \u0022. – Gui Oliveira Apr 21 '20 at 22:21
  • `System.Text.Json` escapes many more characters that is strictly necessary for the JSON standard. For explanations and workarounds, see [dotnet core System.Text.Json unescape unicode string](https://stackoverflow.com/q/58003293/3744182) and [Issues with System.Text.Json serializing Unicode characters (like emojis)](https://stackoverflow.com/q/58738258/3744182). – dbc Apr 21 '20 at 23:52
  • In fact I think this may be a duplicate of those two, agree? (I haven't tried running your code to make sure, but it looks the same.) – dbc Apr 21 '20 at 23:57
  • @dbc I will try it out, just a sec. – Gui Oliveira Apr 21 '20 at 23:59
  • 1
    @dbc It is duplicate actually. It worked after a set the property Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping in the JsonSerializerOptions. Thanks – Gui Oliveira Apr 22 '20 at 00:23

0 Answers0