Ejemplo n.º 1
0
        /// <summary>
        ///     Initialize a new instance of the JsonSerializer class.
        /// </summary>
        public JsonSerializer(ISchemaRegistryClient schemaRegistryClient, JsonSerializerConfig config = null)
        {
            this.schemaRegistryClient = schemaRegistryClient;

            this.schema         = NJsonSchema.JsonSchema.FromType <T>();
            this.schemaFullname = schema.Title;
            this.schemaText     = schema.ToJson();

            if (config == null)
            {
                return;
            }

            var nonJsonConfig = config.Where(item => !item.Key.StartsWith("json."));

            if (nonJsonConfig.Count() > 0)
            {
                throw new ArgumentException($"JsonSerializer: unknown configuration parameter {nonJsonConfig.First().Key}");
            }

            if (config.BufferBytes != null)
            {
                this.initialBufferSize = config.BufferBytes.Value;
            }
            if (config.AutoRegisterSchemas != null)
            {
                this.autoRegisterSchema = config.AutoRegisterSchemas.Value;
            }
            if (config.SubjectNameStrategy != null)
            {
                this.subjectNameStrategy = config.SubjectNameStrategy.Value.ToDelegate();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Initialize a new instance of the JsonSerializer class.
        /// </summary>
        /// <param name="schemaRegistryClient">
        ///     Confluent Schema Registry client instance.
        /// </param>
        /// <param name="config">
        ///     Serializer configuration.
        /// </param>
        /// <param name="jsonSchemaGeneratorSettings">
        ///     JSON schema generator settings.
        /// </param>
        public JsonSerializer(ISchemaRegistryClient schemaRegistryClient, JsonSerializerConfig config = null, JsonSchemaGeneratorSettings jsonSchemaGeneratorSettings = null)
        {
            this.schemaRegistryClient        = schemaRegistryClient;
            this.jsonSchemaGeneratorSettings = jsonSchemaGeneratorSettings;

            this.schema = this.jsonSchemaGeneratorSettings == null
                ? NJsonSchema.JsonSchema.FromType <T>()
                : NJsonSchema.JsonSchema.FromType <T>(this.jsonSchemaGeneratorSettings);

            this.schemaFullname = schema.Title;
            this.schemaText     = schema.ToJson();

            if (config == null)
            {
                return;
            }

            var nonJsonConfig = config.Where(item => !item.Key.StartsWith("json."));

            if (nonJsonConfig.Count() > 0)
            {
                throw new ArgumentException($"JsonSerializer: unknown configuration parameter {nonJsonConfig.First().Key}");
            }

            if (config.BufferBytes != null)
            {
                this.initialBufferSize = config.BufferBytes.Value;
            }
            if (config.AutoRegisterSchemas != null)
            {
                this.autoRegisterSchema = config.AutoRegisterSchemas.Value;
            }
            if (config.NormalizeSchemas != null)
            {
                this.normalizeSchemas = config.NormalizeSchemas.Value;
            }
            if (config.UseLatestVersion != null)
            {
                this.useLatestVersion = config.UseLatestVersion.Value;
            }
            if (config.SubjectNameStrategy != null)
            {
                this.subjectNameStrategy = config.SubjectNameStrategy.Value.ToDelegate();
            }

            if (this.useLatestVersion && this.autoRegisterSchema)
            {
                throw new ArgumentException($"JsonSerializer: cannot enable both use.latest.version and auto.register.schemas");
            }
        }