none
DataContracJsonSerializer and WriteObject when dealing with '/'s RRS feed

  • Question

  • I have the following function

     
     public static string Serialize<T>(T obj)
            {
                string returnVal = "";
                try
                {
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
                    using (MemoryStream ms = new MemoryStream())
                    {
                        serializer.WriteObject(ms, obj);
                       returnVal = Encoding.UTF8.GetString(ms.ToArray());
                    }
                }
                catch (Exception /*exception*/)
                {
                    returnVal = "";
                }
                return returnVal;
            }

    when obj contains a string field which contains a string with '/' s such as :

    "A4AAAAAAAAAAAAA///////"

    when WriteObject is called and later made into a string (with ToArray()) it becomes (when JSON)

    "A4AAAAAAAAAAAAA\/\/\/\/\/\/\/"

    How can I avoid this and make it that it does not add the '/'s for every '\'?

    Thursday, October 25, 2018 5:28 AM

All replies

  • Hi Robothito,

    I suggest you use the javascriptSerializer or Newtonsoft.Json, which could solve the problem gracefully.
    I have made a demo, wish it is useful to you.

    class Program
        {
            static void Main(string[] args)
            {
                Product p = new Product()
                {
                    Id = 1,
                    Name = "Apple////"
                };
                var result = Serialize<Product>(p);
                Console.WriteLine(result);
            }
            public static string Serialize<T>(T obj)
            {
                string result = "";
                try
                {
                    JavaScriptSerializer serializer = new JavaScriptSerializer();
                    result = serializer.Serialize(obj);
                    //JsonSerializer serializer = new JsonSerializer();
                    //StringBuilder sb = new StringBuilder();
                    //TextWriter tw = new StringWriter(sb);
                    //serializer.Serialize(tw, obj);
                    //result = sb.ToString();
                }
                catch (Exception)
                {
                    result = "";
                }
                return result;
            }
        }
        public class Product
        {
            public int Id { get; set; }
            public string Name { get; set; }
    }

    Result.

    https://i.stack.imgur.com/ULDGC.png

    Feel free to let me know if you have any questions.

    Best Regards

    Abraham

    Thursday, October 25, 2018 1:06 PM
    Moderator
  • Thank you very much for your reply.

    I have used JavaScriptSerializer in the past but stopped using because my application has to deal with optional fields. AFAIK, JavaScriptSerializer cannot manage those, but DataContractJsonSerializer can. I have never used Newtonsoft json. Can it deal with optional fields?

    And, does that mean that there is no way for DataCotractJsonSerializer to stop putting extra '/'s to its serialization??

    Friday, October 26, 2018 12:57 AM
  • Hi Robothito,

    You could directly replace the special characters with empty characters

    string newresult=result.Replace("\\","");
    Feel free to let me know if you have any questions
    Best Regards
    Abraham

    Friday, October 26, 2018 7:33 AM
    Moderator