Implicit conversion from JToken in Json.NET

c#, json.net, type-conversion

Solution

The reason for explicit operators is that implicit operators cause all sorts of problems. So, no, you can't do it, it's by design.

However, in addition to explicit cast, you can also get `Value` property:

if (parsed.ShouldDoStuff.Value)
    Console.WriteLine("Die you gravy-sucking pigs");

I think it's cleaner than type cast.

Problem

Using Json.NET, I see that all of the conversions of native types to `JToken` are implicit, but conversions from `JToken` are explicit. My motivation is to avoid the explicit casts in `if` statements, method calls etc. For example, it would have been nice if the last `if` did not throw: ``` string dummyJson = @"{'ShouldDoStuff': true}"; dynamic parsed = JValue.Parse(dummyJson); // Works: bool explicitShouldDoStuff = parsed.ShouldDoStuff; // Also works: if ((bool)parsed.ShouldDoStuff) Console.WriteLine("Hooray, there's a rosebush?"); // Throws RuntimeBinderException: Cannot implicitly convert type 'Newtonsoft.Json.Linq.JValue' to 'bool' if (parsed.ShouldDoStuff) Console.WriteLine("Die you gravy-sucking pigs"); ``` Is there a way to make conversions from `JToken` to native types implicit as well?

Original source