API.Util.Conversion - JuDelCo/Core GitHub Wiki

Namespace: Ju.Data.Conversion

Cast (static class)

Allows you to convert data between types using default values if there is an exception trying to convert the type.

You can extend this static class with more extension methods to convert between any types.

Example usage:

var exampleData = "7";
Cast.This(exampleData).AsInt(10); // 10 as default value in case the conversion fails

Currently these are the built-in extension methods available:

// Cast.This(string)
bool AsBool(bool defaultValue = default(bool));
byte AsByte(byte defaultValue = default(byte));
sbyte AsSByte(sbyte defaultValue = default(sbyte));
char AsChar(char defaultValue = default(char));
float AsSingle(float defaultValue = default(float));
float AsFloat(float defaultValue = default(float));
double AsDouble(double defaultValue = default(double));
double AsFloat64(double defaultValue = default(double));
decimal AsDecimal(decimal defaultValue = default(decimal));
decimal AsFloat128(decimal defaultValue = default(decimal));
short AsShort(short defaultValue = default(short));
short AsInt16(short defaultValue = default(short));
int AsInt(int defaultValue = default(int));
int AsInt32(int defaultValue = default(int));
long AsLong(long defaultValue = default(long));
long AsInt64(long defaultValue = default(long));
ushort AsUShort(ushort defaultValue = default(ushort));
ushort AsUInt16(ushort defaultValue = default(ushort));
uint AsUInt(uint defaultValue = default(uint));
uint AsUInt32(uint defaultValue = default(uint));
ulong AsULong(ulong defaultValue = default(ulong));
ulong AsUInt64(ulong defaultValue = default(ulong));
T AsEnum<T>(T defaultValue = default(T)) where T : struct, Enum;
DateTime AsDateTimeFromUnixTimeStamp(DateTime defaultValue = default(DateTime));
Guid AsGuid(Guid defaultValue = default(Guid));
Color AsColor(Color defaultValue = default(Color));
Color32 AsColor32(Color32 defaultValue = default(Color32));

// Cast.This(Enum)
string AsString<T>() where T : Enum;
string AsStringLowercase<T>() where T : Enum;

// Cast.This(DateTime)
string AsString(string format = "yyyyMMdd_HHmmss");
double AsUnixTimeStamp();
string AsUnixTimeStampString();

// Cast.This(bool)
string AsString();
string AsStringLowercase();

// Cast.This(byte)
string AsString();

// Cast.This(sbyte)
string AsString();

// Cast.This(float)
string AsString();

// Cast.This(double)
string AsString();
DateTime AsDateTimeFromUnixTimeStamp();

// Cast.This(decimal)
string AsString();

// Cast.This(short)
string AsString();

// Cast.This(int)
string AsString();
string AsTimeStringFromSeconds();
string AsTimeStringFromSecondsShort();
DateTime AsDateTimeFromUnixTimeStamp();

// Cast.This(long)
string AsString();
string AsTimeStringFromSeconds();
string AsTimeStringFromSecondsShort();
DateTime AsDateTimeFromUnixTimeStamp();

// Cast.This(ushort)
string AsString();

// Cast.This(uint)
string AsString();

// Cast.This(ulong)
string AsString();

// Cast.This(char)
string AsString();

// Cast.This(Guid)
string AsString();

// Cast.This(Color)
string AsString(bool prependHashChar = true, bool toUpper = true);

// Cast.This(Color32)
string AsString(bool prependHashChar = true, bool toUpper = true);

// Cast.This(T) where T : struct, IComparable, IComparable<T>, IConvertible, IEquatable<T>, IFormattable
string AsFormattedBytesString(bool round = false);

DataTypeConverter (static class)

Allows you to convert data between types if there is an available converter method registered.

Used internally by the Data classes to serialize custom data to Hjson/Json (ToHjson & ToJson extension methods) and when using the SetRawData method (in case the set data type is different to the JData type).

Note: If the type can't be converted/serialized/deserialized the conversion will throw an Exception.

void AddConverter(ConversionType type, Func<object, object> converter);
bool HasConverter(ConversionType type);
Func<object, object> GetConverter(ConversionType type);
void RemoveConverter(ConversionType type);

This is the list of the available built-int converters, you can add or replace any converter as you need.

From Type To Type
bool byte
bool sbyte
bool float
bool double
bool decimal
bool short
bool int
bool long
bool ushort
bool uint
bool ulong
byte bool
byte sbyte
byte float
byte double
byte decimal
byte short
byte int
byte long
byte ushort
byte uint
byte ulong
sbyte bool
sbyte byte
sbyte float
sbyte double
sbyte decimal
sbyte short
sbyte int
sbyte long
sbyte ushort
sbyte uint
sbyte ulong
float bool
float byte
float sbyte
float double
float decimal
float short
float int
float long
float ushort
float uint
float ulong
double bool
double byte
double sbyte
double float
double decimal
double short
double int
double long
double ushort
double uint
double ulong
double DateTime
decimal bool
decimal byte
decimal sbyte
decimal float
decimal double
decimal short
decimal int
decimal long
decimal ushort
decimal uint
decimal ulong
short bool
short byte
short sbyte
short float
short double
short decimal
short int
short long
short ushort
short uint
short ulong
int bool
int byte
int sbyte
int float
int double
int decimal
int short
int long
int ushort
int uint
int ulong
long bool
long byte
long sbyte
long float
long double
long decimal
long short
long int
long ushort
long uint
long ulong
ushort bool
ushort byte
ushort sbyte
ushort float
ushort double
ushort decimal
ushort short
ushort int
ushort long
ushort uint
ushort ulong
uint bool
uint byte
uint sbyte
uint float
uint double
uint decimal
uint short
uint int
uint long
uint ushort
uint ulong
ulong bool
ulong byte
ulong sbyte
ulong float
ulong double
ulong decimal
ulong short
ulong int
ulong long
ulong ushort
ulong uint
bool string
byte string
sbyte string
float string
double string
decimal string
short string
int string
long string
ushort string
uint string
ulong string
char string
string bool
string byte
string sbyte
string float
string double
string decimal
string short
string int
string long
string ushort
string uint
string ulong
string char
string Guid
string Color
string Color32
DateTime double
Guid string
Color string
Color32 string