views:

45

answers:

2

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.

+1  A: 

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:

Mikael Svenson
thanks. it's not me who need that "myFunc" parameter. It's the clientside framework I'm using (extJS) who need it. Basically I'm trying to putting together ExtJs and WCF/Rest. But they seem to be very different in the way they approach the REST/jSon stuffs...
MatteoSp
I added a couple of more links. Basically you need to implement your own serializer which includes the myFunc part. You might can inhert and extend the JavaScriptSerializer - http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx
Mikael Svenson
+1  A: 

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.

Bryan
yes, this is the solution finally i had to adopt too.
MatteoSp