c# - How to avoid replacing space for "_x0020_"? -


i using wcf , of methods return class that, when converted json, generate object this:

{     "__type": "data:#mynamespace.model"     "dado_x0020_1": "1"     "dado_x0020_2": "2"     "dado_x0020_3": "3" } 

i can remember wasn't before, wcf wouldn't replace space character "_x0020_". problem have no idea changed in code make happen. don't recall changing configuration cause this. ideas?

this code of class. it's way allow object variable attribute names , 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; }                         {                 if (attributes.containskey(index))                     return attributes[index];                 else                     return null;             }         }     } } 

it wcf's datacontractjsonserializer adds _x0020_ characters. need use different json serializer (like json.net) if want use output else wcf communication.

if changed data class 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; }                 {             if (attributes.containskey(index))                 return attributes[index];             else                 return null;         }     } } 

then following wcf serialisation:

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 produce result:

{   "attributes": [     {       "key": "dado 1",       "value": 1     },     {       "key": "dado 2",       "value": 2     },     {       "key": "dado 3",       "value": 3     }   ] } 

but still don't think of use outside of the wcf context.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -