I would like to be specific in parameter types or write something like this:
Test test = (Json<Test>)"{\"age\": 46}";
Where:
public class Test
{
public int Age { get; set; }
}
My helper is:
public struct Json<T>
{
public static implicit operator string(Json<T> json) => json.JObject.ToString();
public static explicit operator Json<T>(string json) => new Json<T>(json);
public static implicit operator T(Json<T> json) => json.JObject.ToObject<T>();
public static implicit operator Json<T>(T obj) => new Json<T>(JObject.FromObject(obj));
static readonly JObject Default = JObject.FromObject(new { });
Json(string json) : this(JObject.Parse(json)) { }
Json(JObject jObject) : this() => _jObject = jObject ??
throw new ArgumentNullException(nameof(jObject));
JObject JObject => _jObject ?? Default;
readonly JObject _jObject;
}