MatteoSp
MatteoSp

Reputation: 3048

How to totally control json serialization in WCF

I need to totally control the json serialization process in my Rest WCF service. I need to substitute the serialization result, that is something similar to:

{ foo: 42, bar: 43 }

with:

myFunc( { foo: 42, bar: 43 } );

any ideas?

thanks m.

Upvotes: 0

Views: 1148

Answers (2)

Bryan
Bryan

Reputation: 1441

I had a similar issue previously, which I solved by returning a memory stream from a WCF service. once you've done that you can set the MIME type manually. this basically allows you to return any result as any MIME type. I think I used this for jsonp. Sorry, but I'm working off my iPad at the moment so I can't provide an example. A quick google search should get you what you need.

Also, I'd recommend using json.net for your serialization...it's much easier to work with than the standard .net serialized.

Upvotes: 3

Mikael Svenson
Mikael Svenson

Reputation: 39695

I think you need to create your own serializer. You inherit from DataContractSerializerOperationBehavior and override CreateSerializer.

You can see a sample of how it's done in protobuf.net.

It might be simpler to expand your output object to include a parameter which is the name of the calling function, and then invoking it on the callback on your web page.

{ "func": "myFunc", "foo": "42", "bar": "43" }

Other helpful links:

Upvotes: 0

Related Questions