コード例 #1
0
        internal override bool CanRead(TypeSchema writerSchema)
        {
            if (writerSchema is UnionSchema || Type == writerSchema.Type)
            {
                return(true);
            }
            AvroType t = writerSchema.Type;

            switch (Type)
            {
            case AvroType.Double:
                return(t == AvroType.Int || t == AvroType.Long || t == AvroType.Float);

            case AvroType.Float:
                return(t == AvroType.Int || t == AvroType.Long);

            case AvroType.Long:
                return(t == AvroType.Int);

            case AvroType.String:
                return(t == AvroType.String || t == AvroType.Null);

            default:
                return(false);
            }
        }
コード例 #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>
        private static void Write <S>(object value, AvroType tag, Writer <S> writer)
        {
            if (value == null)
            {
                value = default(S);
            }

            if (!(value is S))
            {
                throw new AvroTypeMismatchException(
                          $"[{typeof(S)}] required to write against [{tag}] schema but found " + value?.GetType());
            }

            writer((S)value);
        }