/// <summary>
 /// Serializes given object to AVRO format (including header with metadata)
 /// Choosing <paramref name="codecType"/> reduces output object size
 /// </summary>
 public static byte[] Serialize(object obj, CodecType codecType)
 {
     using (MemoryStream resultStream = new MemoryStream())
     {
         var schema = Schema.Create(obj);
         using (var writer = new Encoder(schema, resultStream, codecType))
         {
             writer.Append(obj);
         }
         var result = resultStream.ToArray();
         return(result);
     }
 }
 /// <summary>
 /// Serializes given object to AVRO format (including header with metadata)
 /// Choosing <paramref name="codecType"/> reduces output object size
 /// </summary>
 public static byte[] Serialize(object obj, CodecType codecType)
 {
     using (MemoryStream resultStream = new MemoryStream())
     {
         string schema = GenerateSchema(obj?.GetType());
         using (var writer = new Encoder(Schema.Schema.Parse(schema), resultStream, codecType))
         {
             writer.Append(obj);
         }
         var result = resultStream.ToArray();
         return(result);
     }
 }
Example #3
0
        public static byte[] Serialize(object obj)
        {
            MemoryStream resultStream = new MemoryStream();

            string schema = AvroConvert.GenerateSchema(obj.GetType(), false);

            using (var writer = new Encoder(Schema.Schema.Parse(schema), resultStream))
            {
                writer.Append(obj);
            }

            var result = resultStream.ToArray();

            return(result);
        }
        private string ToJsonViaAvro <T>(T data, TypeSchema schema)
        {
            byte[] result;
            using (MemoryStream resultStream = new MemoryStream())
            {
                using (var writer = new Encoder(schema, resultStream, CodecType.Null))
                {
                    writer.Append(data);
                }
                result = resultStream.ToArray();
            }

            var avro2Json = AvroConvert.Avro2Json(result, schema.ToString());

            return(avro2Json);
        }
        public void GivenDateTimeProperty_WhenUsingSchemaWithTimeAsTimestampMicroseconds_ThenShouldWork()
        {
            //Arrange
            var toSerialize = new ClassWithDateTime {
                ArriveBy = DateTime.UtcNow
            };

            //Act
            var schema = Schema.Create(toSerialize);

            // Change schema logical type from timestamp-millis to timestamp-micros (a bit hacky)
            var schemaJson         = schema.ToString().Replace(LogicalTypeSchema.LogicalTypeEnum.TimestampMilliseconds, LogicalTypeSchema.LogicalTypeEnum.TimestampMicroseconds);
            var microsecondsSchema = new JsonSchemaBuilder().BuildSchema(schemaJson);

            byte[] result;
            using (MemoryStream resultStream = new MemoryStream())
            {
                using (var writer = new Encoder(microsecondsSchema, resultStream, CodecType.Null))
                {
                    writer.Append(toSerialize);
                }
                result = resultStream.ToArray();
            }

            var avro2Json    = AvroConvert.Avro2Json(result, microsecondsSchema.ToString());
            var deserialized = JsonConvert.DeserializeObject <ClassWithDateTime>(avro2Json);

            //Assert
            Assert.NotNull(result);
            Assert.NotNull(deserialized);
            Assert.Equal(toSerialize.ArriveBy.Millisecond, deserialized.ArriveBy.Millisecond);
            Assert.Equal(toSerialize.ArriveBy.Second, deserialized.ArriveBy.Second);
            Assert.Equal(toSerialize.ArriveBy.Minute, deserialized.ArriveBy.Minute);
            Assert.Equal(toSerialize.ArriveBy.Hour, deserialized.ArriveBy.Hour);
            Assert.Equal(toSerialize.ArriveBy.Day, deserialized.ArriveBy.Day);
            Assert.Equal(toSerialize.ArriveBy.Month, deserialized.ArriveBy.Month);
            Assert.Equal(toSerialize.ArriveBy.Year, deserialized.ArriveBy.Year);
        }