Click here to Skip to main content
14,694,532 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
am generating a json file using c#. One of the properties is a text: " paragraph text 1

". When I save the json file, the text is converted into this: "\u003cp\u003eparagraph text 1\u003c/p\u003e". I would like to preserve the original text. I have an entity named Field. It looks like this:

public partial class Field
    {
        [JsonProperty("name")]
        public string name { get; set; }

        [JsonProperty("identifier")]
        public string identifier { get; set; }

        [JsonProperty("type")]
        public string type { get; set; }

        [JsonProperty("isEditorDialogOpen", NullValueHandling = NullValueHandling.Ignore)]
        public bool? isEditorDialogOpen { get; set; }

        [JsonProperty("paragraphHeight", NullValueHandling = NullValueHandling.Ignore)]
        public string paragraphHeight { get; set; }

        [JsonProperty("integrationID")]
        public string integrationID { get; set; }

        [JsonProperty("hint")]
        public string hint { get; set; }

        [JsonProperty("tooltip")]
        public string tooltip { get; set; }

        [JsonProperty("validations")]
        public object[] validations { get; set; }

        [JsonProperty("permission")]
        public string permission { get; set; }

        [JsonProperty("maskingViewer")]
        public string maskingViewer { get; set; }

        [JsonProperty("defaultValue")]
        public object[] defaultValue { get; set; }

        [JsonProperty("width")]
        public string width { get; set; }

        [JsonProperty("editedParagraph", NullValueHandling = NullValueHandling.Ignore)]
        public string editedParagraph { get; set; }

        [JsonProperty("label")]
        public string label { get; set; }

        [JsonProperty("stepIdx")]
        public long stepIdx { get; set; }

        [JsonProperty("blockIdx")]
        public long blockIdx { get; set; }

        [JsonProperty("rowIdx")]
        public long rowIdx { get; set; }

        [JsonProperty("readOnly", NullValueHandling = NullValueHandling.Ignore)]
        public bool? readOnly { get; set; }

        [JsonProperty("clearable", NullValueHandling = NullValueHandling.Ignore)]
        public bool? clearable { get; set; }

        [JsonProperty("required", NullValueHandling = NullValueHandling.Ignore)]
        public bool? fieldRequired { get; set; }

        [JsonProperty("maxLength", NullValueHandling = NullValueHandling.Ignore)]
        [JsonConverter(typeof(ParseStringConverter))]
        public string maxLength { get; set; }

        [JsonProperty("fieldIdx", NullValueHandling = NullValueHandling.Ignore)]
        public long? fieldIdx { get; set; }

        [JsonProperty("shownInPdfMode", NullValueHandling = NullValueHandling.Ignore)]
        public bool? shownInPdfMode { get; set; }
    }

And this is the json:
"fields": [{
    "name": "editor.fields.paragraph",
    "identifier": "paragraph_kdg3wty6",
    "type": "paragraph",
    "isEditorDialogOpen": false,
    "paragraphHeight": "auto",
    "integrationID": "yobi1_a",
    "hint": "",
    "tooltip": "",
    "validations": [],
    "permission": "both",
    "maskingViewer": "none",
    "defaultValue": [],
    "width": "full",
    "editedParagraph": "\u003cp\u003eparagraph text 1\u003c/p\u003e",
    "label": "",
    "stepIdx": 0,
    "blockIdx": 0,
    "rowIdx": 1,
    "readOnly": null,
    "clearable": null,
    "fieldRequired": null,
    "maxLength": null,
    "fieldIdx": null,
    "shownInPdfMode": null
}]

And this is how I save the file:
var json = new JavaScriptSerializer().Serialize(field);
                //File.WriteAllText(destination, json);
                using (StreamWriter sw = new StreamWriter(File.Open(destination, FileMode.Create), Encoding.Unicode))
                {
                    sw.WriteLine(json);
                }

I tried saving the file as text under any encoding format with no success.

What I have tried:

I tried saving the file as text under any encoding format with no success.
Posted
Updated 6-Aug-20 2:57am
v3

When I try it:
string s = "\u003cp\u003eparagraph text 1\u003c/p\u003e";
string x = "<p>paragraph text 1</p>";
string json1 = JsonConvert.SerializeObject(s);
string json2 = JsonConvert.SerializeObject(x);
I get perfectly respectable json from both strings:
"<p>paragraph text 1</p>"
Even if I assume that your string is a literal:
string s = @"\u003cp\u003eparagraph text 1\u003c/p\u003e";
string json1 = JsonConvert.SerializeObject(s);
Then I still get exactly what I expect
as JSON:
"\\u003cp\\u003eparagraph text 1\\u003c/p\\u003e"
Since '\' is a JSON special character and needs to be escaped.

So ... I'd look closely at the code that generates your JSON and the data it is working from - I'd suspect that your "editedParagraph" string is originating with some form of HTML encoding applied, so that's why you get unicode characters in your output.

Use the debugger and have a close look at what is going on.
   
v2
Comments
oronsultan 6-Aug-20 4:56am
   
Dear Friend,
Thank you very much for your quick reply.
Pls, this is the drill down:
First, I create a 'Field' entity:
Field f = new Field();
Than, I initialize the properties. One of them is called 'editedParagraph':
f1.editedParagraph = "paragraph text 1";
After finishing initializing all of the properties, I save the entity as a json file:
string destination = "c:\\...\Output.Json"
var json = new JavaScriptSerializer().Serialize(f);
File.WriteAllText(destination, json);
I need the 'editedParagraph' to be saved as 'paragraph text 1' and not '"\\u003cp\\u003eparagraph text 1\\u003c/p\\u003e"'
OriginalGriff 6-Aug-20 5:31am
   
Then don't use JavaScriptSerializer then!
It's designed to do that: '<' and '>' are significant characters in HTML on which Javascript depends - so it is intended to replace them with the Unicode character representations so that the string can be passed from the server to the client Javascript via HTML. If it didn't replace them, they - and the data between them - would be "swallowed" by the browser HTML render before JS could get anywhere near them!

If you don't want to talk to JS, use NewtonSoft or similar to generate "generic" JSON instead of JS specific JSON.
OriginalGriff,
Thank you very much for drawing my attention to the problem. I seem to have been chasing my own tail all this time.
Instead of using
JavaScriptSerializer
, I did this and now it works ok:
var json = Newtonsoft.Json.JsonConvert.SerializeObject(main);
File.WriteAllText(destination, json);


One important thing:
I have an override method for
WriteJson
(inherit
JsonConverter
class)
and i hade to modify it:
public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)
        {
            var value = (DefaultValue)untypedValue;
            if (value.Bool != null)
            {
                serializer.Serialize(writer, value.Bool.Value);
                return;
            }
            if (value.String != null)
            {
                serializer.Serialize(writer, value.String);
                return;
            }
            throw new Exception("Cannot marshal type DefaultValue");
        }
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900