2

I have a question about serialization with JSON.NET and working with a WCF REST webservice.

My device which runs the .NET compact framework and has limited feature support uses JSON.NET for serialization/deserialization. Communication via HttpWebRequest to simple WCF REST webservice with parameters like strings or simple classes work like a charm.

Now i have to transmit a collection of key value pairs

List<KeyValuePair<int, object>> 

The result of serializing a collection with one element containing a Guid as value looks like this:

[ { "Key": 52120, "Value": "180047f8-2ee5-41bb-8da1-68bd94178d79" }, ] 

I think that is correct json formatting but if i look at my WCF service it requires me to send the data like:

[{ "key":2147483647, "value":{ "KeyValuePairOfintanyType":{ "key":2147483647, "value":{ "KeyValuePairOfintanyType":null } } } }] 

Sending the data serialized by JSON.NET results in connection abort. Does anyone have an idea how to serialize the generic List to fit the requirements of the WCF service? Is there a way to tell JSON.NET to insert those KeyValuePairOf... format?

2
  • Just wondering - do you have to declare your collection as List<KeyValuePair<int, object>>? Also, where does the GUID value show up in the JSON that WCF is expecting? Commented Feb 20, 2013 at 13:08
  • @DavidHoerster The Guid will be passed as "object" of the KeyValuePair which is the value which is ""KeyValuePairOfintanyType":null". There is no special reason to use List<T> but i need a collection of key value pairs. I thought to create a struct without using generics but i want to keep the interface flexible. Commented Feb 20, 2013 at 15:42

1 Answer 1

1

use DataContractJsonSerializer wcf use it for rest and they are full compatible

Sign up to request clarification or add additional context in comments.

3 Comments

sadly this is not possible because it is not available in the compact framework.
or you can use restsharp, it has own simular serialization but can't use DataContract attribute Name
Sorry but that's not an option to "copy&paste" disassembled source code into our projects. I already tried serializing the same object with DataContractJsonSerializer in .NET 4.0 and the output is not the same as the WCF service requires which confuses me.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.