示例#1
0
            /// <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);
                }
            }
示例#2
0
                /// <inheritdoc/>
                public T Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
                {
                    // Read variant from reader
                    var o = MsgPack.Deserialize <object>(ref reader, options);

                    return(new MessagePackVariantValue(o, options, false) as T);
                }
示例#3
0
                /// <inheritdoc/>
                public T Deserialize(byte[] bytes, int offset, MessagePackSerializerOptions options,
                                     out int readSize)
                {
                    var o = MsgPack.Deserialize(typeof(object), bytes, offset, options, out readSize);

                    if (o == null)
                    {
                        return(default);
示例#4
0
 /// <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);
     }
 }
示例#5
0
        /// <inheritdoc/>
        public object Deserialize(ReadOnlyMemory <byte> buffer, Type type)
        {
            try {
#if MessagePack2
                return(MsgPack.Deserialize(type, buffer, Options));
#else
                return(MsgPack.Deserialize(type, buffer.ToArray(), Options));
#endif
            }
            catch (MessagePackSerializationException ex) {
                throw new SerializerException(ex.Message, ex);
            }
        }
示例#6
0
        /// <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);
            }
        }
示例#7
0
        /// <inheritdoc/>
        public VariantValue Parse(ReadOnlyMemory <byte> buffer)
        {
            try {
#if MessagePack2
                var o = MsgPack.Deserialize(typeof(object), buffer, Options);
#else
                var o = MsgPack.Deserialize(typeof(object), buffer.ToArray(), Options);
#endif
                if (o is VariantValue v)
                {
                    return(v);
                }
                return(new MessagePackVariantValue(o, Options, false));
            }
            catch (MessagePackSerializationException ex) {
                throw new SerializerException(ex.Message, ex);
            }
        }
示例#8
0
 /// <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);
         }
     }
 }
示例#9
0
            /// <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);
                }
            }