John
John

Reputation: 4601

deserialize json using construtor with servicestack

I have an immutable object that I serialized into json using servicestack's framework. Now I want to deserialize this json string into real object. But my object is immutable.

Is there a way to tell servicestack json deserializer to use the constructor ? I don't want to add a public set in my properties.

I find a way to do it using Newtonking Json Deserializer, but is there a equivalent in servicestack ? http://james.newtonking.com/projects/json/help/index.html?topic=html/T_Newtonsoft_Json_JsonConstructorAttribute.htm

Thanks

Upvotes: 1

Views: 340

Answers (1)

Mike Mertsock
Mike Mertsock

Reputation: 12015

I'm not aware of a simple config that will enable this, but you could write a custom deserializer easily enough:

JsConfig<MyRequestClass>.DeSerializeFn = jsonString => {
    var obj = JsonObject.Parse(jsonString);
    return new MyRequestClass(obj.Get("PropertyA"), obj.Get("PropertyB"), ...);
}

See this related answer for more info.

Upvotes: 2

Related Questions