Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I Dump() a Newtonsoft JObject in LinqPad?

Tags:

In LinqPad, trying to call .Dump() on a Newtonsoft JSON.Net JObject yields an exception:

RuntimeBinderException: 'Newtonsoft.Json.Linq.JObject' does not contain a definition for 'Dump'.

This works for almost everything else in LinqPad. I'd like to figure out a method that will Dump out a Newtonsoft JObject, just like other objects, showing property names, values, etc.

I've already figured out how to get it to dump the JSON string, but I'd like to see an object get output rather than just a text string.

like image 577
Ken Mason Avatar asked Jan 18 '13 20:01

Ken Mason


People also ask

What is dump in Linqpad?

The single most powerful feature of linqpad is the Dump() extension method that you can call on any object. It will analyze the object graph and use its fine-tuned heuristics to present the object's data in the most efficient way possible. It can also render bitmaps, xml data, and even WPF and WinForms controls!

What is JObject?

JObject. It represents a JSON Object. It helps to parse JSON data and apply querying (LINQ) to filter out required data. It is presented in Newtonsoft.

What is Newtonsoft JSON LINQ?

LINQ to JSON is an API for working with JSON objects. It has been designed with LINQ in mind to enable quick querying and creation of JSON objects. LINQ to JSON sits under the Newtonsoft. Json.


2 Answers

For anyone who lands here wanting to get pretty LINQPad output from a JSON string, deserializing to ExpandoObject is an effective approach and works recursively down any hierarchies that may be in the data:

JsonConvert.DeserializeObject<ExpandoObject>(myJSONString).Dump();

Extending that to cover the actual question, an extension method on JObject along these lines would do the trick:

public static class ExtMethods {     public static JObject DumpPretty(this JObject jo)     {         var jsonString = JsonConvert.SerializeObject(jo);         JsonConvert.DeserializeObject<ExpandoObject>(jsonString).Dump();          return jo;  // return input in the spirit of LINQPad's Dump() method.     } } 

Not the most efficient method, but for quick use when digging around in LINQPad it will do the trick.

like image 138
rdavisau Avatar answered Sep 22 '22 07:09

rdavisau


It's a static extension method, so you can call it as a static method:

LINQPad.Extensions.Dump(jObject); 

I see that happen on some types when (I presume) the compiler isn't able to bind to the extension for some reason.

There's a post on LinqPad 's site and a blog post regarding using Dump() with dynamic objects.

You might try creating another Dump() extension that examines the properties of JObject and creating a Dictionary that can be Dumped prettily.

Something like this: (complete WAG based on JObject's definition):

var values = jObject.Properties.ToDictionary(p=>p.Name, p=>p.Value); values.Dump(); 

of course you could add recursion for nested objects, etc.:

//Usage: GetProperties(jObject).Dump(); public static object GetProperties(object o) {     JObject j = o as JObject;     if(j == null)     {         return o.ToString();     }     return j.Properties().ToDictionary(p=>p.Name,p=>GetProperties(p.Value)); } 
like image 43
D Stanley Avatar answered Sep 21 '22 07:09

D Stanley