Пример #1
0
        void ActivateSchema(Schema schema)
        {
            // What schema changes are relevant to us?
            // - Schema of own type
            // - Schema of value-types inside us (dispatches for ref types are handled by RefFormatter anyway)

            // For now we only adapt to change to the current type schema.
            // Do we have serializers prepared for this schema already?


            // Important sanity check, if this happens the user should definitely know about it!
            if (_deserializationDepth > 0)
            {
                if (schema.Type.IsValueType)
                {
                    throw new InvalidOperationException("Schema of a value-type has changed while an object-type is being deserialized. This is feature is WIP, check out GitHub for more information.");
                }
            }


            // Use already compiled serializers
            if (_generatedSerializerPairs.TryGetValue(schema, out var pair))
            {
                _serializer   = pair.Serializer;
                _deserializer = pair.Deserializer;

                _currentSchema = schema;
                return;
            }

            bool isStatic = schema.IsStatic;

            // Generate
            if (schema.IsPrimary)
            {
                _serializer = DynamicFormatter <T> .GenerateSerializer(_ceras, schema, true, isStatic).Compile();

                _deserializer = DynamicFormatter <T> .GenerateDeserializer(_ceras, schema, true, isStatic).Compile();
            }
            else
            {
                // Different Schema -> generate no serializer!
                // Writing data in some old format is not supported (yet, maybe in the future).
                // In theory we could do it. But it's not implemented because there would have to be some way for the user to specify what Schema to use.
                // And we get into all sorts of troubles with type-conversion (not implemented yet, but it will probably arrive earlier than this...)
                // This also protects us against bugs!
                _serializer   = ErrorSerializer;
                _deserializer = DynamicFormatter <T> .GenerateDeserializer(_ceras, schema, true, isStatic).Compile();
            }

            _currentSchema = schema;

            _generatedSerializerPairs.Add(schema, new SerializerPair(_serializer, _deserializer));
        }
Пример #2
0
        public void Setup()
        {
            var ceras = new CerasSerializer();

            var parent1 = new Person
            {
                Age       = -901,
                FirstName = "Parent 1",
                LastName  = "abc",
                Sex       = Sex.Male,
            };
            var parent2 = new Person
            {
                Age       = 7881964,
                FirstName = "Parent 2",
                LastName  = "xyz",
                Sex       = Sex.Female,
            };

            _person = new Person
            {
                Age       = 5,
                FirstName = "Riki",
                LastName  = "Example Person Object",
                Sex       = Sex.Unknown,
                Parent1   = parent1,
                Parent2   = parent2,
            };

            var meta   = ceras.GetTypeMetaData(typeof(Person));
            var schema = meta.PrimarySchema;

            _serializer1 = DynamicFormatter <Person> .GenerateSerializer(ceras, schema, false, false).Compile();

            //_serializer2 = DynamicFormatter<Person>.GenerateSerializer2(ceras, schema, false, false);
        }