Class Converter
- Namespace
- Dynamicweb.Core
- Assembly
- Dynamicweb.Core.dll
The Converter class provides methods for converting objects into other types. All methods are guaranteed to return a valid default type.
public static class Converter
- Inheritance
-
Converter
- Inherited Members
Methods
Deserialize(string, Type)
Deserializes a JSON string to an object instance of the specified type using the default serializer settings.
public static object? Deserialize(string data, Type type)
Parameters
Returns
- object
An object instance of the specified type if deserialization succeeds; otherwise,
null.
Exceptions
- ArgumentNullException
Thrown if
typeisnull.- ConverterException
Thrown if the JSON string cannot be deserialized to the specified type.
DeserializeCompact(string, Type)
Deserializes a compact (non-indented) JSON string to an object instance of the specified type using the compact serializer settings.
public static object? DeserializeCompact(string data, Type type)
Parameters
datastringThe compact JSON string to deserialize.
typeTypeThe type of the object to deserialize to.
Returns
- object
An object instance of the specified type if deserialization succeeds; otherwise,
null.
Exceptions
- ArgumentNullException
Thrown if
typeisnull.- ConverterException
Thrown if the JSON string cannot be deserialized to the specified type.
DeserializeCompact<T>(string)
Deserializes a compact (non-indented) JSON string to an object instance of the specified type using the compact serializer settings.
public static T? DeserializeCompact<T>(string data)
Parameters
datastringThe compact JSON string to deserialize.
Returns
- T
An object instance of type
Tif deserialization succeeds; otherwise,null.
Type Parameters
TThe type of the object to deserialize to.
Exceptions
- ConverterException
Thrown if the JSON string cannot be deserialized to the specified type.
Deserialize<T>(string)
Deserializes a JSON string to an object instance of the specified type using the default serializer settings.
public static T? Deserialize<T>(string data)
Parameters
datastringThe JSON string to deserialize.
Returns
- T
An object instance of type
Tif deserialization succeeds; otherwise,null.
Type Parameters
TThe type of the object to deserialize to.
Exceptions
- ConverterException
Thrown if the JSON string cannot be deserialized to the specified type.
GetGuid()
Gets a new guid object that is not Empty.
public static Guid GetGuid()
Returns
- Guid
A guid object.
GetGuidString()
Gets the string representation of a new guid object. The string does not contain dashes.
public static string GetGuidString()
Returns
- string
System.String.
GetGuidString(Guid?)
Gets the string representation of the given guid object. The string does not contain dashes.
public static string GetGuidString(Guid? value)
Parameters
valueGuid?The guid to get the string representation of.
Returns
- string
System.String.
GetGuidStringWithDashes()
Gets the string representation of a new guid object. The string contains dashes.
public static string GetGuidStringWithDashes()
Returns
- string
System.String.
GetGuidStringWithDashes(Guid)
Gets the string representation of the given guid object. The string contains dashes.
public static string GetGuidStringWithDashes(Guid value)
Parameters
valueGuidThe guid to get the string representation of.
Returns
- string
System.String.
IsGuid(object?)
Determines whether the specified value is a guid.
public static bool IsGuid(object? value)
Parameters
valueobjectThe value.
Returns
- bool
trueif the specified value is a guid; otherwise,false.
Serialize(object?, Type)
Serializes an object instance to a JSON string using the default serializer settings and the specified type.
public static string Serialize(object? obj, Type type)
Parameters
Returns
- string
A JSON string representing the object if serialization succeeds; otherwise, an empty string.
Exceptions
- ArgumentNullException
Thrown if
typeisnull.- ConverterException
Thrown if the object cannot be serialized to JSON.
SerializeCompact(object?, Type)
Serializes an object instance to a compact (non-indented) JSON string using the compact serializer settings and the specified type.
public static string SerializeCompact(object? obj, Type type)
Parameters
Returns
- string
A compact JSON string representing the object if serialization succeeds; otherwise, an empty string.
Exceptions
- ArgumentNullException
Thrown if
typeisnull.- ConverterException
Thrown if the object cannot be serialized to JSON.
SerializeCompact<T>(T?)
Serializes an object instance to a compact (non-indented) JSON string using the compact serializer settings.
public static string SerializeCompact<T>(T? obj)
Parameters
objTThe object instance to serialize.
Returns
- string
A compact JSON string representing the object if serialization succeeds; otherwise, an empty string.
Type Parameters
TThe type of the object to serialize.
Exceptions
- ConverterException
Thrown if the object cannot be serialized to JSON.
Serialize<T>(T?)
Serializes an object instance to a JSON string using the default serializer settings.
public static string Serialize<T>(T? obj)
Parameters
objTThe object instance to serialize.
Returns
- string
A JSON string representing the object if serialization succeeds; otherwise, an empty string.
Type Parameters
TThe type of the object to serialize.
Exceptions
- ConverterException
Thrown if the object cannot be serialized to JSON.
ToBoolean(object?)
Tries to parse/convert the object to a boolean and return the value of this result. If this isn't possible, false is returned instead.
public static bool ToBoolean(object? value)
Parameters
valueobjectBoolean object to determine validity for.
Returns
- bool
The value of a valid boolean object (
value). In all other casesfalse.
ToBoolean(string?)
Checks the input string for a true/false statement and returns it as a Boolean.
public static bool ToBoolean(string? value)
Parameters
valuestring
Returns
- bool
Returns a Boolean parsed from the input string.
Remarks
Method is case-insensitive.
If the input string is set to an empty string, null, "false", "0", "off" or "no" it returns false.
Otherwise true is returned.
ToDateTime(long?)
Parses an object to a DateTime if it represents a DateTime. The default value is returned if the object does not represent a DateTime.
public static DateTime ToDateTime(long? value)
Parameters
valuelong?The object to parse.
Returns
- DateTime
The DateTime represented by the given object; default(DateTime) if the object cannot be parsed.
ToDateTime(object?)
Parses an object to a DateTime if it represents a DateTime. The default value is returned if the object does not represent a DateTime.
public static DateTime ToDateTime(object? value)
Parameters
valueobjectThe object to parse.
Returns
- DateTime
The DateTime represented by the given object; default(DateTime) if the object cannot be parsed.
ToDateTime(object?, DateTime)
Parses an object to a DateTime if it represents a DateTime. The default value is returned if the object does not represent a DateTime.
public static DateTime ToDateTime(object? value, DateTime defaultValue)
Parameters
valueobjectThe object to parse.
defaultValueDateTimeThe default value for when the conversion fails
Returns
- DateTime
The DateTime represented by the given object; the default value if the object cannot be parsed.
ToDecimal(object?)
Parses the given object into a decimal if possible.
public static decimal ToDecimal(object? value)
Parameters
valueobjectThe object to parse.
Returns
- decimal
The decimal represented by the given object; the default value is returned if the value cannot be parsed.
ToDouble(object?)
Parses the given object into a double if possible.
public static double ToDouble(object? value)
Parameters
valueobjectThe object to parse.
Returns
- double
The double represented by the given object; the default value is returned if the value cannot be parsed.
ToGuid(object?)
Parses the object into a guid if possible.
public static Guid ToGuid(object? value)
Parameters
valueobjectThe value.
Returns
ToInt32(object?)
Parses the given object into an integer if possible.
public static int ToInt32(object? value)
Parameters
valueobjectThe object to parse.
Returns
- int
The integer represented by the given object; the default value is returned if the value cannot be parsed.
ToInt64(object?)
Parses the given object into a long if possible.
public static long ToInt64(object? value)
Parameters
valueobjectThe object to parse.
Returns
- long
The long represented by the given object; the default value is returned if the value cannot be parsed.
ToNullableBoolean(object?)
Tries to parse/convert the object to a boolean and return the value of this result. If this isn't possible, null is returned instead.
public static bool? ToNullableBoolean(object? value)
Parameters
valueobjectBoolean object to determine validity for.
Returns
- bool?
The value of a valid boolean object (
value). In all other casesfalse.
ToNullableDateTime(object?, DateTime?)
Parses an object to a DateTime if it represents a DateTime. The default value is returned if the object does not represent a DateTime.
public static DateTime? ToNullableDateTime(object? value, DateTime? defaultValue)
Parameters
valueobjectThe object to parse.
defaultValueDateTime?The default value for when the conversion fails
Returns
- DateTime?
The DateTime represented by the given object; the default value if the object cannot be parsed.
ToNullableDecimal(object?)
Parses the given object into a decimal if possible.
public static decimal? ToNullableDecimal(object? value)
Parameters
valueobjectThe object to parse.
Returns
- decimal?
The decimal represented by the given object; the null is returned if the value cannot be parsed.
ToNullableDouble(object?)
Parses the given object into a double if possible.
public static double? ToNullableDouble(object? value)
Parameters
valueobjectThe object to parse.
Returns
- double?
The double represented by the given object; the null is returned if the value cannot be parsed.
ToNullableInt32(object?)
Parses the given object into an integer if possible.
public static int? ToNullableInt32(object? value)
Parameters
valueobjectThe object to parse.
Returns
- int?
The integer represented by the given object; the null is returned if the value cannot be parsed.
ToNullableInt64(object?)
Parses the given object into a long if possible.
public static long? ToNullableInt64(object? value)
Parameters
valueobjectThe object to parse.
Returns
- long?
The long represented by the given object; the null is returned if the value cannot be parsed.
ToNullableString(object?)
Parses an object into a string that it represents. If null is passed, null is returned.
public static string? ToNullableString(object? value)
Parameters
valueobjectThe object to pass to a string.
Returns
- string
Returns the string representation of the given object; null if the given object is
null.
ToString(object?)
Parses an object into a string that it represents. If null is passed, the empty string is returned.
public static string ToString(object? value)
Parameters
valueobjectThe object to pass to a string.
Returns
- string
Returns the string representation of the given object; the empty string if the given object is
null.
TryDeserialize(string, Type, out object?)
Tries to deserialize a JSON string to an object instance of the specified type.
public static bool TryDeserialize(string data, Type type, out object? obj)
Parameters
datastringThe JSON string to deserialize.
typeTypeThe type of the object to deserialize to.
objobjectWhen this method returns, contains the deserialized object if successful; otherwise,
null.
Returns
- bool
trueif the JSON string could be deserialized to the specified type; otherwise,false.
TryDeserializeCompact(string, Type, out object?)
Tries to deserialize a compact (non-indented) JSON string to an object instance of the specified type.
public static bool TryDeserializeCompact(string data, Type type, out object? obj)
Parameters
datastringThe compact JSON string to deserialize.
typeTypeThe type of the object to deserialize to.
objobjectWhen this method returns, contains the deserialized object if successful; otherwise,
null.
Returns
- bool
trueif the compact JSON string could be deserialized to the specified type; otherwise,false.
TryDeserializeCompact<T>(string, out T?)
Tries to deserialize a compact (non-indented) JSON string to an object instance of the specified type.
public static bool TryDeserializeCompact<T>(string data, out T? obj)
Parameters
datastringThe compact JSON string to deserialize.
objTWhen this method returns, contains the deserialized object if successful; otherwise, the default value for the type.
Returns
- bool
trueif the compact JSON string could be deserialized to the specified type; otherwise,false.
Type Parameters
TThe type of the object to deserialize to.
TryDeserialize<T>(string, out T?)
Tries to deserialize a JSON string to an object instance of the specified type.
public static bool TryDeserialize<T>(string data, out T? obj)
Parameters
datastringThe JSON string to deserialize.
objTWhen this method returns, contains the deserialized object if successful; otherwise, the default value for the type.
Returns
- bool
trueif the JSON string could be deserialized to the specified type; otherwise,false.
Type Parameters
TThe type of the object to deserialize to.
TryParseEnum<T>(int, out T)
Converts the value to a enum value. Defaults to default value of enum, or first defined enum, if '0' is not a valid enum value.
public static bool TryParseEnum<T>(int value, out T result) where T : struct
Parameters
valueintEnum value
resultTResulting enum
Returns
- bool
isValidEnumValue
Type Parameters
T
TryParseEnum<T>(string?, out T)
Converts the value to a enum value. Defaults to default value of enum, or first defined enum, if '0' is not a valid enum value.
public static bool TryParseEnum<T>(string? value, out T result) where T : struct
Parameters
valuestringEnum value
resultTResulting enum
Returns
- bool
isValidEnumValue
Type Parameters
T
TrySerialize(object?, Type, out string?)
Tries to serialize an object instance to a JSON string using the specified type.
public static bool TrySerialize(object? obj, Type type, out string? data)
Parameters
objobjectThe object instance to serialize.
typeTypeThe type of the object to serialize.
datastringWhen this method returns, contains the JSON string if serialization succeeded; otherwise,
null.
Returns
- bool
trueif the object could be serialized to JSON; otherwise,false.
TrySerializeCompact(object?, Type, out string?)
Tries to serialize an object instance to a compact (non-indented) JSON string using the specified type.
public static bool TrySerializeCompact(object? obj, Type type, out string? data)
Parameters
objobjectThe object instance to serialize.
typeTypeThe type of the object to serialize.
datastringWhen this method returns, contains the compact JSON string if serialization succeeded; otherwise,
null.
Returns
- bool
trueif the object could be serialized to compact JSON; otherwise,false.
TrySerializeCompact<T>(T?, out string?)
Tries to serialize an object instance to a compact (non-indented) JSON string.
public static bool TrySerializeCompact<T>(T? obj, out string? data)
Parameters
objTThe object instance to serialize.
datastringWhen this method returns, contains the compact JSON string if serialization succeeded; otherwise,
null.
Returns
- bool
trueif the object could be serialized to compact JSON; otherwise,false.
Type Parameters
TThe type of the object to serialize.
TrySerialize<T>(T?, out string?)
Tries to serialize an object instance to a JSON string.
public static bool TrySerialize<T>(T? obj, out string? data)
Parameters
objTThe object instance to serialize.
datastringWhen this method returns, contains the JSON string if serialization succeeded; otherwise,
null.
Returns
- bool
trueif the object could be serialized to JSON; otherwise,false.
Type Parameters
TThe type of the object to serialize.