/// <inheritdoc/> public override object ConvertTo(Type type) { if (_value == null) { return(null); } var valueType = _value.GetType(); if (type.IsAssignableFrom(valueType)) { return(_value); } try { #if MessagePack2 var mem = new ArrayBufferWriter <byte>(); MsgPack.Serialize(mem, _value, _options); var buffer = buffer.WrittenMemory; #else var buffer = MsgPack.Serialize( _value?.GetType() ?? typeof(object), _value, _options); #endif // Special case - convert byte array to buffer if not bin to begin. if (type == typeof(byte[]) && valueType.IsArray) { return(((IList <byte>)MsgPack.Deserialize(typeof(IList <byte>), buffer, _options)).ToArray()); } return(MsgPack.Deserialize(type, buffer, _options)); } catch (MessagePackSerializationException ex) { throw new SerializerException(ex.Message, ex); } }
/// <inheritdoc/> public int Serialize(ref byte[] bytes, int offset, T value, MessagePackSerializerOptions options) { if (value is MessagePackVariantValue packed) { return(MsgPack.Serialize(packed._value?.GetType() ?? typeof(object), ref bytes, offset, packed._value, options)); } else if (value is null) { return(MsgPackWriter.WriteNil(ref bytes, offset)); } else if (value is VariantValue variant) { if (VariantValueEx.IsNull(variant)) { return(MsgPackWriter.WriteNil(ref bytes, offset)); } else if (variant.IsListOfValues) { var written = MsgPackWriter.WriteArrayHeader( ref bytes, offset, variant.Count); foreach (var item in variant.Values) { written += MsgPack.Serialize(item?.GetType() ?? typeof(object), ref bytes, offset + written, item, options); } return(written); } else if (variant.IsObject) { // Serialize objects as key value pairs var dict = variant.PropertyNames .ToDictionary(k => k, k => variant[k]); return(MsgPack.Serialize(dict.GetType(), ref bytes, offset, dict, options)); } else if (variant.TryGetValue(out var primitive)) { return(MsgPack.Serialize(primitive?.GetType() ?? typeof(object), ref bytes, offset, primitive, options)); } else { return(MsgPack.Serialize(variant.Value?.GetType() ?? typeof(object), ref bytes, offset, variant.Value, options)); } } else { return(offset); } }
/// <inheritdoc/> public void Serialize(IBufferWriter <byte> buffer, object o, SerializeOption format) { try { #if MessagePack2 MsgPack.Serialize(buffer, o, Options); #else var b = MsgPack.Serialize(o?.GetType() ?? typeof(object), o, Options); buffer.Write(b); #endif } catch (MessagePackSerializationException ex) { throw new SerializerException(ex.Message, ex); } }
/// <inheritdoc/> public void Serialize(ref MessagePackWriter writer, T value, MessagePackSerializerOptions options) { if (value is MessagePackVariantValue packed) { MsgPack.Serialize(ref writer, packed._value, options); } else if (value is null) { writer.WriteNil(); } else if (value is VariantValue variant) { if (variant.IsNull()) { writer.WriteNil(); } else if (variant.IsListOfValues) { writer.WriteArrayHeader(variant.Count); foreach (var item in variant.Values) { MsgPack.Serialize(ref writer, item, options); } } else if (variant.IsObject) { // Serialize objects as key value pairs var dict = variant.PropertyNames .ToDictionary(k => k, k => variant[k]); MsgPack.Serialize(ref writer, dict, options); } else if (variant.TryGetValue(out var primitive)) { MsgPack.Serialize(ref writer, primitive, options); } else { MsgPack.Serialize(ref writer, variant.Value, options); } } }
/// <summary> /// Convert to typeless object /// </summary> /// <param name="value"></param> /// <returns></returns> internal object ToTypeLess(object value) { if (value == null) { return(null); } try { #if MessagePack2 var mem = new ArrayBufferWriter <byte>(); MsgPack.Serialize(mem, value, _options); var buffer = mem.WrittenMemory; #else var buffer = MsgPack.Serialize( value?.GetType() ?? typeof(object), value, _options); #endif return(MsgPack.Deserialize(typeof(object), buffer, _options)); } catch (MessagePackSerializationException ex) { throw new SerializerException(ex.Message, ex); } }