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

StackOverflow https://stackoverflow.com/questions/20857432

문제

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.

도움이 되었습니까?

해결책

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

다른 팁

If you look at the source code here and there, both method use jsonSerializer. So it should be the exact same.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top