/// <summary>
        /// Writes a <see cref="FixedSchema" />.
        /// </summary>
        /// <inheritdoc />
        public virtual JsonSchemaWriterCaseResult Write(Schema schema, Utf8JsonWriter json, bool canonical, JsonSchemaWriterContext context)
        {
            if (schema is FixedSchema fixedSchema)
            {
                if (context.Names.TryGetValue(fixedSchema.FullName, out var existing))
                {
                    if (!schema.Equals(existing))
                    {
                        throw new InvalidSchemaException($"A conflicting schema with the name {fixedSchema.FullName} has already been written.");
                    }

                    json.WriteStringValue(fixedSchema.FullName);
                }
                else
                {
                    context.Names.Add(fixedSchema.FullName, fixedSchema);

                    json.WriteStartObject();
                    json.WriteString(JsonAttributeToken.Name, fixedSchema.FullName);

                    if (!canonical)
                    {
                        if (fixedSchema.Aliases.Count > 0)
                        {
                            json.WritePropertyName(JsonAttributeToken.Aliases);
                            json.WriteStartArray();

                            foreach (var alias in fixedSchema.Aliases)
                            {
                                json.WriteStringValue(alias);
                            }

                            json.WriteEndArray();
                        }
                    }

                    json.WriteString(JsonAttributeToken.Type, JsonSchemaToken.Fixed);
                    json.WriteNumber(JsonAttributeToken.Size, fixedSchema.Size);
                    json.WriteEndObject();
                }

                return(new JsonSchemaWriterCaseResult());
            }
            else
            {
                return(JsonSchemaWriterCaseResult.FromException(new UnsupportedSchemaException(schema, $"{nameof(JsonFixedSchemaWriterCase)} can only be applied to {nameof(FixedSchema)}s.")));
            }
        }
        /// <summary>
        /// Writes a <see cref="MapSchema" />.
        /// </summary>
        /// <inheritdoc />
        public virtual JsonSchemaWriterCaseResult Write(Schema schema, Utf8JsonWriter json, bool canonical, JsonSchemaWriterContext context)
        {
            if (schema is MapSchema mapSchema)
            {
                json.WriteStartObject();
                json.WriteString(JsonAttributeToken.Type, JsonSchemaToken.Map);
                json.WritePropertyName(JsonAttributeToken.Values);
                Writer.Write(mapSchema.Value, json, canonical, context);
                json.WriteEndObject();

                return(new JsonSchemaWriterCaseResult());
            }
            else
            {
                return(JsonSchemaWriterCaseResult.FromException(new UnsupportedSchemaException(schema, $"{nameof(JsonMapSchemaWriterCase)} can only be applied to {nameof(MapSchema)}s.")));
            }
        }
        /// <summary>
        /// Writes a <see cref="UnionSchema" /> with a <see cref="UuidLogicalType" />.
        /// </summary>
        /// <inheritdoc />
        public virtual JsonSchemaWriterCaseResult Write(Schema schema, Utf8JsonWriter json, bool canonical, JsonSchemaWriterContext context)
        {
            if (schema is StringSchema && schema.LogicalType is UuidLogicalType)
            {
                if (canonical)
                {
                    json.WriteStringValue(JsonSchemaToken.String);
                }
                else
                {
                    json.WriteStartObject();
                    json.WriteString(JsonAttributeToken.Type, JsonSchemaToken.String);
                    json.WriteString(JsonAttributeToken.LogicalType, JsonSchemaToken.Uuid);
                    json.WriteEndObject();
                }

                return(new JsonSchemaWriterCaseResult());
            }
            else
            {
                return(JsonSchemaWriterCaseResult.FromException(new UnsupportedSchemaException(schema, $"{nameof(JsonUuidSchemaWriterCase)} can only be applied to {nameof(StringSchema)}s with {nameof(UuidLogicalType)}.")));
            }
        }
        /// <summary>
        /// Writes an <see cref="EnumSchema" />.
        /// </summary>
        /// <inheritdoc />
        public virtual JsonSchemaWriterCaseResult Write(Schema schema, Utf8JsonWriter json, bool canonical, JsonSchemaWriterContext context)
        {
            if (schema is EnumSchema enumSchema)
            {
                if (context.Names.TryGetValue(enumSchema.FullName, out var existing))
                {
                    if (!schema.Equals(existing))
                    {
                        throw new InvalidSchemaException($"A conflicting schema with the name {enumSchema.FullName} has already been written.");
                    }

                    json.WriteStringValue(enumSchema.FullName);
                }
                else
                {
                    context.Names.Add(enumSchema.FullName, enumSchema);

                    json.WriteStartObject();
                    json.WriteString(JsonAttributeToken.Name, enumSchema.FullName);

                    if (!canonical)
                    {
                        if (enumSchema.Aliases.Count > 0)
                        {
                            json.WritePropertyName(JsonAttributeToken.Aliases);
                            json.WriteStartArray();

                            foreach (var alias in enumSchema.Aliases)
                            {
                                json.WriteStringValue(alias);
                            }

                            json.WriteEndArray();
                        }

                        if (enumSchema.Default is not null)
                        {
                            if (!enumSchema.Symbols.Contains(enumSchema.Default))
                            {
                                throw new InvalidSchemaException($"The default value \"{enumSchema.Default}\" is not a symbol in {enumSchema.FullName}.");
                            }

                            json.WriteString(JsonAttributeToken.Default, enumSchema.Default);
                        }

                        if (!string.IsNullOrEmpty(enumSchema.Documentation))
                        {
                            json.WriteString(JsonAttributeToken.Doc, enumSchema.Documentation);
                        }
                    }

                    json.WriteString(JsonAttributeToken.Type, JsonSchemaToken.Enum);
                    json.WritePropertyName(JsonAttributeToken.Symbols);
                    json.WriteStartArray();

                    foreach (var symbol in enumSchema.Symbols)
                    {
                        json.WriteStringValue(symbol);
                    }

                    json.WriteEndArray();
                    json.WriteEndObject();
                }

                return(new JsonSchemaWriterCaseResult());
            }
            else
            {
                return(JsonSchemaWriterCaseResult.FromException(new UnsupportedSchemaException(schema, $"{nameof(JsonEnumSchemaWriterCase)} can only be applied to {nameof(EnumSchema)}s.")));
            }
        }
 /// <summary>
 /// Writes an <see cref="PrimitiveSchema" />.
 /// </summary>
 /// <inheritdoc />
 public virtual JsonSchemaWriterCaseResult Write(Schema schema, Utf8JsonWriter json, bool canonical, JsonSchemaWriterContext context)
 {
     if (schema is PrimitiveSchema primitiveSchema)
     {
         json.WriteStringValue(primitiveSchema switch
         {
             BooleanSchema _ => JsonSchemaToken.Boolean,
             BytesSchema _ => JsonSchemaToken.Bytes,
             DoubleSchema _ => JsonSchemaToken.Double,
             FloatSchema _ => JsonSchemaToken.Float,
             IntSchema _ => JsonSchemaToken.Int,
             LongSchema _ => JsonSchemaToken.Long,
             NullSchema _ => JsonSchemaToken.Null,
             StringSchema _ => JsonSchemaToken.String,
             _ => throw new UnsupportedSchemaException(schema, $"Unknown primitive schema {schema}."),
         });
Esempio n. 6
0
        /// <summary>
        /// Writes a <see cref="RecordSchema" />.
        /// </summary>
        /// <inheritdoc />
        public virtual JsonSchemaWriterCaseResult Write(Schema schema, Utf8JsonWriter json, bool canonical, JsonSchemaWriterContext context)
        {
            if (schema is RecordSchema recordSchema)
            {
                if (context.Names.TryGetValue(recordSchema.FullName, out var existing))
                {
                    if (!schema.Equals(existing))
                    {
                        throw new InvalidSchemaException($"A conflicting schema with the name {recordSchema.FullName} has already been written.");
                    }

                    json.WriteStringValue(recordSchema.FullName);
                }
                else
                {
                    context.Names.Add(recordSchema.FullName, recordSchema);

                    json.WriteStartObject();
                    json.WriteString(JsonAttributeToken.Name, recordSchema.FullName);

                    if (!canonical)
                    {
                        if (recordSchema.Aliases.Count > 0)
                        {
                            json.WritePropertyName(JsonAttributeToken.Aliases);
                            json.WriteStartArray();

                            foreach (var alias in recordSchema.Aliases)
                            {
                                json.WriteStringValue(alias);
                            }

                            json.WriteEndArray();
                        }

                        if (!string.IsNullOrEmpty(recordSchema.Documentation))
                        {
                            json.WriteString(JsonAttributeToken.Doc, recordSchema.Documentation);
                        }
                    }

                    json.WriteString(JsonAttributeToken.Type, JsonSchemaToken.Record);
                    json.WritePropertyName(JsonAttributeToken.Fields);
                    json.WriteStartArray();

                    foreach (var field in recordSchema.Fields)
                    {
                        json.WriteStartObject();
                        json.WriteString(JsonAttributeToken.Name, field.Name);

                        if (!canonical)
                        {
                            if (field.Default != null)
                            {
                                json.WritePropertyName(JsonAttributeToken.Default);

                                if (field.Default is JsonDefaultValue jsonDefault)
                                {
                                    jsonDefault.Element.WriteTo(json);
                                }
                                else
                                {
                                    // work around lack of JsonSerializer.SerializeToDocument in netstandard2.0:
                                    var serialized = JsonSerializer.SerializeToUtf8Bytes(field.Default.ToObject <object>());
                                    var element    = JsonSerializer.Deserialize <JsonElement>(serialized);
                                    element.WriteTo(json);
                                }
                            }

                            if (!string.IsNullOrEmpty(field.Documentation))
                            {
                                json.WriteString(JsonAttributeToken.Doc, field.Documentation);
                            }
                        }

                        json.WritePropertyName(JsonAttributeToken.Type);
                        Writer.Write(field.Type, json, canonical, context);
                        json.WriteEndObject();
                    }

                    json.WriteEndArray();
                    json.WriteEndObject();
                }

                return(new JsonSchemaWriterCaseResult());
            }
            else
            {
                return(JsonSchemaWriterCaseResult.FromException(new UnsupportedSchemaException(schema, $"{nameof(JsonRecordSchemaWriterCase)} can only be applied to {nameof(RecordSchema)}s.")));
            }
        }
        /// <summary>
        /// Writes a <see cref="UnionSchema" />.
        /// </summary>
        /// <inheritdoc />
        public virtual JsonSchemaWriterCaseResult Write(Schema schema, Utf8JsonWriter json, bool canonical, JsonSchemaWriterContext context)
        {
            if (schema is UnionSchema unionSchema)
            {
                json.WriteStartArray();

                foreach (var child in unionSchema.Schemas)
                {
                    Writer.Write(child, json, canonical, context);
                }

                json.WriteEndArray();

                return(new JsonSchemaWriterCaseResult());
            }
            else
            {
                return(JsonSchemaWriterCaseResult.FromException(new UnsupportedSchemaException(schema, $"{nameof(JsonUnionSchemaWriterCase)} can only be applied to {nameof(UnionSchema)}s.")));
            }
        }