How to create JsonConverter with ServiceStack?

I have some pre defined POCO’s using Newtonsoft.Json attributes and converters.

It seems that I have to replace
[JsonProperty("messenger_extensions", NullValueHandling = NullValueHandling.Ignore)]

with

[DataMember(Name = "messenger_extensions")]

Correct? Or is there a way to use the JsonProperty attributes?

Furthermore, I have

[JsonProperty("attachments", ItemConverterType = typeof(AttachmentConverter))]

Which basically checks a certain value in the json and then returns another type of Attachment in this case.

Is there any way to use this in ServiceStack services?

No ServiceStack doesn’t support JSON.NET’s [JsonProperty] or have any property-level attribute converters of its own.

The way to customize how JSON is serialized/deserialized is by using the JsConfig class with the SerializeFn/DeSerializeFn and RawSerializeFn/RawDeserializeFn whilst the OnSerializingFn/OnSerializedFn provides hooks that get called during serialization.

Your Classes can also implement ToJson() method to override the JSON that gets used for each type, here’s an example of both approaches:

Okay thanks.
In the end I found a better solution for this existed attributed poco’s:
http://docs.servicestack.net/serialization-deserialization#create-a-custom-request-dto-binder

This way I could, for this service, use the existing NewtonSoft.Json formatters.

1 Like