How to use a custom JSON Serializer in Servicestack?

流过昼夜 提交于 2019-12-06 01:02:53

问题


I am wondering how you can use a custom JSON Serializer in ServiceStack. I am aware of the JsConfig.SerializeFn/DeSerializeFn but these seem to always 'JSON.stringify' the result of my custom Serializer.

I like to replace Serialization for the whole DTO. The endresult should be something like

{"Name":"Greg"}

and not

"{\"Name\":\"Greg\"}"

Is that possible?


回答1:


In terms of other hooks in ServiceStack's JSON Serializers there's also JsConfig<T>.RawSerializeFn and JsConfig<T>.RawDeserializeFn which should do what you need.

Otherwise if you just want to some pre/post processing there's also the JsConfig<T>.OnSerializingFn and JsConfig<T>.OnDeserializedFn custom hooks.




回答2:


Did you mean to use ServiceStack.Text.JsonSerializer.SerializeToString(object, typeof(object))? If you using it in a view in .net you might need to use Html.Raw(ServiceStack.Text.JsonSerializer.SerializeToString(object, typeof(object))).



来源:https://stackoverflow.com/questions/14387202/how-to-use-a-custom-json-serializer-in-servicestack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!