Using a different JSON Serializer?

496 views
Skip to first unread message

Badaro

unread,
May 26, 2011, 6:22:34 PM5/26/11
to ServiceStack .NET Open Source REST Web Services Framework
Is it possible to use a different JSON Serializer/Deserializer for
ServiceStack? I want to use JsonFX since it has support for C# 4.0
dynamic objects.

Demis Bellot

unread,
May 26, 2011, 7:00:57 PM5/26/11
to servic...@googlegroups.com
At the moment you can only switch between ServiceStack's JsonSerializer and the built-in JsonDataContractSerializer which you can do by adding this to the AppHost.Configure section:

SetConfig(new EndpointHostConfig {
UseBclJsonSerializers = true
});

Note: Even though the ServiceStack JsonSerializer supports serialization of late-bound (i.e. object) and anonymous types, I consider it an anti-pattern to define your DTOs using it.

You'll lose visibility and the ability to statically verify the external API of your web service (i.e. the most important interface/contract in your entire solution). Where you can only determine by inspecting the internal implementation of the web service implementation or inspecting the service by executing it at runtime. Either way you (and ServiceStack) will lose the intelligence about the contract of your API which means XSDs, WSDLs and auto-generated documentation pages are impossible to generate. Versioning/testing also becomes harder as you'll have no point-in-time idea of what requests existing clients are making and whether your changes will impact them.

I really need to write a blog post advising against this practice, as for some reason it seems like a popular approach (must be the recent cool attached to 'dynamic' keyword that seems to transcend best-practices web service development).

Martin Fowler has some good patterns and advice pertaining to remote service development which are covered in his highly regarded Patterns of Enterprise Application Architecture.
The relevant patterns to web services design include:


This best-practices advice which has stand the test of time is the inspiration behind ServiceStack and the development model it helps to promote.

Hope this explains things better.


Cheers,

Badaro

unread,
May 27, 2011, 11:15:10 AM5/27/11
to ServiceStack .NET Open Source REST Web Services Framework
I already have a working API with well-defined DTOs, the problem is
that I have a new requirement that the client must be able to select
which fields of the DTO it will receive. Dynamic objects seemed the
most reasonable way to do this.
> Application Architecture <http://www.martinfowler.com/books.html#eaa>.
> The relevant patterns to web services design include:
>
> http://martinfowler.com/eaaCatalog/dataTransferObject.htmlhttp://martinfowler.com/eaaCatalog/gateway.htmlhttp://martinfowler.com/eaaCatalog/remoteFacade.html

Badaro

unread,
May 27, 2011, 11:57:12 AM5/27/11
to ServiceStack .NET Open Source REST Web Services Framework
Are you willing to accept a patch adding a configuration option to use
a different JSON serializer? I just took a look at how
UseBclJsonSerializers is implemented, and it seems quite simple to add
this option, I certainly don't mind implementing it myself.
> >http://martinfowler.com/eaaCatalog/dataTransferObject.htmlhttp://mart...

Demis Bellot

unread,
May 27, 2011, 12:03:26 PM5/27/11
to servic...@googlegroups.com
That's fine, but the solution will need to be able be register a different JsonSerializer options without introducing a dependency on it.

Badaro

unread,
May 27, 2011, 3:57:19 PM5/27/11
to ServiceStack .NET Open Source REST Web Services Framework
My rough plan: Create an IJsonSerializer interface (with Serialize/
Deserialize methods), an IJsonSerializerFactory interface (with
instance creation methods) and add a setting in EndpointHostConfig
where you can specify a custom IJsonSerializerFactory.

Sounds good?

Demis Bellot

unread,
May 27, 2011, 4:38:20 PM5/27/11
to servic...@googlegroups.com
Well right idea, but too many specific artefacts and boiler plate for my liking.

Anyway I've just checked in ITextSerializer which is the only interface that needs implementing:

https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack.Interfaces/DesignPatterns/Serialization/ITextSerializer.cs

To register you just need to do:

JsonDataContractSerializer.UseSerializer(new JsonFxTextSerializer());

You can see what I mean by how it works:

Anyway this should be all the changes ServiceStack needs, which will be available in the next release.

Cheers,

Reply all
Reply to author
Forward
0 new messages