コード例 #1
0
        private static AvroSerializer <T> CreateForCore <T>(string writerSchema, AvroSerializerSettings settings)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            var key        = Tuple.Create(writerSchema, typeof(T), settings);
            var serializer = TypedSerializers.Get(key);

            if (serializer != null && settings.UseCache)
            {
                return(new AvroSerializer <T>(serializer));
            }

            var reader           = new ReflectionSchemaBuilder(settings).BuildSchema(typeof(T));
            var generator        = new SerializerGenerator();
            var builderGenerator = new SerializerAssigningVisitor(settings);

            if (string.IsNullOrEmpty(writerSchema))
            {
                builderGenerator.Visit(reader);
                serializer = new GeneratedSerializer
                {
                    WriterSchema = reader,
                    ReaderSchema = reader,
                    Serialize    = settings.GenerateSerializer
                        ? generator.GenerateSerializer <T>(reader)
                        : null,
                    Deserialize = settings.GenerateDeserializer
                        ? generator.GenerateDeserializer <T>(reader)
                        : null
                };
            }
            else
            {
                var writer        = new JsonSchemaBuilder().BuildSchema(writerSchema);
                var matchedSchema = new EvolutionSchemaBuilder().Build(writer, reader);
                if (matchedSchema == null)
                {
                    throw new SerializationException(string.Format(CultureInfo.InvariantCulture, "Writer schema does not match reader schema."));
                }

                builderGenerator.Visit(matchedSchema);
                serializer = new GeneratedSerializer
                {
                    WriterSchema = writer,
                    ReaderSchema = reader,
                    Deserialize  = generator.GenerateDeserializer <T>(matchedSchema)
                };
            }

            if (settings.UseCache)
            {
                TypedSerializers.Add(key, serializer);
            }
            return(new AvroSerializer <T>(serializer));
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="AvroSerializer{T}" /> class.
        /// Prevents a default instance of the <see cref="AvroSerializer{T}" /> class from being created.
        /// </summary>
        /// <param name="serializer">The serializer.</param>
        internal AvroSerializer(GeneratedSerializer serializer)
        {
            if (serializer == null)
            {
                throw new ArgumentNullException("serializer");
            }

            this.serializer  = serializer;
            this.serialize   = (Action <IEncoder, T>)serializer.Serialize;
            this.deserialize = (Func <IDecoder, T>)serializer.Deserialize;
        }
コード例 #3
0
        private static AvroSerializer <T> CreateForCore <T>(string writerSchema, string readerSchema)
        {
            var key        = Tuple.Create(writerSchema, readerSchema);
            var serializer = UntypedSerializers.Get(key);

            if (serializer != null)
            {
                return(new AvroSerializer <T>(serializer));
            }

            var reader           = new JsonSchemaBuilder().BuildSchema(readerSchema);
            var builderGenerator = new SerializerAssigningVisitor(new AvroSerializerSettings());

            if (string.IsNullOrEmpty(writerSchema))
            {
                builderGenerator.Visit(reader);
                Action <IEncoder, T> s = (e, obj) => reader.Serializer.Serialize(e, obj);
                Func <IDecoder, T>   d = decode =>
                {
                    return((T)reader.Serializer.Deserialize(decode));
                };
                serializer = new GeneratedSerializer
                {
                    WriterSchema = reader,
                    ReaderSchema = reader,
                    Serialize    = s,
                    Deserialize  = d
                };
            }
            else
            {
                var writer        = new JsonSchemaBuilder().BuildSchema(writerSchema);
                var matchedSchema = new EvolutionSchemaBuilder().Build(writer, reader);
                if (matchedSchema == null)
                {
                    throw new SerializationException(string.Format(CultureInfo.InvariantCulture, "Writer schema does not match reader schema."));
                }

                builderGenerator.Visit(matchedSchema);

                Func <IDecoder, T> d = decode => (T)matchedSchema.Serializer.Deserialize(decode);
                serializer = new GeneratedSerializer
                {
                    WriterSchema = writer,
                    ReaderSchema = reader,
                    Deserialize  = d
                };
            }

            UntypedSerializers.Add(key, serializer);
            return(new AvroSerializer <T>(serializer));
        }