Ejemplo n.º 1
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);
                }
Ejemplo n.º 2
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);
                }
            }
Ejemplo n.º 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);
Ejemplo n.º 4
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);
            }
        }
Ejemplo n.º 5
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);
            }
        }
Ejemplo n.º 6
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);
                }
            }