zer0ne
zer0ne

Reputation: 423

Easiest way to serialize and deserialize a JSON class that has interface properties?

I have a

class Product
{
    public IComponent[] components;
}

What would be the easiest way to deserialize a Product object from some JSON description like

{
    "components": [
        {"type":"ComponentA", "key":value}
        {"type":"ComponentB", "key":value}
    ]
}

?

Thanks.

Upvotes: 0

Views: 456

Answers (1)

I4V
I4V

Reputation: 35353

Use Json.Net

string json = JsonConvert.SerializeObject(product);

or use JavaScriptSerializer

string json = new JavaScriptSerializer().Serialize(product);

And the reverse operation

var product = JsonConvert.DeserializeObject<Product>(json)

or

var product = new JavaScriptSerializer().Deserialize<Product>(json)

Upvotes: 2

Related Questions