Exemple #1
0
        /// <summary>
        /// Reads a schema from a JSON token.
        /// </summary>
        /// <param name="element">
        /// The element to parse.
        /// </param>
        /// <param name="cache">
        /// An optional schema cache. The cache is populated as the schema is read and can be used
        /// to provide schemas for certain names or cache keys.
        /// </param>
        /// <param name="scope">
        /// The surrounding namespace, if any.
        /// </param>
        public override IJsonSchemaReadResult Read(JsonElement element, ConcurrentDictionary <string, Schema> cache, string?scope)
        {
            var result = new JsonSchemaReadResult();

            if (GetType(element) == JsonSchemaToken.Int && GetLogicalType(element) == JsonSchemaToken.Date)
            {
                result.Schema = cache.GetOrAdd($"{JsonSchemaToken.Int}!{JsonSchemaToken.Date}", _ => new IntSchema()
                {
                    LogicalType = new DateLogicalType()
                });
            }
            else
            {
                result.Exceptions.Add(new UnknownSchemaException("The date case can only be applied to \"int\" schemas with a \"date\" logical type."));
            }

            return(result);
        }
Exemple #2
0
        /// <summary>
        /// Reads a schema from a JSON token.
        /// </summary>
        /// <param name="element">
        /// The element to parse.
        /// </param>
        /// <param name="cache">
        /// An optional schema cache. The cache is populated as the schema is read and can be used
        /// to provide schemas for certain names or cache keys.
        /// </param>
        /// <param name="scope">
        /// The surrounding namespace, if any.
        /// </param>
        public override IJsonSchemaReadResult Read(JsonElement element, ConcurrentDictionary <string, Schema> cache, string?scope)
        {
            var result = new JsonSchemaReadResult();

            if (GetType(element) == JsonSchemaToken.Array)
            {
                var child = Reader.Read(GetItems(element), cache, scope);
                var key   = cache.Single(p => p.Value == child).Key;

                result.Schema = cache.GetOrAdd($"{JsonSchemaToken.Array}<{key}>", _ => new ArraySchema(child));
            }
            else
            {
                result.Exceptions.Add(new UnknownSchemaException("The array case can only be applied to valid array schema representations."));
            }

            return(result);
        }
Exemple #3
0
        /// <summary>
        /// Reads a schema from a JSON token.
        /// </summary>
        /// <param name="element">
        /// The element to parse.
        /// </param>
        /// <param name="cache">
        /// An optional schema cache. The cache is populated as the schema is read and can be used
        /// to provide schemas for certain names or cache keys.
        /// </param>
        /// <param name="scope">
        /// The surrounding namespace, if any.
        /// </param>
        public override IJsonSchemaReadResult Read(JsonElement element, ConcurrentDictionary <string, Schema> cache, string?scope)
        {
            var result = new JsonSchemaReadResult();

            if (GetType(element) is string type && (type == JsonSchemaToken.Bytes || type == JsonSchemaToken.Fixed) && GetLogicalType(element) == JsonSchemaToken.Decimal)
            {
                if (type == JsonSchemaToken.Fixed)
                {
                    var schema = new FixedSchema(GetQualifiedName(element, scope), GetSize(element))
                    {
                        Aliases     = GetQualifiedAliases(element, scope) ?? new string[0],
                        LogicalType = new DecimalLogicalType(GetPrecision(element), GetScale(element))
                    };

                    if (!cache.TryAdd(schema.FullName, schema))
                    {
                        throw new InvalidDataException($"Invalid fixed name; a definition for {schema.FullName} was already read.");
                    }

                    foreach (var alias in schema.Aliases)
                    {
                        if (!cache.TryAdd(alias, schema))
                        {
                            throw new InvalidDataException($"Invalid fixed alias; a definition for {alias} was already read.");
                        }
                    }

                    result.Schema = schema;
                }
                else
                {
                    result.Schema = cache.GetOrAdd($"{JsonSchemaToken.Bytes}!{JsonSchemaToken.Decimal}", new BytesSchema()
                    {
                        LogicalType = new DecimalLogicalType(GetPrecision(element), GetScale(element))
                    });
                }
            }