How to avoid replacing space for "_x0020_"?

I am using WCF and some of my methods return a class that, when converted to JSON, generates an object like this:

{
    "__type": "Data:#MyNamespace.Model"
    "Dado_x0020_1": "1"
    "Dado_x0020_2": "2"
    "Dado_x0020_3": "3"
}

      

I can clearly remember that this was not the case before, WCF did not replace the space character for "_x0020_". The problem is, I don't know what changed in my code to make this happen. I don't remember changing the config that would cause this. Any ideas?

This is the code for my class. It's just a way to resolve an object with variable attribute names and count:

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace MyNamespace.Model
{
    [Serializable]
    public class Data : ISerializable
    {
        internal Dictionary<string, object> Attributes { get; set; }

        public Data()
        {
            Attributes = new Dictionary<string, object>();
        }

        protected Data(SerializationInfo info, StreamingContext context)
            : this()
        {
            SerializationInfoEnumerator e = info.GetEnumerator();
            while (e.MoveNext())
            {
                Attributes[e.Name] = e.Value;
            }
        }

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            foreach (string key in Attributes.Keys)
            {
                info.AddValue(key, Attributes[key]);
            }
        }

        public void Add(string key, object value)
        {
            Attributes.Add(key, value);
        }

        public object this[string index]
        {
            set { Attributes[index] = value; }
            get
            {
                if (Attributes.ContainsKey(index))
                    return Attributes[index];
                else
                    return null;
            }
        }
    }
}

      

+3


source to share


1 answer


It is the WCF DataContractJsonSerializer that adds these characters _x0020_

. You need to use a different json serializer (like json.net) if you want to use the output for something else and then for WCF communication.

If you, however, changed your class Data

to something like this:

[DataContract]
public class Data
{
    public Data()
    {
        Attributes = new Dictionary<string, object>();
    }

    [DataMember]
    public Dictionary<string, object> Attributes { get; set; }

    [IgnoreDataMember]
    public object this[string index]
    {
        set { Attributes[index] = value; }
        get
        {
            if (Attributes.ContainsKey(index))
                return Attributes[index];
            else
                return null;
        }
    }
}

      

then the following WCF serialization:



class Program
{
    static void Main(string[] args)
    {
        var data = new Data();
        data["Dado 1"] = 1;
        data["Dado 2"] = 2;
        data["Dado 3"] = 3;

        var dcjs = new DataContractJsonSerializer(typeof(Data));
        MemoryStream stream1 = new MemoryStream();
        dcjs.WriteObject(stream1, data);
        stream1.Seek(0, SeekOrigin.Begin);
        var json3 = new StreamReader(stream1).ReadToEnd();
    }
}

      

will lead to this result:

{
  "Attributes": [
    {
      "Key": "Dado 1",
      "Value": 1
    },
    {
      "Key": "Dado 2",
      "Value": 2
    },
    {
      "Key": "Dado 3",
      "Value": 3
    }
  ]
}

      

but still I don't think it would be very useful outside of WCF context.

0


source







All Articles