コード例 #1
0
 static DefaultResolverFormatterCache()
 {
     if (typeof(T) == TypeConstants.ObjectType)
     {
         // final fallback
         Formatter = (IMessagePackFormatter <T>)DefaultResolverCore.ObjectFallbackFormatter;
     }
     else
     {
         Formatter = DefaultResolverCore.Instance.GetFormatter <T>();
     }
 }
コード例 #2
0
 static Cache()
 {
     foreach (var item in new[] { NativeDateTimeResolver.Instance, ContractlessStandardResolver.Instance })
     {
         var f = item.GetFormatter <T>();
         if (f != null)
         {
             formatter = f;
             return;
         }
     }
 }
コード例 #3
0
 static FormatterCache()
 {
     foreach (var item in resolvers)
     {
         var f = item.GetFormatter <T>();
         if (f != null)
         {
             formatter = f;
             return;
         }
     }
 }
コード例 #4
0
 static FormatterCache()
 {
     foreach (IFormatterResolver item in Resolvers)
     {
         IMessagePackFormatter <T> f = item.GetFormatter <T>();
         if (f != null)
         {
             Formatter = f;
             return;
         }
     }
 }
コード例 #5
0
 static FormatterCache()
 {
     if (typeof(T) == typeof(object))
     {
         // final fallback
         formatter = (IMessagePackFormatter <T>)ObjectFallbackFormatter;
     }
     else
     {
         formatter = StandardResolverAllowPrivateCore.Instance.GetFormatter <T>();
     }
 }
コード例 #6
0
        public void Serialize(ref MessagePackWriter writer, TDictionary value, MessagePackSerializerOptions options)
        {
            if (value == null)
            {
                writer.WriteNil();
            }
            else
            {
                IFormatterResolver             resolver       = options.Resolver;
                IMessagePackFormatter <TKey>   keyFormatter   = resolver.GetFormatterWithVerify <TKey>();
                IMessagePackFormatter <TValue> valueFormatter = resolver.GetFormatterWithVerify <TValue>();

                int count;
                {
                    var col = value as ICollection <KeyValuePair <TKey, TValue> >;
                    if (col != null)
                    {
                        count = col.Count;
                    }
                    else
                    {
                        var col2 = value as IReadOnlyCollection <KeyValuePair <TKey, TValue> >;
                        if (col2 != null)
                        {
                            count = col2.Count;
                        }
                        else
                        {
                            throw new MessagePackSerializationException("DictionaryFormatterBase's TDictionary supports only ICollection<KVP> or IReadOnlyCollection<KVP>");
                        }
                    }
                }

                writer.WriteMapHeader(count);

                TEnumerator e = this.GetSourceEnumerator(value);
                try
                {
                    while (e.MoveNext())
                    {
                        writer.CancellationToken.ThrowIfCancellationRequested();
                        KeyValuePair <TKey, TValue> item = e.Current;
                        keyFormatter.Serialize(ref writer, item.Key, options);
                        valueFormatter.Serialize(ref writer, item.Value, options);
                    }
                }
                finally
                {
                    e.Dispose();
                }
            }
        }
コード例 #7
0
            static FormatterCache()
            {
                isFreezed = true;

                var t = typeof(T);

                object formatterObject;

                if (formatters.TryGetValue(t, out formatterObject))
                {
                    formatter = (IMessagePackFormatter <T>)formatterObject;
                }
            }
コード例 #8
0
            /// <inheritdoc/>
            protected override IMessagePackFormatter <T> GetFormatterCore <T>()
            {
                foreach (IFormatterResolver resolver in this.subResolvers)
                {
                    IMessagePackFormatter <T> formatter = resolver.GetFormatter <T>();
                    if (formatter != null)
                    {
                        return((IMessagePackFormatter <T>)formatter);
                    }
                }

                return(null);
            }
コード例 #9
0
        protected T SerializeThenDeserialize <T>(IMessagePackFormatter <T> formatter, T expectedResult, MessagePackSerializerOptions options)
        {
            var sequence = new Sequence <byte>();
            var writer   = new MessagePackWriter(sequence);

            formatter.Serialize(ref writer, expectedResult, options);

            writer.Flush();

            var reader = new MessagePackReader(sequence.AsReadOnlySequence);

            return(formatter.Deserialize(ref reader, options));
        }
コード例 #10
0
        public SpanBuffer(int maxBufferSize, IFormatterResolver formatterResolver)
        {
            if (maxBufferSize < HeaderSize)
            {
                ThrowHelper.ThrowArgumentException($"Buffer size should be at least {HeaderSize}", nameof(maxBufferSize));
            }

            _maxBufferSize     = maxBufferSize;
            _offset            = HeaderSize;
            _buffer            = new byte[Math.Min(InitialBufferSize, maxBufferSize)];
            _formatterResolver = formatterResolver;
            _formatter         = _formatterResolver.GetFormatter <ArraySegment <Span> >();
        }
コード例 #11
0
        public SpanBuffer(int maxBufferSize, IFormatterResolver formatterResolver)
        {
            if (maxBufferSize < HeaderSize)
            {
                throw new ArgumentException(nameof(maxBufferSize), $"Buffer size should be at least {HeaderSize}");
            }

            _maxBufferSize     = maxBufferSize;
            _offset            = HeaderSize;
            _buffer            = new byte[Math.Min(InitialBufferSize, maxBufferSize)];
            _formatterResolver = formatterResolver;
            _formatter         = _formatterResolver.GetFormatter <Span[]>();
        }
コード例 #12
0
            protected override IMessagePackFormatter <T> GetFormatterCore <T>()
            {
                foreach (IFormatterResolver item in this.resolvers)
                {
                    IMessagePackFormatter <T> f = item.GetFormatter <T>();
                    if (f != null)
                    {
                        return(f);
                    }
                }

                return(null);
            }
コード例 #13
0
 public Shared(
     IMessagePackFormatter <T> formatter,
     MessagePackSerializerOptions options,
     int backed,
     ReadOnlyMemory <byte> offsets,
     IBigMemory backing)
 {
     Formatter = formatter ?? throw new ArgumentNullException(nameof(formatter));
     Options   = options ?? throw new ArgumentNullException(nameof(options));
     Backed    = backed;
     Backing   = backing ?? throw new ArgumentNullException(nameof(backing));
     Offsets   = offsets;
 }
コード例 #14
0
            static FormatterCache()
            {
                formatter = (IMessagePackFormatter <T>)FSharpGetFormatterHelper.GetFormatter(typeof(T));

                if (formatter == null)
                {
                    var f = DynamicUnionResolver.Instance.GetFormatter <T>();
                    if (f != null)
                    {
                        formatter = f;
                    }
                }
            }
コード例 #15
0
 private static void SetGenericFormatter(Type[] arguments, Type type)
 {
     if (typeof(IPocoTracker <>).MakeGenericType(arguments) == type)
     {
         var trackerType = typeof(TrackablePocoTrackerInterfaceMessagePackFormatter <>)
                           .MakeGenericType(arguments);
         formatter = (IMessagePackFormatter <T>)Activator.CreateInstance(trackerType);
     }
     else if (typeof(TrackablePocoTracker <>).MakeGenericType(arguments) == type)
     {
         var trackerType = typeof(TrackablePocoTrackerClassMessagePackFormatter <>)
                           .MakeGenericType(arguments);
         formatter = (IMessagePackFormatter <T>)Activator.CreateInstance(trackerType);
     }
     else if (typeof(IListTracker <>).MakeGenericType(arguments) == type)
     {
         var trackerType = typeof(TrackableListTrackerInterfaceMessagePackFormatter <>)
                           .MakeGenericType(arguments);
         formatter = (IMessagePackFormatter <T>)Activator.CreateInstance(trackerType);
     }
     else if (typeof(TrackableListTracker <>).MakeGenericType(arguments) == type)
     {
         var trackerType = typeof(TrackableListTrackerClassMessagePackFormatter <>)
                           .MakeGenericType(arguments);
         formatter = (IMessagePackFormatter <T>)Activator.CreateInstance(trackerType);
     }
     else if (typeof(ISetTracker <>).MakeGenericType(arguments) == type)
     {
         var trackerType = typeof(TrackableSetTrackerInterfaceMessagePackFormatter <>)
                           .MakeGenericType(arguments);
         formatter = (IMessagePackFormatter <T>)Activator.CreateInstance(trackerType);
     }
     else if (typeof(TrackableSetTracker <>).MakeGenericType(arguments) == type)
     {
         var trackerType = typeof(TrackableSetTrackerClassMessagePackFormatter <>)
                           .MakeGenericType(arguments);
         formatter = (IMessagePackFormatter <T>)Activator.CreateInstance(trackerType);
     }
     else if (typeof(TrackableSet <>).MakeGenericType(arguments) == type)
     {
         var trackerType = typeof(TrackableSetMessagePackFormatter <>)
                           .MakeGenericType(arguments);
         formatter = (IMessagePackFormatter <T>)Activator.CreateInstance(trackerType);
     }
     else if (typeof(TrackableList <>).MakeGenericType(arguments) == type)
     {
         var trackerType = typeof(TrackableListMessagePackFormatter <>)
                           .MakeGenericType(arguments);
         formatter = (IMessagePackFormatter <T>)Activator.CreateInstance(trackerType);
     }
 }
コード例 #16
0
        public T[,,] Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
        {
            if (reader.TryReadNil())
            {
                return(null);
            }
            else
            {
                IMessagePackFormatter <T> formatter = options.Resolver.GetFormatterWithVerify <T>();

                var len = reader.ReadArrayHeader();
                if (len != ArrayLength)
                {
                    throw new InvalidOperationException("Invalid T[,,] format");
                }

                var iLength = reader.ReadInt32();
                var jLength = reader.ReadInt32();
                var kLength = reader.ReadInt32();
                var maxLen  = reader.ReadArrayHeader();

                var array = new T[iLength, jLength, kLength];

                var i = 0;
                var j = 0;
                var k = -1;
                for (int loop = 0; loop < maxLen; loop++)
                {
                    reader.CancellationToken.ThrowIfCancellationRequested();
                    if (k < kLength - 1)
                    {
                        k++;
                    }
                    else if (j < jLength - 1)
                    {
                        k = 0;
                        j++;
                    }
                    else
                    {
                        k = 0;
                        j = 0;
                        i++;
                    }

                    array[i, j, k] = formatter.Deserialize(ref reader, options);
                }

                return(array);
            }
        }
コード例 #17
0
        public T[,] Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
        {
            if (reader.TryReadNil())
            {
                return(null);
            }
            else
            {
                IMessagePackFormatter <T> formatter = options.Resolver.GetFormatterWithVerify <T>();

                var len = reader.ReadArrayHeader();
                if (len != ArrayLength)
                {
                    throw new MessagePackSerializationException("Invalid T[,] format");
                }

                var iLength = reader.ReadInt32();
                var jLength = reader.ReadInt32();
                var maxLen  = reader.ReadArrayHeader();

                var array = new T[iLength, jLength];

                var i = 0;
                var j = -1;
                options.Security.DepthStep(ref reader);
                try
                {
                    for (int loop = 0; loop < maxLen; loop++)
                    {
                        reader.CancellationToken.ThrowIfCancellationRequested();
                        if (j < jLength - 1)
                        {
                            j++;
                        }
                        else
                        {
                            j = 0;
                            i++;
                        }

                        array[i, j] = formatter.Deserialize(ref reader, options);
                    }
                }
                finally
                {
                    reader.Depth--;
                }

                return(array);
            }
        }
コード例 #18
0
            static FormatterCache()
            {
                if (typeof(T) == typeof(Vector3))
                {
                    formatter = (IMessagePackFormatter <T>)(object) new TempVector3Formatter();
                    return;
                }

                formatter = UnityBlitResolver.Instance.GetFormatter <T>();
                if (formatter == null)
                {
                    formatter = DefaultResolver.Instance.GetFormatter <T>();
                }
            }
コード例 #19
0
            static Cache()
            {
                var t = typeof(T);

                foreach (var resolver in Resolvers)
                {
                    var f = resolver.GetFormatter <T>();
                    if (f != null)
                    {
                        Formatter = f;
                        return;
                    }
                }
            }
コード例 #20
0
        protected virtual object DeserializeMap(ref MessagePackReader reader, int length, MessagePackSerializerOptions options)
        {
            IMessagePackFormatter <object> objectFormatter = options.Resolver.GetFormatter <object>();
            var dictionary = new Dictionary <object, object>(length, options.Security.GetEqualityComparer <object>());

            for (int i = 0; i < length; i++)
            {
                var key   = objectFormatter.Deserialize(ref reader, options);
                var value = objectFormatter.Deserialize(ref reader, options);
                dictionary.Add(key, value);
            }

            return(dictionary);
        }
コード例 #21
0
            protected override object DeserializeMap(ref MessagePackReader reader, int length, MessagePackSerializerOptions options)
            {
                IMessagePackFormatter <string> keyFormatter    = options.Resolver.GetFormatterWithVerify <string>();
                IMessagePackFormatter <object> objectFormatter = options.Resolver.GetFormatter <object>();
                IDictionary <string, object>   dictionary      = new ExpandoObject();

                for (int i = 0; i < length; i++)
                {
                    var key   = keyFormatter.Deserialize(ref reader, options);
                    var value = objectFormatter.Deserialize(ref reader, options);
                    dictionary.Add(key, value);
                }

                return(dictionary);
            }
        public override bool TryGet(Type type, out IMessagePackFormatter formatter)
        {
            if (!base.TryGet(type, out formatter))
            {
                for (int i = 0; i < Providers.Count; i++)
                {
                    if (Providers[i].TryGet(type, out formatter))
                    {
                        return(true);
                    }
                }
            }

            return(formatter != null);
        }
コード例 #23
0
 static FormatterCache()
 {
     if (typeof(T) == typeof(Span))
     {
         formatter = (IMessagePackFormatter <T>)(object) SpanFormatter.Instance;
     }
     else if (typeof(T) == typeof(Service))
     {
         formatter = (IMessagePackFormatter <T>)(object) ServiceFormatter.Instance;
     }
     else
     {
         formatter = StandardResolver.Instance.GetFormatter <T>();
     }
 }
コード例 #24
0
        public void Serialize()
        {
            IMessagePackProvider           provider  = MessagePackUtility.CreateProvider(MessagePackContext.Empty, -2);
            IMessagePackFormatter <Target> formatter = provider.Get <Target>();

            var target = new Target {
                TargetValue = new Target()
            };
            var writer = new MessagePackWriter();

            formatter.Serialize(ref writer, target);

            Assert.NotNull(writer.Buffer);
            Assert.Pass(writer.Print());
        }
コード例 #25
0
            static FormatterCache()
            {
                if (typeof(T) == typeof(object))
                {
                    // final fallback
#if !ENABLE_IL2CPP
                    Formatter = (IMessagePackFormatter <T>)ObjectFallbackFormatter;
#else
                    Formatter = PrimitiveObjectResolver.Instance.GetFormatter <T>();
#endif
                }
                else
                {
                    Formatter = ContractlessStandardResolverAllowPrivateCore.Instance.GetFormatter <T>();
                }
            }
コード例 #26
0
            static FormatterCache()
            {
                if (typeof(T) == typeof(object))
                {
                    // final fallback
#if !UNITY
                    Formatter = (IMessagePackFormatter <T>)ObjectFallbackFormatter;
#else
                    formatter = PrimitiveObjectResolver.Instance.GetFormatter <T>();
#endif
                }
                else
                {
                    Formatter = StandardResolverCore.Instance.GetFormatter <T>();
                }
            }
コード例 #27
0
        public void SerializeThenDeserialize_WithValidArguments_RoundTrips(PackageDeprecationMetadataContextInfo expectedResult)
        {
            var formatters = new IMessagePackFormatter[]
            {
                AlternatePackageMetadataContextInfoFormatter.Instance,
            };
            var resolvers = new IFormatterResolver[] { MessagePackSerializerOptions.Standard.Resolver };
            var options   = MessagePackSerializerOptions.Standard.WithSecurity(MessagePackSecurity.UntrustedData).WithResolver(CompositeResolver.Create(formatters, resolvers));

            PackageDeprecationMetadataContextInfo actualResult = SerializeThenDeserialize(PackageDeprecationMetadataContextInfoFormatter.Instance, expectedResult, options);

            Assert.Equal(expectedResult.Message, actualResult.Message);
            Assert.Equal(expectedResult.Reasons.First(), actualResult.Reasons.First());
            Assert.Equal(expectedResult.AlternatePackage.PackageId, actualResult.AlternatePackage.PackageId);
            Assert.Equal(expectedResult.AlternatePackage.VersionRange, actualResult.AlternatePackage.VersionRange);
        }
コード例 #28
0
            static FormatterCache()
            {
                if (typeof(T) == typeof(object))
                {
                    // final fallback
#if NETSTANDARD || NETFRAMEWORK
                    formatter = (IMessagePackFormatter <T>)ObjectFallbackFormatter;
#else
                    formatter = PrimitiveObjectResolver.Instance.GetFormatter <T>();
#endif
                }
                else
                {
                    formatter = ContractlessStandardResolverCore.Instance.GetFormatter <T>();
                }
            }
コード例 #29
0
        public void SerializeThenDeserialize_WithValidArguments_RoundTrips(VersionInfoContextInfo expectedResult)
        {
            var formatters = new IMessagePackFormatter[]
            {
                PackageSearchMetadataContextInfoFormatter.Instance,
                PackageVulnerabilityMetadataContextInfoFormatter.Instance,
            };
            var resolvers = new IFormatterResolver[] { MessagePackSerializerOptions.Standard.Resolver };
            var options   = MessagePackSerializerOptions.Standard.WithSecurity(MessagePackSecurity.UntrustedData).WithResolver(CompositeResolver.Create(formatters, resolvers));

            VersionInfoContextInfo actualResult = SerializeThenDeserialize(VersionInfoContextInfoFormatter.Instance, expectedResult, options);

            Assert.Equal(expectedResult.DownloadCount, actualResult.DownloadCount);
            Assert.Equal(expectedResult.Version, actualResult.Version);
            Assert.Equal(expectedResult.PackageDeprecationMetadata.Message, actualResult.PackageDeprecationMetadata.Message);
            Assert.Equal(expectedResult.PackageSearchMetadata.Identity, actualResult.PackageSearchMetadata.Identity);
        }
コード例 #30
0
                static Cache()
                {
                    var type = typeof(T);

                    if (type == typeof(Point))
                    {
                        Formatter = PointFormatter.Instance as IMessagePackFormatter <T>;
                    }
                    else if (type == typeof(Polygon))
                    {
                        Formatter = PolygonFormatter.Instance as IMessagePackFormatter <T>;
                    }
                    else
                    {
                        Formatter = StandardResolver.Instance.GetFormatter <T>();
                    }
                }