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<T>(string)
Deserializes a json string to an object.
public static T Deserialize<T>(string data)
Parameters
data
string- Json data to deserialize to object
Returns
- T
- object
Type Parameters
T
- Type of object
Deserialize<T>(string, JsonSettings)
Deserializes a json string to an object.
public static T Deserialize<T>(string data, JsonSettings settings)
Parameters
data
string- Json data to deserialize to object
settings
JsonSettings- Settings used when object was serialized
Returns
- T
- object
Type Parameters
T
- Type of object
DeserializeCompact<T>(string)
Deserializes a json string to an object. Expects computer-readable and compacted json.
public static T DeserializeCompact<T>(string data)
Parameters
data
string- Json data to deserialize to object
Returns
- T
- object
Type Parameters
T
- Type of object
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
value
Guid- 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
value
Guid- The 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
value
object- The value.
Returns
- bool
true
if the specified value is a guid; otherwise,false
.
Serialize<T>(T)
Serializes the object to json.
public static string Serialize<T>(T obj)
Parameters
obj
T- Object to convert to string
Returns
- string
- serializedJson
Type Parameters
T
- Type of object
Serialize<T>(T, JsonSettings)
Serializes the object to json.
public static string Serialize<T>(T obj, JsonSettings settings)
Parameters
obj
T- Object to convert to string
settings
JsonSettings- Settings for serializing the object
Returns
- string
- serializedJson
Type Parameters
T
- Type of object
SerializeCompact<T>(T)
Serializes the object to json. Computer-readable and compacted for storage purposes.
public static string SerializeCompact<T>(T obj)
Parameters
obj
T- Object to convert to string
Returns
- string
- serializedJson
Type Parameters
T
- Type of object
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
value
object- Boolean 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
value
string
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.ToBooleanLegacy(object)
[Obsolete("Use ToBoolean instead")]
public static bool ToBooleanLegacy(object value)
Parameters
value
object
Returns
ToBooleanLegacy(string)
[Obsolete("Use ToBoolean instead")]
public static bool ToBooleanLegacy(string value)
Parameters
value
string
Returns
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
value
long- 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
value
object- The 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
value
object- The object to parse.
defaultValue
DateTime- 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.
ToDateTimeLegacy(object)
Determines whether or not an object is 'something' (as opposed to nothing), and
if the object type is a date. If so, the function returns the value of the date
object. In all other cases Now is returned.
[Obsolete("Use ToDateTime instead")]
public static DateTime ToDateTimeLegacy(object value)
Parameters
value
object
Returns
- DateTime
- The value of a valid date object (
value
). In all other cases Now.
ToDecimal(object)
Parses the given object into a decimal if possible.
public static decimal ToDecimal(object value)
Parameters
value
object- The 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
value
object- The 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
value
object- The value.
Returns
ToInt32(object)
Parses the given object into an integer if possible.
public static int ToInt32(object value)
Parameters
value
object- The 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
value
object- The 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
value
object- Boolean 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
value
object- The object to parse.
defaultValue
DateTime?- 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
value
object- The 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
value
object- The 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
value
object- The 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
value
object- The object to parse.
Returns
- long?
- The long represented by the given object; the null is returned if the value cannot be parsed.
ToString(object)
Parses an object into a the string that it represents. If
null
is passed, the empty string is returned.public static string ToString(object value)
Parameters
value
object- The 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
.
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
value
int- Enum value
result
T- Resulting 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
value
string- Enum value
result
T- Resulting enum
Returns
- bool
- isValidEnumValue
Type Parameters
T