HumanReadableSerializerOptions Class

Provides options for controlling the behavior of HumanReadableSerializer.

public sealed class HumanReadableSerializerOptions : IEquatable<HumanReadableSerializerOptions>

Example

var options = new HumanReadableSerializerOptions
{
    IncludeFields = true,
    DefaultIgnoreCondition = HumanReadableIgnoreCondition.WhenWritingNull,
};
var obj = new { Name = "John Doe", Nickname = (string?)null };
var output = HumanReadableSerializer.Serialize(obj, options);
Assert.Equal("Name: John Doe", output);
Implements

Constructors

NameDescription
HumanReadableSerializerOptions()

HumanReadableSerializerOptions()

HumanReadableSerializerOptions.cs
public HumanReadableSerializerOptions()

No documentation is available.

Properties

NameDescription
ConvertersGets the list of converters used for serialization.
DefaultIgnoreConditionGets or sets the default ignore condition for properties and fields.
DictionaryKeyOrderGets or sets the comparer used to sort dictionary keys when serializing dictionaries.
IncludeFieldsGets or sets whether to include public fields during serialization.
IncludeObsoleteMembersGets or sets whether to include members marked with the Obsolete attribute.
IsReadOnlyGets whether this instance is read-only.
MaxDepthGets or sets the maximum depth allowed when serializing nested objects.
NewLineGets or sets the line terminator written between lines, either "\n" or "\r\n". The default is "\n" on every platform, so the output does not depend on the operating system.
PropertyOrderGets or sets the comparer used to sort property names when serializing objects.
ShowInvisibleCharactersInValuesGets or sets whether to show invisible characters (like newlines and tabs) in values using Unicode control pictures.

Gets the list of converters used for serialization.

public IList<HumanReadableConverter> Converters { get; }

DefaultIgnoreCondition

HumanReadableSerializerOptions.cs

Gets or sets the default ignore condition for properties and fields.

public HumanReadableIgnoreCondition DefaultIgnoreCondition { get; set; }

Gets or sets the comparer used to sort dictionary keys when serializing dictionaries.

public IComparer<string>? DictionaryKeyOrder { get; set; }

Property value

IComparer<string>?

Remarks

When null, the keys of dictionaries that keep an order (for example Dictionary<TKey, TValue>) are written in enumeration order. The keys of hash-based collections that have no meaningful order (Hashtable, HybridDictionary and StringDictionary) are always sorted, using StringComparer.Ordinal by default, so the output does not depend on the process. The comparer also applies to the other non-generic IDictionary implementations.

Gets or sets whether to include public fields during serialization.

public bool IncludeFields { get; set; }

Property value

bool

IncludeObsoleteMembers

HumanReadableSerializerOptions.cs

Gets or sets whether to include members marked with the Obsolete attribute.

public bool IncludeObsoleteMembers { get; set; }

Property value

bool

Gets whether this instance is read-only.

public bool IsReadOnly { get; }

Property value

bool

Gets or sets the maximum depth allowed when serializing nested objects.

public int MaxDepth { get; set; }

Property value

int

Gets or sets the line terminator written between lines, either "\n" or "\r\n". The default is "\n" on every platform, so the output does not depend on the operating system.

public string NewLine { get; set; }

Property value

string

Exceptions

ArgumentException
The value is neither "\n" nor "\r\n".

Remarks

Line breaks inside serialized values are rewritten with this terminator too.

Gets or sets the comparer used to sort property names when serializing objects.

public IComparer<string>? PropertyOrder { get; set; }

Property value

IComparer<string>?

ShowInvisibleCharactersInValues

HumanReadableSerializerOptions.cs

Gets or sets whether to show invisible characters (like newlines and tabs) in values using Unicode control pictures.

public bool ShowInvisibleCharactersInValues { get; set; }

Property value

bool

Remarks

Every value and property name is affected, including single-line ones. A space is kept between other characters, but written as ␠ at the start or the end of a line. The other space characters, the zero-width characters, the format characters (for example soft hyphens and bidirectional marks) and the C1 control characters, which have no control picture, are written as their code point, for example <U+00A0> for a no-break space.

Methods

NameDescription
AddAttribute(FieldInfo, HumanReadableAttribute)Adds an attribute to the specified field.
AddAttribute(PropertyInfo, HumanReadableAttribute)Adds an attribute to the specified property.
AddAttribute(Type, HumanReadableAttribute)Adds an attribute to the specified type.
AddAttribute(Type, string, HumanReadableAttribute)Adds an attribute to a member of the specified type.
AddAttribute<T>(Expression<Func<T, object>>, HumanReadableAttribute)Adds an attribute to a member identified by an expression.
AddFieldAttribute(Func<FieldInfo, bool>, HumanReadableAttribute)Adds an attribute to all fields matching the specified condition.
AddFormatter(string, ValueFormatter)Adds a value formatter for the specified media type.
AddPropertyAttribute(Func<PropertyInfo, bool>, HumanReadableAttribute)Adds an attribute to all properties matching the specified condition.
AddTypeAttribute(Func<Type, bool>, HumanReadableAttribute)Adds an attribute to all types matching the specified condition.
Equals(HumanReadableSerializerOptions?)
Equals(object?)
GetFormatter(string)
GetHashCode()
GetOrSetSerializationData<T>(string, Func<T>)Gets or creates serialization data for the current serialization scope.
MakeReadOnly()Makes this instance read-only, preventing further modifications.
ToString()

AddAttribute overloads

AddAttribute(FieldInfo, HumanReadableAttribute)

HumanReadableSerializerOptions.cs

Adds an attribute to the specified field.

public void AddAttribute(FieldInfo member, HumanReadableAttribute attribute)
Parameters
member FieldInfo
The field to add the attribute to.
attribute HumanReadableAttribute
The attribute to add.
Remarks

The attribute applies when serializing the type the field is retrieved from (MemberInfo.ReflectedType) or a type deriving from it.

AddAttribute(PropertyInfo, HumanReadableAttribute)

HumanReadableSerializerOptions.cs

Adds an attribute to the specified property.

public void AddAttribute(PropertyInfo member, HumanReadableAttribute attribute)
Parameters
member PropertyInfo
The property to add the attribute to.
attribute HumanReadableAttribute
The attribute to add.
Remarks

The attribute applies when serializing the type the property is retrieved from (MemberInfo.ReflectedType) or a type deriving from it, including to the properties that override it.

AddAttribute(Type, HumanReadableAttribute)

HumanReadableSerializerOptions.cs

Adds an attribute to the specified type.

public void AddAttribute(Type type, HumanReadableAttribute attribute)
Parameters
type Type
The type to add the attribute to.
attribute HumanReadableAttribute
The attribute to add.
Remarks

The attribute also applies to the types that derive from type or implement it, as an attribute declared on the type would.

AddAttribute(Type, string, HumanReadableAttribute)

HumanReadableSerializerOptions.cs

Adds an attribute to a member of the specified type.

public void AddAttribute(Type type, string memberName, HumanReadableAttribute attribute)
Parameters
type Type
The type containing the member.
memberName string
The name of the member.
attribute HumanReadableAttribute
The attribute to add.
Remarks

The member can be declared in type or in one of its base types, including private members of the base types. The attribute applies when serializing type or a type deriving from it, including to the members that override the member.

AddAttribute<T>(Expression<Func<T, object>>, HumanReadableAttribute)

HumanReadableSerializerOptions.cs

Adds an attribute to a member identified by an expression.

public void AddAttribute<T>(Expression<Func<T, object>> member, HumanReadableAttribute attribute)
Type parameters
T
The type containing the member.
Parameters
member Expression<Func<T, object>>
An expression identifying the member, such as x => x.Name, or several members, such as x => new { x.Name, x.Age }.
attribute HumanReadableAttribute
The attribute to add.
Remarks

The attribute applies to the member when serializing the type it is accessed on, or a type deriving from it, including to the members that override it. For x => x.Name, this is T. An attribute belongs to a member, not to a path in the object graph: for x => x.Address.City, the attribute applies to the City member of every Address instance, wherever it is.

AddFieldAttribute(Func<FieldInfo, bool>, HumanReadableAttribute)

HumanReadableSerializerOptions.cs

Adds an attribute to all fields matching the specified condition.

public void AddFieldAttribute(Func<FieldInfo, bool> condition, HumanReadableAttribute attribute)

Parameters

condition Func<FieldInfo, bool>
A function that determines which fields to add the attribute to.
attribute HumanReadableAttribute
The attribute to add.

AddFormatter(string, ValueFormatter)

HumanReadableSerializerOptions.cs

Adds a value formatter for the specified media type.

public void AddFormatter(string mediaType, ValueFormatter formatter)

Parameters

mediaType string
The media type (e.g., "application/json", "text/xml").
formatter ValueFormatter
The formatter to use for the media type.

AddPropertyAttribute(Func<PropertyInfo, bool>, HumanReadableAttribute)

HumanReadableSerializerOptions.cs

Adds an attribute to all properties matching the specified condition.

public void AddPropertyAttribute(Func<PropertyInfo, bool> condition, HumanReadableAttribute attribute)

Parameters

condition Func<PropertyInfo, bool>
A function that determines which properties to add the attribute to.
attribute HumanReadableAttribute
The attribute to add.

AddTypeAttribute(Func<Type, bool>, HumanReadableAttribute)

HumanReadableSerializerOptions.cs

Adds an attribute to all types matching the specified condition.

public void AddTypeAttribute(Func<Type, bool> condition, HumanReadableAttribute attribute)

Parameters

condition Func<Type, bool>
A function that determines which types to add the attribute to.
attribute HumanReadableAttribute
The attribute to add.

Equals overloads

Equals(HumanReadableSerializerOptions?)

public bool Equals(HumanReadableSerializerOptions? other)

No documentation is available.

Equals(object?)

public override bool Equals(object? obj)
Parameters
obj object?
Returns

bool

No documentation is available.

public ValueFormatter? GetFormatter(string mediaType)

Parameters

mediaType string

Returns

ValueFormatter?

No documentation is available.

GetHashCode()

public override int GetHashCode()

Returns

int

No documentation is available.

GetOrSetSerializationData<T>(string, Func<T>)

HumanReadableSerializerOptions.cs

Gets or creates serialization data for the current serialization scope.

public T GetOrSetSerializationData<T>(string name, Func<T> addValue)

Type parameters

T
The type of the data.

Parameters

name string
The name of the data.
addValue Func<T>
A factory function to create the value if it doesn't exist.

Returns

T The value associated with the specified name, creating it if necessary.

Makes this instance read-only, preventing further modifications.

public void MakeReadOnly()

ToString()

public override string ToString()

Returns

string

No documentation is available.

Operators

NameDescription
operator ==(HumanReadableSerializerOptions?, HumanReadableSerializerOptions?)
operator !=(HumanReadableSerializerOptions?, HumanReadableSerializerOptions?)

operator ==(HumanReadableSerializerOptions?, HumanReadableSerializerOptions?)

public static bool operator ==(HumanReadableSerializerOptions? left, HumanReadableSerializerOptions? right)

No documentation is available.

operator !=(HumanReadableSerializerOptions?, HumanReadableSerializerOptions?)

public static bool operator !=(HumanReadableSerializerOptions? left, HumanReadableSerializerOptions? right)

No documentation is available.