NamespaceSystemLibrary.Common.Framework.Extensions SystemLibrary.Common.Framework.Json.dll
This class contains extension methods on Object
ObjectExtensions
public static class ObjectExtensions
Inheritance
Examples
Arguments
Methods
Join multiple arrays of same 'Enum' to an Array of that Enum Type
public static TEnum[] AsEnumArray<TEnum>(this object[] objects) where TEnum : struct, IComparable, IFormattable, IConvertible
public enum Colors
{
Black, White, Red, Blue
}
var integers = new object[] { 1, 2, 3, 4 };
var colors = integers.AsEnumArray<Colors>();
// colors is now an array of 'Colors', with one of each of the values:
// colors[0] == Black
// colors[1] == White
// ...
// colors[3] == Blue
public enum Colors
{
Black, White, Red, Blue
}
var texts = new object[] { "Red", "Black", "White" };
var colors = texts.AsEnumArray<Colors>();
// colors[1] == Black
Generic types
Name | Description |
---|---|
TEnum |
Methods arguments
Type | Name | Description |
---|---|---|
object[] | objects |
Type | Description |
---|---|
TEnum[] | An array of Enum Type |
Convert object to its json string representation with option to camelCase properties
Remarks
Uses built-in custom json converters for int, datetime, Enum, etc.
- for instance Enum, with an EnumValue attribute, will be outputted as the EnumValue attribute and not the Enum.Key
public static string Json(this object obj, bool camelCase)
// Assume camelCase argument true:
class User {
public string FirstName { get;set; }
}
var user = new User();
user.FirstName = "Hello World";
var json = user.Json();
var contains = json.Contains("firstName") && json.Contains("Hello World");
// contains is True, note that Json() has formatted 'FirstName' to 'firstName' when going from C# model to json string
Type | Description |
---|---|
string | Returns json string representation of input, or null if input was so |
Convert object to its json string representation with option to pass Custom JsonConverters
Remarks
Uses built-in custom json converters for int, datetime, Enum, etc.
- for instance Enum, with an EnumValue attribute, will be outputted as the EnumValue attribute and not the Enum.Key
public static string Json(this object obj, JsonSerializerOptions options = null, bool translateUnicodeCodepoints = false, params JsonConverter[] jsonConverters)
class User {
public string FirstName { get;set; }
}
class CustomConverter : JsonConverter...
var user = new User();
user.FirstName = "Hello World";
var json = user.Json(new CustomConverter());
var isTrue = json.Contains("FirstName") && json.Contains("Hello World");
// isTrue is 'True' as FirstName is not camelCased by default and 'Hello World' is its value
Methods arguments
Type | Name | Description |
---|---|---|
object | obj | |
JsonSerializerOptions | options | |
bool | translateUnicodeCodepoints | |
JsonConverter[] | jsonConverters |
Type | Description |
---|---|
string | Returns json string representation of input, or null if input was so |
Convert object to its json string representation with option to pass Custom JsonConverters
Remarks
Uses built-in custom json converters for int, datetime, Enum, etc.
- for instance Enum, with an EnumValue attribute, will be outputted as the EnumValue attribute and not the Enum.Key
public static string Json(this object obj, params JsonConverter[] jsonConverters)
class User {
public string FirstName { get;set; }
}
class CustomConverter : JsonConverter...
var user = new User();
user.FirstName = "Hello World";
var json = user.Json(new CustomConverter());
var isTrue = json.Contains("FirstName") && json.Contains("Hello World");
// isTrue is 'True' as FirstName is not camelCased by default and 'Hello World' is its value
Type | Description |
---|---|
string | Returns json string representation of input, or null if input was so |
Convert object to its xml string representation
public static string Xml(this object obj)
Methods arguments
Type | Name | Description |
---|---|---|
object | obj |
Type | Description |
---|---|
string |
}