Skip to content

JTokenExtensions

Rob edited this page Aug 4, 2026 · 11 revisions

JTokenExtensions Class

Extensions which extends QuickJSON with the ability to create c# objects from JTokens
An JToken can be converted to a c# object if (in order) its:

  • It has [JsonCustomFormat] or [JsonCustomFormatArray] marked on it making it convertable using customconverter callback
  • Or the class has a constructor taking a JToken - class-constructor(JToken)
  • A simple c# value type
  • Or it is an enumeration or DateTime and you've defined process callback which allows for custom processing of these strings to enumeration/datetime
  • Or it is an enumeration or DateTime
    Can convert JArrays to classes implementing IEnumeration such as Lists/Arrays/Hashsets
    Can convert JObjects to dictionaries. For dictionaries, the key must be either:
  • a string
  • an enumeration or DateTime
  • an enumeration or DateTime which is passed thru process callback to convert to a enumeration/DateTime
  • A class having a constructor taking a single string parameter
  • A class implementing IConvertible which can convert a string to it (such as DateTime)
public static class JTokenExtensions

Inheritance System.Object 🡒 JTokenExtensions


Methods


JTokenExtensions.ClearToObjectCache() Method

Clear ToObject convert cache

public static void ClearToObjectCache();


JTokenExtensions.ToObject<T>(JToken, bool, Func<Type,Type,string,object>, string, Func<MemberInfo,object,object>) Method

Convert the JToken tree to an object of type T. Protected from all exception sources

public static T ToObject<T>(this QuickJSON.JToken token, bool ignoretypeerrors=false, System.Func<System.Type,System.Type,string,object> process=null, string setname=null, System.Func<System.Reflection.MemberInfo,object,object> customconverter=null);
Type parameters

T
Convert to this type

Parameters

token JToken
JToken to convert from

ignoretypeerrors System.Boolean
Ignore any errors in type between the JToken type and the member type.

process System.Func<System.Type,System.Type,System.String,System.Object>
For enums and datetime, and if set, process and return enum or DateTime. First type is type wanted, second type is only for dictionaries is the dictionary type

setname System.String
Define set of JSON attributes to apply, null for default

customconverter System.Func<System.Reflection.MemberInfo,System.Object,System.Object>
Class members convert to object using this function if marked with [JsonCustomFormat]. Return value (incl null) or ToObjectError object

Returns

T
New object T containing fields filled by JToken, or default(T) on error.


JTokenExtensions.ToObject(JToken, Type, bool, BindingFlags, object, Func<Type,Type,string,object>, string, Func<MemberInfo,object,object>) Method

Convert the JToken tree to an object of type. This member will except.

public static object ToObject(this QuickJSON.JToken token, System.Type converttype, bool ignoretypeerrors, System.Reflection.BindingFlags membersearchflags=System.Reflection.BindingFlags.Instance|System.Reflection.BindingFlags.Static|System.Reflection.BindingFlags.Public, object initialobject=null, System.Func<System.Type,System.Type,string,object> process=null, string setname=null, System.Func<System.Reflection.MemberInfo,object,object> customconverter=null);
Parameters

token JToken
JToken to convert from

converttype System.Type
Type to convert to

ignoretypeerrors System.Boolean
Ignore any errors in type between the JToken type and the member type.

membersearchflags System.Reflection.BindingFlags
Member search flags, to select what types of members are serialised.
Use the default plus DeclaredOnly for only members of the top class only

initialobject System.Object
If null, object is newed. If given, start from this object. Will except if it and the converttype is not compatible.

process System.Func<System.Type,System.Type,System.String,System.Object>
For enums and datetime, and if set, process and return enum or DateTime. First type is type wanted, second type is only for dictionaries and is the dictionary type

setname System.String
Define set of JSON attributes to apply, null for default

customconverter System.Func<System.Reflection.MemberInfo,System.Object,System.Object>
Class members convert to object using this function if marked with [JsonCustomFormat]. Return value (incl null) or ToObjectError object

Returns

System.Object
Object containing fields filled by JToken, or a object of ToObjectError on named error, or null if no tokens

Exceptions

System.Exception
Generic exception System.InvalidCastException
If a type failure occurs. Other excepections could also occur.


JTokenExtensions.ToObjectProtected(JToken, Type, bool, BindingFlags, object, Func<Type,Type,string,object>, string, Func<MemberInfo,object,object>) Method

Convert the JToken tree to an object of type. Protected from all exception sources

public static object ToObjectProtected(this QuickJSON.JToken token, System.Type converttype, bool ignoretypeerrors, System.Reflection.BindingFlags membersearchflags=System.Reflection.BindingFlags.Instance|System.Reflection.BindingFlags.Static|System.Reflection.BindingFlags.Public, object initialobject=null, System.Func<System.Type,System.Type,string,object> process=null, string setname=null, System.Func<System.Reflection.MemberInfo,object,object> customconverter=null);
Parameters

token JToken
JToken to convert from

converttype System.Type
Type to convert to

ignoretypeerrors System.Boolean
Ignore any errors in type between the JToken type and the member type.

membersearchflags System.Reflection.BindingFlags
Member search flags, to select what types of members are serialised

initialobject System.Object
If null, object is newed. If given, start from this object. Will except if it and the converttype is not compatible.

process System.Func<System.Type,System.Type,System.String,System.Object>
For enums and datetime, and if set, process and return enum or DateTime. First type is type wanted, second type is only for dictionaries and is the dictionary type

setname System.String
Define set of JSON attributes to apply, null for default

customconverter System.Func<System.Reflection.MemberInfo,System.Object,System.Object>
Class members convert to object using this function if marked with [JsonCustomFormat]. Return value (incl null) or ToObjectError object

Returns

System.Object
Object containing fields filled by JToken, or a object of ToObjectError on named error, or null if no tokens


JTokenExtensions.ToObjectQ<T>(JToken) Method

Convert the JToken tree to an object of type T.
Type errors are not ignored

public static T ToObjectQ<T>(this QuickJSON.JToken token);
Type parameters

T
Convert to this type

Parameters

token JToken
JToken to convert from

Returns

T
New object T containing fields filled by JToken, or default(T) on error


Classes

Classes
JTokenExtensions.ToObjectError Class to hold an conversion error

Clone this wiki locally