Example #1
0
        public Ceras(bool forNetworking = true)
        {
            if (CerasBufferPool.Pool == null)
            {
                CerasBufferPool.Pool = new CerasDefaultBufferPool();
            }

            mSerializerConfig = new SerializerConfig
            {
                PreserveReferences = false
            };

            mSerializerConfig.Advanced.SealTypesWhenUsingKnownTypes = forNetworking;

            if (forNetworking)
            {
                mSerializerConfig.VersionTolerance.Mode = VersionToleranceMode.Disabled;
                mSerializerConfig.KnownTypes.AddRange(KnownTypes);
                mSerializerConfig.KnownTypes.ForEach(
                    knownType =>
                    mSerializerConfig.ConfigType(knownType).TypeConstruction = TypeConstruction.ByUninitialized()
                    );
            }
            else
            {
                mSerializerConfig.VersionTolerance.Mode = VersionToleranceMode.Standard;
            }

            mSerializer = new CerasSerializer(mSerializerConfig);
        }
        public CerasMessageSerializer()
        {
            CerasBufferPool.Pool = new CerasBuffer();

            _serializer = new ThreadLocal <CerasSerializer>(() =>
            {
                config = new SerializerConfig();

                config.Warnings.ExceptionOnStructWithAutoProperties = false;

                config.OnConfigNewType = (t) =>
                {
                    t.TargetMembers = TargetMember.AllPublic;

                    t.TypeConstruction = TypeConstruction.ByUninitialized();

                    if (typeof(Exception).IsAssignableFrom(t.Type))
                    {
                        t.TargetMembers = TargetMember.All;
                    }
                };

                return(new CerasSerializer(config));
            });
        }
Example #3
0
        private void AddKnownTypes(SerializerConfig config, string nameSpce)
        {
            var packetTypes = Assembly.GetExecutingAssembly().GetTypes().Where(t => t.Namespace == nameSpce).ToList();

            foreach (var typ in packetTypes)
            {
                config.KnownTypes.Add(typ);
                mSerializerConfig.ConfigType(typ).TypeConstruction = TypeConstruction.ByUninitialized();
            }
        }
Example #4
0
        public SerializationLargeGraph()
        {
            var initial = new TopLevel(Guid.NewGuid(),
                                       ImmutableSortedDictionary <string, FirstLevelChild> .Empty.WithComparers(StringComparer.OrdinalIgnoreCase),
                                       0, "Tag", "Description");

            var current = initial;

            for (int i = 0; i < 1000; ++i)
            {
                if (i % 10 == 0)
                {
                    _t1.Add(current);
                }

                if (i % 10 == 0)
                {
                    current = AddFirstLevelChild(current);
                }

                current = AddSecondLevelChild(current);
            }

            var config = new SerializerConfig {
                DefaultTargets = TargetMember.AllFields, PreserveReferences = true
            };

            config.Advanced.ReadonlyFieldHandling = ReadonlyFieldHandling.ForcedOverwrite;
            config.OnConfigNewType = tc => tc.TypeConstruction = TypeConstruction.ByUninitialized();
            config.UseImmutableFormatters();
            ceras = new CerasSerializer(config);

            //_hyperion.Serialize(_t1, _m2);

            _msgPackOptions = MessagePackSerializerOptions.Standard.WithResolver(
                CompositeResolver.Create(
                    ImmutableCollectionResolver.Instance,
                    DynamicObjectResolver.Instance,
                    StandardResolver.Instance
                    )
                );

            //_netSerializer.Serialize(_m5, _t1);
        }
		public void Repro64_v1_NullableStruct()
		{
			var config = new SerializerConfig { DefaultTargets = TargetMember.AllFields, PreserveReferences = false };
			config.Advanced.ReadonlyFieldHandling = ReadonlyFieldHandling.ForcedOverwrite;
			config.Advanced.SkipCompilerGeneratedFields = false;
			config.OnConfigNewType = tc => tc.TypeConstruction = TypeConstruction.ByUninitialized();

			var ceras = new CerasSerializer(config);

			var wrapper = new Wrapper(new TestDec { Value = 5 });
			var clone = ceras.Advanced.Clone(wrapper);

			Assert.Equal(5M, clone.NullableStruct.Value.Value);

			wrapper = new Wrapper(null);
			clone = ceras.Advanced.Clone(wrapper);

			Assert.Null(clone.NullableStruct);
		}
		public void Repro64_v2_ReadonlyStructs_Direct()
		{
			var config = new SerializerConfig { DefaultTargets = TargetMember.AllFields, PreserveReferences = false };
			config.Advanced.ReadonlyFieldHandling = ReadonlyFieldHandling.ForcedOverwrite;
			config.Advanced.SkipCompilerGeneratedFields = false;
			config.OnConfigNewType = tc =>
			{
				tc.TypeConstruction = TypeConstruction.ByUninitialized();
			};


			var ceras = new CerasSerializer(config);

			var obj = new NullableWrapper(new Test { Value = 2.34M, SubStruct = new SubStruct(new NameAge("riki", 5), 6, 7) });


			var objClone = ceras.Advanced.Clone(obj);
			Assert.Equal(obj.TestStruct, objClone.TestStruct);

			var subStruct = new SubStruct(new NameAge("riki", 5), 6, 7);
			var ssClone = ceras.Advanced.Clone(subStruct);
			Assert.True(DeepComparer.Instance.CheckEquality(subStruct, ssClone));
		}
		public void Repro64_v2_ReadonlyStructs_Ctor()
		{
			var config = new SerializerConfig { DefaultTargets = TargetMember.AllFields, PreserveReferences = false };
			config.Advanced.ReadonlyFieldHandling = ReadonlyFieldHandling.ForcedOverwrite;
			config.Advanced.SkipCompilerGeneratedFields = false;
			int nCtor=0;
			int nUninit=0;
			config.OnConfigNewType = tc =>
			{
				var ctor = tc.Type.GetConstructors(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).FirstOrDefault();
				if (ctor != null)
				{
					tc.TypeConstruction = TypeConstruction.ByConstructor(ctor);
					nCtor++;
				}
				else
				{
					tc.TypeConstruction = TypeConstruction.ByUninitialized();
					nUninit++;
				}
			};
			

			var ceras = new CerasSerializer(config);

			var obj = new NullableWrapper(new Test { Value = 2.34M, SubStruct = new SubStruct(new NameAge("riki", 5), 6, 7) });
			var clone = ceras.Advanced.Clone(obj);

			Assert.Equal(7, nCtor);
			Assert.Equal(1, nUninit);

			Assert.Equal(obj.TestStruct.Value.Value, clone.TestStruct.Value.Value);
			Assert.Equal(obj.TestStruct.Value.SubStruct.Num, clone.TestStruct.Value.SubStruct.Num);
			Assert.Equal(obj.TestStruct.Value.SubStruct.NameAge.Age, clone.TestStruct.Value.SubStruct.NameAge.Age);
			Assert.Equal(obj.TestStruct.Value.SubStruct.NameAge.Name, clone.TestStruct.Value.SubStruct.NameAge.Name);
		}