public void JsonSchemaBuilder_ClassOfGuidWriterSchema()
 {
     var settings = new AvroSerializerSettings { Resolver = new AvroDataContractResolver(false) };
     IAvroSerializer<ClassOfGuid> serializer = AvroSerializer.Create<ClassOfGuid>(settings);
     string writerSchema = serializer.WriterSchema.ToString();
      AvroSerializer.CreateDeserializerOnly<ClassOfGuid>(writerSchema, settings);
 }
        public void TestSerializeDeserializeObject()
        {
            //CreateDeserializerOnly a new AvroSerializer instance and specify a custom serialization strategy AvroDataContractResolver
            //for serializing only properties attributed with DataContract/DateMember
            var settings = new AvroSerializerSettings();
            var avroSerializer = AvroSerializer.Create<SensorData>(settings);

            //CreateDeserializerOnly a new buffer
            using (var buffer = new MemoryStream())
            {
                //CreateDeserializerOnly sample data
                var expected = new SensorData { Value = new byte[] { 1, 2, 3, 4, 5 }, Position = new Location { Room = 243, Floor = 1 } };

                //Serialize the data to the specified stream
                avroSerializer.Serialize(buffer, expected);

                //Prepare the stream for deserializing the data
                buffer.Seek(0, SeekOrigin.Begin);

                //Derserialize data from the stream and cast it to the same type used for serialization
                var actual = avroSerializer.Deserialize(buffer);

                //Finally, verify that deserialized data matches the original ones
                Assert.IsTrue(this.Equal(expected, actual));
            }
        }
        /// <summary>
        ///     Initializes a new instance of the <see cref="ReflectionSchemaBuilder" /> class.
        /// </summary>
        /// <param name="settings">The settings.</param>
        public ReflectionSchemaBuilder(AvroSerializerSettings settings)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            this.settings = settings;
            this.knownTypes = new HashSet<Type>(this.settings.KnownTypes);
        }
        public void TestSerializeDeserializeUsingSequentialContainers()
        {
            //CreateDeserializerOnly a new AvroSerializer instance and specify a custom serialization strategy AvroDataContractResolver
            //for handling only properties attributed with DataContract/DateMember.
            var settings = new AvroSerializerSettings();

            //CreateDeserializerOnly a new buffer
            using (var buffer = new MemoryStream())
            {
                //CreateDeserializerOnly sample data
                var testData = new List<SensorData>
                        {
                            new SensorData { Value = new byte[] { 1, 2, 3, 4, 5 }, Position = new Location { Room = 243, Floor = 1 } },
                            new SensorData { Value = new byte[] { 6, 7, 8, 9 }, Position = new Location { Room = 244, Floor = 1 } }
                        };

                //CreateDeserializerOnly a SequentialWriter instance for type SensorData which can serialize a sequence of SensorData objects to stream
                using (var w = AvroContainer.CreateWriter<SensorData>(buffer, settings, Codec.Null))
                {
                    using (var writer = new SequentialWriter<SensorData>(w, 24))
                    {
                        // Serialize the data to stream using the sequential writer
                        testData.ForEach(writer.Write);
                    }
                }

                //Prepare the stream for deserializing the data
                buffer.Seek(0, SeekOrigin.Begin);

                //CreateDeserializerOnly a SequentialReader for type SensorData which will derserialize all serialized objects from the given stream
                //It allows iterating over the deserialized objects because it implements IEnumerable<T> interface
                using (var reader = new SequentialReader<SensorData>(
                    AvroContainer.CreateReader<SensorData>(buffer, true, settings, new CodecFactory())))
                {
                    var results = reader.Objects;

                    //Finally, verify that deserialized data matches the original ones
                    var pairs = testData.Zip(results, (serialized, deserialized) => new { expected = serialized, actual = deserialized });
                    foreach (var pair in pairs)
                    {
                        Assert.IsTrue(this.Equal(pair.expected, pair.actual));
                    }
                }
            }
        }
        public void SchemaEvolution_RecordWithMemberOrderChanged()
        {
            var settings = new AvroSerializerSettings() { Resolver = new AvroDataContractResolver(true, true) };
            var serializer = AvroSerializer.Create<Rectangle>(settings);
            var deserializer = AvroSerializer.Create<AnotherRectangle>(settings);

            using (var stream = new MemoryStream())
            {
                Rectangle expected = Rectangle.Create();
                serializer.Serialize(stream, expected);
                stream.Seek(0, SeekOrigin.Begin);

                AnotherRectangle actual = deserializer.Deserialize(stream);
                Assert.AreEqual(expected.Width, actual.Width);
                Assert.AreEqual(expected.Height, actual.Height);
            }
        }
 public void TestSetup()
 {
     this.dataContractSettings = new AvroSerializerSettings();
 }
        //Serializes and deserializes sample data set using Reflection and Avro Object Container Files
        //Serialized data is compressed with the Custom compression codec (Deflate of .NET Framework 4.5)
        //
        //This sample uses Memory Stream for all operations related to serialization, deserialization and
        //Object Container manipulation, though File Stream could be easily used.
        public void SerializeDeserializeUsingObjectContainersReflectionCustomCodec()
        {

            Console.WriteLine("SERIALIZATION USING REFLECTION, AVRO OBJECT CONTAINER FILES AND CUSTOM CODEC\n");

            //Path for Avro Object Container File
            string path = "AvroSampleReflectionDeflate45.avro";

            //Create a data set using sample Class and struct
            var testData = new List<SensorData>
                        {
                            new SensorData { Value = new byte[] { 1, 2, 3, 4, 5 }, Position = new Location { Room = 243, Floor = 1 } },
                            new SensorData { Value = new byte[] { 6, 7, 8, 9 }, Position = new Location { Room = 244, Floor = 1 } }
                        };

            //Serializing and saving data to file
            //Creating a Memory Stream buffer
            using (var buffer = new MemoryStream())
            {
                Console.WriteLine("Serializing Sample Data Set...");

                //Create a SequentialWriter instance for type SensorData which can serialize a sequence of SensorData objects to stream
                //Here the custom Codec is introduced. For convenience the next commented code line shows how to use built-in Deflate.
                //Note, that because the sample deals with different IMPLEMENTATIONS of Deflate, built-in and custom codecs are interchangeable
                //in read-write operations
                //using (var w = AvroContainer.CreateWriter<SensorData>(buffer, Codec.Deflate))
                using (var w = AvroContainer.CreateWriter<SensorData>(buffer, new DeflateCodec45()))
                {
                    using (var writer = new SequentialWriter<SensorData>(w, 24))
                    {
                        // Serialize the data to stream using the sequential writer
                        testData.ForEach(writer.Write);
                    }
                }

                //Save stream to file
                Console.WriteLine("Saving serialized data to file...");
                if (!WriteFile(buffer, path))
                {
                    Console.WriteLine("Error during file operation. Quitting method");
                    return;
                }
            }

            //Reading and deserializing data
            //Creating a Memory Stream buffer
            using (var buffer = new MemoryStream())
            {
                Console.WriteLine("Reading data from file...");

                //Reading data from Object Container File
                if (!ReadFile(buffer, path))
                {
                    Console.WriteLine("Error during file operation. Quitting method");
                    return;
                }

                Console.WriteLine("Deserializing Sample Data Set...");

                //Prepare the stream for deserializing the data
                buffer.Seek(0, SeekOrigin.Begin);

                //Because of SequentialReader<T> constructor signature an AvroSerializerSettings instance is required
                //when Codec Factory is explicitly specified
                //You may comment the line below if you want to use built-in Deflate (see next comment)
                AvroSerializerSettings settings = new AvroSerializerSettings();

                //Create a SequentialReader for type SensorData which will derserialize all serialized objects from the given stream
                //It allows iterating over the deserialized objects because it implements IEnumerable<T> interface
                //Here the custom Codec Factory is introduced.
                //For convenience the next commented code line shows how to use built-in Deflate
                //(no explicit Codec Factory parameter is required in this case).
                //Note, that because the sample deals with different IMPLEMENTATIONS of Deflate, built-in and custom codecs are interchangeable
                //in read-write operations
                //using (var reader = new SequentialReader<SensorData>(AvroContainer.CreateReader<SensorData>(buffer, true)))
                using (var reader = new SequentialReader<SensorData>(
                    AvroContainer.CreateReader<SensorData>(buffer, true, settings, new CodecFactoryDeflate45())))
                {
                    var results = reader.Objects;

                    //Finally, verify that deserialized data matches the original one
                    Console.WriteLine("Comparing Initial and Deserialized Data Sets...");
                    bool isEqual;
                    int count = 1;
                    var pairs = testData.Zip(results, (serialized, deserialized) => new { expected = serialized, actual = deserialized });
                    foreach (var pair in pairs)
                    {
                        isEqual = this.Equal(pair.expected, pair.actual);
                        Console.WriteLine("For Pair {0} result of Data Set Identity Comparison is {1}", count, isEqual.ToString());
                        count++;
                    }
                }
            }

            //Delete the file
            RemoveFile(path);
        }
 public SerializerAssigningVisitor(AvroSerializerSettings settings)
 {
     this.visited = new HashSet<Schema>();
     this.settings = settings;
 }
 public void TestSetup()
 {
     this.dataContractSettings = new AvroSerializerSettings { Resolver = new AvroDataContractResolver(true) };
 }
 public void ReflectionSchemaBuilder_BuildSchemaForSchemaNullableUsingDataContractResolverWithNoNulls()
 {
     var nullableSettings = new AvroSerializerSettings { Resolver = new AvroDataContractResolver(false) };
     RoundTripTestNullableSchema(nullableSettings, false, true, false, true, false, true, false);
 }
        private static void RoundTripTestNullableSchema(
            AvroSerializerSettings settings,
            bool rootSchemaIsUnion,
            bool nullableValueSchemaNullableIsUnion,
            bool nullableValueSchemaNotNullableIsUnion,
            bool valueSchemaNullableIsUnion,
            bool valueSchemaNotNullableIsUnion,
            bool referenceSchemaNullableIsUnion,
            bool referenceSchemaNotNullableIsUnion)
        {
            var builder = new ReflectionSchemaBuilder(settings);
            var schema = builder.BuildSchema(typeof(ClassWithSchemaNullableField));
            RecordSchema recordSchema = null;

            if (rootSchemaIsUnion)
            {
                var asUnion = schema as UnionSchema;
                Assert.IsNotNull(asUnion);

                var innerNullSchema = asUnion.Schemas[0] is NullSchema
                ? asUnion.Schemas[0]
                : asUnion.Schemas[1];

                recordSchema = asUnion.Schemas.Single(s => s != innerNullSchema) as RecordSchema;
                Assert.IsNotNull(recordSchema);
            }
            else
            {
                recordSchema = schema as RecordSchema;
                Assert.IsNotNull(recordSchema);
            }

            Assert.AreEqual(typeof(ClassWithSchemaNullableField).GetAllFields().Count(), recordSchema.Fields.Count);

            var nullableValueSchemaNullable = recordSchema.Fields.Single(f => f.Name == "NullableValueNullableSchema");
            ValidateSchema(nullableValueSchemaNullable.TypeSchema, nullableValueSchemaNullableIsUnion, typeof(IntSchema));

            var nullableValueSchemaNotNullable = recordSchema.Fields.Single(f => f.Name == "NullableValueNotNullableSchema");
            ValidateSchema(nullableValueSchemaNotNullable.TypeSchema, nullableValueSchemaNotNullableIsUnion, typeof(IntSchema));

            var valueSchemaNullable = recordSchema.Fields.Single(f => f.Name == "NotNullableValueNullableSchema");
            ValidateSchema(valueSchemaNullable.TypeSchema, valueSchemaNullableIsUnion, typeof(IntSchema));

            var valueSchemaNotNullable = recordSchema.Fields.Single(f => f.Name == "NotNullableValueNotNullableSchema");
            ValidateSchema(valueSchemaNotNullable.TypeSchema, valueSchemaNotNullableIsUnion, typeof(IntSchema));

            var referenceSchemaNullable = recordSchema.Fields.Single(f => f.Name == "ReferenceFieldNullableSchema");
            ValidateSchema(referenceSchemaNullable.TypeSchema, referenceSchemaNullableIsUnion, typeof(RecordSchema));

            var referenceSchemaNotNullable = recordSchema.Fields.Single(f => f.Name == "ReferenceFieldNotNullableSchema");
            ValidateSchema(referenceSchemaNotNullable.TypeSchema, referenceSchemaNotNullableIsUnion, typeof(RecordSchema));
        }
 public void ReflectionSchemaBuilder_BuildSchemaForSchemaNullableUsingPublicMembersResolverWithCSharpNulls()
 {
     var nullableSettings = new AvroSerializerSettings { Resolver = new AvroPublicMemberContractResolver(true) };
     RoundTripTestNullableSchema(nullableSettings, true, true, true, true, false, true, true);
 }