JSon.Net JObject.FromObject Vs JsonConvert.DeserializeObject<JObject>(JsonConvert.SerializeObject(obj));

maicalal picture maicalal · Dec 31, 2013 · Viewed 8.3k times · Source

I tried looking for the above comparison but couldn't find an answer.

As there are multiple ways to get a JObject (or all child types inheriting from JToken) eg:

Method1

. JObject.FromObject(obj);

Method2

. JsonConvert.DeserializeObject<JObject>(JsonConvert.SerializeObject(obj));

would Method1 perform better than Method2 ?

My usecase is related to backing up a set of entities into a text file and restoring it back.

Answer

James Newton-King picture James Newton-King · Jan 2, 2014

This is faster:

JObject.FromObject(obj);

It builds a JObject directly from the object. The other method serializes the object to JSON a JSON string and then parses the JSON to build a JObject.

Documentation: JObject.FromObject