public void Serialize(ref JsonWriter writer, StartBasicLicenseFeatureAcknowledgements value, IJsonFormatterResolver formatterResolver) { if (value == null) { writer.WriteNull(); return; } writer.WriteBeginObject(); var count = 0; if (!string.IsNullOrEmpty(value.Message)) { writer.WritePropertyName("message"); writer.WriteString(value.Message); count++; } foreach (var kv in value) { if (count > 0) { writer.WriteValueSeparator(); } writer.WritePropertyName(kv.Key); StringArrayFormatter.Serialize(ref writer, kv.Value, formatterResolver); count++; } writer.WriteEndObject(); }
private int SerializeInternal(ref byte[] bytes, int offset, object value, IFormatterResolver formatterResolver) { var startOffset = offset; if (value == null) { offset += NilFormatter.Instance.Serialize(ref bytes, offset, Nil.Default, formatterResolver); } else { var type = value.GetType(); var typeInfo = type.GetTypeInfo(); if (value is DateTime dateTime) { offset += UnixTimestampDateTimeFormatter.Instance.Serialize(ref bytes, offset, dateTime, formatterResolver); } else if (value is DateTime?) // Nullable-type can't use pattern-matching. { offset += _nullableDateTimeFormatter.Serialize(ref bytes, offset, (DateTime?)value, formatterResolver); } else if (value is DateTime[] dateTimes) { offset += _dateTimeArrayFormatter.Serialize(ref bytes, offset, dateTimes, formatterResolver); } else if (value is DateTimeOffset dateTimeOffset) { offset += UnixTimestampDateTimeOffsetFormatter.Instance.Serialize(ref bytes, offset, dateTimeOffset, formatterResolver); } else if (value is DateTimeOffset?) // Nullable-type can't use pattern-matching. { offset += _nullableDateTimeOffsetFormatter.Serialize(ref bytes, offset, (DateTimeOffset?)value, formatterResolver); } else if (value is DateTimeOffset[] dateTimeOffsets) { offset += _dateTimeOffsetArrayFormatter.Serialize(ref bytes, offset, dateTimeOffsets, formatterResolver); } else if (value is IDictionary <string, object> dictionary) // before PrimitiveObjectFormatter { offset += MessagePackBinary.WriteMapHeader(ref bytes, offset, dictionary.Count); foreach (var item in dictionary) { offset += MessagePackBinary.WriteString(ref bytes, offset, item.Key); offset += SerializeInternal(ref bytes, offset, item.Value, formatterResolver); } } else if (PrimitiveObjectFormatter.IsSupportedType(type, typeInfo, value)) { offset += PrimitiveObjectFormatter.Instance.Serialize(ref bytes, offset, value, formatterResolver); } else if (typeInfo.IsAnonymous()) { var properties = type.GetProperties(); // heavy dictionary = properties.ToDictionary(x => x.Name, x => x.GetValue(value)); offset += MessagePackBinary.WriteMapHeader(ref bytes, offset, dictionary.Count); foreach (var item in dictionary) { offset += MessagePackBinary.WriteString(ref bytes, offset, item.Key); offset += SerializeInternal(ref bytes, offset, item.Value, formatterResolver); } } else { offset += DynamicObjectTypeFallbackFormatter.Instance.Serialize(ref bytes, offset, value, formatterResolver); } } return(offset - startOffset); }
public void Serialize(ref JsonWriter writer, DayOfWeek[] value, IJsonFormatterResolver formatterResolver) => _internalFormatter.Serialize(ref writer, value?.Select(x => ValueNameMappings[x]).ToArray(), formatterResolver);