1

I need to make a post request to a wcf rest service (4.0). This service will be called from both client-side apps(jquery) and server side apps (asp.net etc). The service is exposing a persistent store and provides CRUD operations.

I have an issue with serialzation.

I read a few articles related to this -> http://blogs.msdn.com/b/youssefm/archive/2009/04/21/understanding-known-types.aspx

public class CustomType
{
    public Dictionary<string,object> CustomObjectCollection {get; set;}
     public SavedBy {get; set;}     
     public SavedOn {get; set;}     


    public CustomType()
    {
        CustomObjectCollection = new Dictionary<string,object>();
    }
}


public class State1
{
    public prop1 {get; set;}
    public prop2 {get; set;}
}

public class State2
{
    public prop1 {get; set;}
    public prop2 {get; set;}
}

Client consuming WCF :

public void save()
{
    CustomType req = new CustomType();
    req.CustomObjectCollection("State1", new State1() {prop1 = val1, prop2 = val2 } ); 
        req.CustomObjectCollection("State2", new State2() {prop1 = val3, prop2 = val4 } ); 

    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(CustomType));
        MemoryStream ms = new MemoryStream();
        serializer.WriteObject(ms, setting);

        string json = Encoding.Default.GetString(ms.ToArray());
        ms.Close();

    Utility.HTTPRequest("http://localhost:2222/MyWCFRestSVC/save", "post", json);

}

WCF Method :

public bool Save(CustomType req)
{
 /////////
}

I may not be able to use the "known types" work-around since every client that consumes this service will use its own custom types. Ideally the service should not worry about the types the clients wants to persist. Note that the service is exposing a persistent store which just holds generic state information.

I have tried Dictionary instead of Dictionary where I store json string representations os State1 & State2 as values. The problem I ran into was that when I used DataContractJsonSerializer to serialize the CustomType instance the json strings for State1 & State2 got backslashes(escape characters) embedded into them, due to double serialization I guess ?

I had thoughts about custom serialization instead of DataContractJsonSerializer.

Please let me know your thoughts.

Thanks for your time.

StudentForever
  • 631
  • 2
  • 9
  • 16

3 Answers3

1

Since the service is obviously not using the Dictionary in a way that it was intended maybe you should look at a different data structure?

By "in a way that it was intended", what I mean is for quick look ups. That is a hash table. You're simply collecting a bunch of information into it only to push it out to your client applications. So what you need is

List<string, List<object>>

Also make sure your "object" is serializeable as well.

Shiv Kumar
  • 9,599
  • 2
  • 36
  • 38
  • Thanks. I do not think its anything to do with the dictionary. Even if I had this property : `public object State {get;set;}' I will not be able to use DataContractJsonSerializer without specifying 'known types', when I use type State1 to set State property, since it does not know the type info to serialize. Any thoughts ? – StudentForever Mar 02 '11 at 14:19
0

I asked a similar question a while ago. No direct answer, but we came up with a workaround:

Serializing IDictionary<string, object> in WCF

Community
  • 1
  • 1
Mike Chamberlain
  • 39,692
  • 27
  • 110
  • 158
  • Thanks, do you mind providing more details on how you replaced the Dictionary to Dictionary. Also, please see my response to Shiv's answer. – StudentForever Mar 02 '11 at 15:37
  • I added another post related to this : http://stackoverflow.com/questions/5171925/serializing-custom-type-with-json-string-as-property-is-adding-escape-sequences – StudentForever Mar 02 '11 at 20:12
  • I think you made a typo - we placed Dictionary Dictionary. Our objects were all of basic types (string, int, DateTime, string[] etc.) The string[] object was causing the issue. So we implemented some simple custom serialization, which basically called ToString() for most types, and for string[] (which was the problem for us) we cam up with a custom string representation. – Mike Chamberlain Mar 02 '11 at 23:18
  • Thanks, yes I meant Dictionary to Dictionary. I might have to deal with more custom types which could be things like classes from thrid party controls etc. so might be a little tricky. – StudentForever Mar 03 '11 at 14:33
0

When I have needed to transport Dictionary over WCF / web services in the past, I have generally subclassed from Dictionary and then implemented the IXmlSerializable interface - i.e. ReadXml(), WriteXml()

This allows control over the format of the message over the wire and, if you have knowledge of the type of data you will be transmitting, can result in more efficient means of serialization.

DaveRead
  • 3,371
  • 1
  • 21
  • 24