protected virtual void SerializeObject <TValue>(Utf8JsonWriter writer, TValue value, CustomPropertyInfo property = null) { this.SerializePropertyName(writer, value, property); writer.WriteStartObject(); this.SerializeTypeName(writer, value, property); SerializeHashCode(writer, value); var type = value.GetType(); if (type.IsIDictionary()) { var dict = value as IDictionary; foreach (var key in dict.Keys) { var dictProperty = new CustomPropertyInfo(key.ToString()); this.Serialize(writer, dict[key], dictProperty); } } else { foreach (var prop in type.GetProperties().Where(t => t.DeclaringType.FullName != "System.Linq.Dynamic.Core.DynamicClass")) { var objProperty = new CustomPropertyInfo(prop); this.Serialize(writer, prop.GetValue(value), objProperty); } } writer.WriteEndObject(); }
protected virtual void SerializeArray <TValue>(Utf8JsonWriter writer, TValue value, CustomPropertyInfo property = null) { this.SerializePropertyName(writer, value, property); writer.WriteStartArray(); try { List <object> list = new List <object>(); if (typeof(IEnumerable).IsAssignableFrom(value.GetType())) { IEnumerable items = (IEnumerable)value; // Money type: the GetEnumerator() returns null... if (!items.IsNullOrEmpty()) { foreach (var item in items) { list.Add(item); } } } else if (value is IEnumerable <object> ) { foreach (var item in value as IEnumerable <object> ) { list.Add(item); } } else if (value is IOrderedEnumerable <object> ) { foreach (var item in value as IOrderedEnumerable <object> ) { list.Add(item); } } this.SerializeEnumerable(writer, list, null); } catch { // Upon failure, use reflection and generic SerializeEnumerable method Type[] args = value.GetType().GetGenericArguments(); Type itemType = args[0]; MethodInfo serializeEnumerableMethod = typeof(CustomConverter <T>) .GetMethod("SerializeEnumerable", BindingFlags.Instance | BindingFlags.NonPublic) .MakeGenericMethod(itemType); serializeEnumerableMethod.Invoke(null, new object[] { writer, value, property }); } writer.WriteEndArray(); }
protected virtual void SerializeCustom <TValue>(Utf8JsonWriter writer, TValue value, CustomPropertyInfo property = null) { return; }
protected virtual void SerializeEnumerable <TValue>(Utf8JsonWriter writer, IEnumerable <TValue> value, CustomPropertyInfo property = null) { this.SerializePropertyName(writer, value, property); foreach (var item in value) { this.Serialize(writer, item); } }
protected virtual void SerializeUndefined <TValue>(Utf8JsonWriter writer, TValue value, CustomPropertyInfo property = null) { this.SerializePropertyName(writer, value, property); writer.WriteStringValue($"$Undefined"); }
protected virtual void SerializeNumber <TValue>(Utf8JsonWriter writer, TValue value, CustomPropertyInfo property = null) { this.SerializePropertyName(writer, value, property); var num = Convert.ToDecimal(value, CultureInfo.CurrentCulture); writer.WriteNumberValue(num); }
protected virtual void SerializeString <TValue>(Utf8JsonWriter writer, TValue value, CustomPropertyInfo property = null) { this.SerializePropertyName(writer, value, property); var result = JsonSerializer.Serialize(value).Replace("\u0022", string.Empty, StringComparison.OrdinalIgnoreCase); writer.WriteStringValue(result); }
protected virtual void SerializeBool(Utf8JsonWriter writer, bool value, CustomPropertyInfo property = null) { this.SerializePropertyName(writer, value, property); writer.WriteBooleanValue(value); }
protected virtual void SerializeEnumeration <TValue>(Utf8JsonWriter writer, TValue value, CustomPropertyInfo property = null) { this.SerializePropertyName(writer, value, property); writer.WriteStringValue(Enum.GetName(value.GetType(), value)); }
protected virtual void SerializePropertyName <TValue>(Utf8JsonWriter writer, TValue value, CustomPropertyInfo property = null) { if (property != null && !property.Name.IsNullOrEmpty()) { writer.WritePropertyName(property.Name); } }
protected virtual void SerializeNull <TValue>(Utf8JsonWriter writer, TValue value, CustomPropertyInfo property = null) { if (property != null && !property.IsContainer) { this.SerializePropertyName(writer, value, property); writer.WriteNullValue(); } }
protected virtual void SerializeTypeName <TValue>(Utf8JsonWriter writer, TValue value, CustomPropertyInfo property = null) { if (value != null) { switch (this.Options.TypeNameHandling) { case TypeNameHandlingOption.Suppress: break; case TypeNameHandlingOption.ShortName: writer.WritePropertyName($"$Type"); writer.WriteStringValue(JsonEncodedText.Encode(value.GetType().ShortAssemblyQualifiedName())); break; case TypeNameHandlingOption.FullName: writer.WritePropertyName($"$Type"); writer.WriteStringValue(JsonEncodedText.Encode(value.GetType().FullName)); break; default: break; } } }
protected virtual bool SerializeMaxDepth <TValue>(Utf8JsonWriter writer, TValue value, CustomPropertyInfo property = null) { switch (this.Options.MaxDepthHandling) { case MaxDepthHandlingOption.Suppress: return(true); case MaxDepthHandlingOption.Value: return(false); case MaxDepthHandlingOption.CurrentDepth: if (property != null) { property.Name = $"{property.Name}$MaxDepth"; this.SerializePropertyName(writer, value, property); writer.WriteStringValue($"{writer.CurrentDepth}"); } return(true); default: return(true); } }
protected virtual bool SerializeCircularRef <TValue>(Utf8JsonWriter writer, TValue value, CustomPropertyInfo property = null) { switch (this.Options.CircularRefHandling) { case CircularRefHandlingOption.Suppress: return(true); case CircularRefHandlingOption.Value: return(false); case CircularRefHandlingOption.HashCode: if (property != null) { property.Name = $"{property.Name}$HashCodeRef"; this.SerializePropertyName(writer, value, property); writer.WriteStringValue($"{value.GetHashCode()}"); } return(true); default: return(true); } }
protected virtual void Serialize <TValue>(Utf8JsonWriter writer, TValue value, CustomPropertyInfo property = null) { // Get the value kind var jsonValueType = this.GetJsonValueKind(value); // Maximum depth detection // with a default value of 0 indicating a maximum depth of 64 if (jsonValueType == CustomJsonValueKind.Object || jsonValueType == CustomJsonValueKind.Array) { int maxDepth = this.Options.MaxDepth > 0 ? this.Options.MaxDepth : 64; if (writer.CurrentDepth > maxDepth) { if (this.SerializeMaxDepth(writer, value, property)) { return; } } } // Circular reference detection if (jsonValueType == CustomJsonValueKind.Object || jsonValueType == CustomJsonValueKind.Array) { var hashCode = value.GetHashCode(); if (this.HashCodes.Contains(hashCode)) { if (this.SerializeCircularRef(writer, value, property)) { return; } } else { this.HashCodes.Add(hashCode); } } // Write an Enumeration if (value != null && value.GetType().IsEnum) { this.SerializeEnumeration(writer, value, property); return; } // Write other JSON value types switch (jsonValueType) { case CustomJsonValueKind.Null: this.SerializeNull(writer, value, property); break; case CustomJsonValueKind.True: this.SerializeBool(writer, true, property); break; case CustomJsonValueKind.False: this.SerializeBool(writer, false, property); break; case CustomJsonValueKind.String: this.SerializeString(writer, value, property); break; case CustomJsonValueKind.Number: this.SerializeNumber(writer, value, property); break; case CustomJsonValueKind.Array: this.SerializeArray(writer, value, property); break; case CustomJsonValueKind.Object: this.SerializeObject(writer, value, property); break; case CustomJsonValueKind.Undefined: this.SerializeUndefined(writer, value, property); break; case CustomJsonValueKind.Custom: this.SerializeCustom(writer, value, property); break; default: return; } }
/// <inheritdoc/> protected override void SerializeCustom <TValue>(Utf8JsonWriter writer, TValue value, CustomPropertyInfo property) { // To handle the serialization of a specified type if (value != null && value.GetType() == typeof(BadEnumerable)) { // To suppress the serialization ////return; // To write a custom serialization writer.WritePropertyName(property.Name); writer.WriteStringValue("This type is an issue..."); } else { // For the other types follow the default behavior base.SerializeCustom(writer, value, property); } }