Example #1
0
 /// <summary>
 /// A generic method to serialize primitive Avro types.
 /// </summary>
 /// <typeparam name="TValue">Type of the C# type to be serialized</typeparam>
 /// <param name="value">The value to be serialized</param>
 /// <param name="tag">The schema type tag</param>
 /// <param name="writer">The writer which should be used to write the given type.</param>
 protected void Write <TValue>(object value, Schema.Type tag, Writer <TValue> writer)
 {
     if (!(value is TValue))
     {
         throw TypeMismatch(value, tag.ToString(), typeof(TValue).ToString());
     }
     writer((TValue)value);
 }
Example #2
0
 /// <summary>
 /// A generic method to serialize primitive Avro types.
 /// </summary>
 /// <typeparam name="S">Type of the C# type to be serialized</typeparam>
 /// <param name="value">The value to be serialized</param>
 /// <param name="tag">The schema type tag</param>
 /// <param name="writer">The writer which should be used to write the given type.</param>
 protected void Write <S>(object value, Schema.Type tag, Writer <S> writer)
 {
     if (!(value is S))
     {
         throw TypeMismatch(value, tag.ToString(), typeof(S).ToString());
     }
     writer((S)value);
 }
Example #3
0
        public void TestPrimitiveWithMetadata(string rawSchema, Schema.Type type)
        {
            Schema definedSchema = Schema.Parse(rawSchema);

            Assert.IsTrue(definedSchema is PrimitiveSchema);
            Assert.AreEqual(type.ToString().ToLower(), definedSchema.Name);
            Assert.AreEqual(type, definedSchema.Tag);

            testEquality(rawSchema, definedSchema);
            testToString(definedSchema);

            Assert.True(definedSchema.ToString().Contains("metafield"));

            var rawRecordSchema = "{\"type\":\"record\",\"name\":\"Foo\"," +
                                  "\"fields\":[{\"name\":\"f1\",\"type\":" + rawSchema +
                                  "}]}";
            Schema baseRecordSchema = Schema.Parse(rawRecordSchema);

            Assert.AreEqual(Schema.Type.Record, baseRecordSchema.Tag);
            RecordSchema recordSchema = baseRecordSchema as RecordSchema;

            Assert.IsNotNull(recordSchema);
            Assert.AreEqual(1, recordSchema.Count);

            Assert.IsTrue(recordSchema["f1"].Schema is PrimitiveSchema);
            Assert.AreEqual(type.ToString().ToLower(), recordSchema["f1"].Schema.Name);
            Assert.AreEqual(type, recordSchema["f1"].Schema.Tag);

            testEquality(rawRecordSchema, baseRecordSchema);
            testToString(recordSchema["f1"].Schema);

            Assert.True(baseRecordSchema.ToString().Contains("metafield"));
            Assert.True(recordSchema["f1"].Schema.ToString().Contains("metafield"));

            Assert.True(definedSchema.Equals(recordSchema["f1"].Schema));
            Assert.AreEqual(definedSchema.GetHashCode(), recordSchema["f1"].Schema.GetHashCode());
        }