0

Say I need to build a json string from pairs (e.g., coming from a database where the column name is the key).

Is it better/faster to:

a) create a JObject and add the <string, JToken> pairs (getting the tokens with the JToken.FromObject) ; at the end call the JObject.ToString() method (here is it better to use the Add method or the indexer set?)

b) create a Dictionary and add the <string, object> pairs; at the end call the JsonConvert.SerializeObject passing the dictionary

c) Any other way?

I'm worried about performance since this will be used multiple times per second.

Thanks in advance.

André Mantas
  • 482
  • 4
  • 13
  • I've personally used the code found on that answer many times to answer questions just like this. Give it a go, and if you have problems, [edit] to add details for your benchmarks and @me to reopen. –  Apr 07 '17 at 18:11
  • 1
    See https://ericlippert.com/2012/12/17/performance-rant/ – dbc Apr 07 '17 at 18:16

0 Answers0