Пример #1
0
        public void Serialize(JsonWriter writer)
        {
            writer.WriteStartObject();

            if (!string.IsNullOrWhiteSpace(Title))
            {
                writer.WritePropertyName("title");
                writer.WriteValue(Title);
            }
            if (!string.IsNullOrWhiteSpace(Description))
            {
                writer.WritePropertyName("description");
                writer.WriteValue(Description);
            }
            writer.WritePropertyName("required");
            writer.WriteValue(Required);
            if (TypeFormat.Type != ParameterType.Unknown)
            {
                writer.WritePropertyName("type");
                writer.WriteValue(TypeFormat.Type.ToString().ToLower());
                if (!string.IsNullOrWhiteSpace(TypeFormat.Format))
                {
                    writer.WritePropertyName("format");
                    writer.WriteValue(TypeFormat.Format);
                }
            }
            writer.WritePropertyName("allowEmptyValue");
            writer.WriteValue(AllowEmptyValue);
            if (TypeFormat.Type == ParameterType.Array && Items != null)
            {
                writer.WritePropertyName("items");
                Items.Serialize(writer);
            }
            if (TypeFormat.Type == ParameterType.Array)
            {
                writer.WritePropertyName("collectionFormat");
                writer.WriteValue(CollectionFormat);
            }
            if (!string.IsNullOrWhiteSpace(Default))
            {
                writer.WritePropertyName("default");
                writer.WriteValue(Default);
            }
            if (Maximum != decimal.MaxValue)
            {
                writer.WritePropertyName("maximum");
                writer.WriteValue(Maximum);
                writer.WritePropertyName("exclusiveMaximum");
                writer.WriteValue(ExclusiveMaximum);
            }
            if (Minimum != decimal.MinValue)
            {
                writer.WritePropertyName("minimum");
                writer.WriteValue(Minimum);
                writer.WritePropertyName("exclusiveMinimum");
                writer.WriteValue(ExclusiveMinimum);
            }
            if (MaxLength != int.MaxValue)
            {
                writer.WritePropertyName("maxLength");
                writer.WriteValue(MaxLength);
            }
            if (MinLength != int.MinValue)
            {
                writer.WritePropertyName("minLength");
                writer.WriteValue(MinLength);
            }
            if (!string.IsNullOrWhiteSpace(Pattern))
            {
                writer.WritePropertyName("pattern");
                writer.WriteValue(Pattern);
            }
            if (MaxItems != int.MaxValue)
            {
                writer.WritePropertyName("maxItems");
                writer.WriteValue(MaxItems);
            }
            if (MinItems != int.MinValue)
            {
                writer.WritePropertyName("minItems");
                writer.WriteValue(MinItems);
            }
            writer.WritePropertyName("uniqueItems");
            writer.WriteValue(UniqueItems);
            if (Enum != null && Enum.Any())
            {
                writer.WritePropertyName("enum");
                writer.WriteStartArray();
                foreach (string e in Enum)
                {
                    writer.WriteValue(e);
                }
                writer.WriteEndArray();
            }
            if (MultipleOf != decimal.MinValue)
            {
                writer.WritePropertyName("multipleOf");
                writer.WriteValue(MultipleOf);
            }

            writer.WriteEndObject();
        }
Пример #2
0
        public override void Serialize(JsonWriter writer, bool skipStartEndObject)
        {
            if (!skipStartEndObject)
            {
                writer.WriteStartObject();
            }

            if (!string.IsNullOrWhiteSpace(Name))
            {
                writer.WritePropertyName("name");
                writer.WriteValue(Name);
            }
            if (In != InType.Unknown)
            {
                writer.WritePropertyName("in");
                writer.WriteValue(In.ToString().ToLower());
            }
            if (!string.IsNullOrWhiteSpace(Description))
            {
                writer.WritePropertyName("description");
                writer.WriteValue(Description);
            }
            if (Required)
            {
                writer.WritePropertyName("required");
                writer.WriteValue(Required);
            }
            if (TypeFormat.Type != ParameterType.Unknown)
            {
                if (In == InType.Body)
                {
                    writer.WritePropertyName("schema");
                    writer.WriteStartObject();

                    writer.WritePropertyName("type");
                    writer.WriteValue(TypeFormat.Type.ToString().ToLower());

                    if (TypeFormat.Type == ParameterType.Array && Items != null)
                    {
                        writer.WritePropertyName("items");

                        writer.WriteStartObject();

                        writer.WritePropertyName("$ref");
                        writer.WriteValue($"#/definitions/{Items?.Ref ?? (Items?.Items as ParameterSchema)?.SchemaRef}");
                        //writer.WriteValue($"#/definitions/{Items?.Ref}");

                        writer.WriteEndObject();
                    }

                    if (!string.IsNullOrWhiteSpace(TypeFormat.Format))
                    {
                        writer.WritePropertyName("format");
                        writer.WriteValue(TypeFormat.Format);
                    }

                    writer.WriteEndObject();
                }
                else
                {
                    writer.WritePropertyName("type");
                    writer.WriteValue(TypeFormat.Type.ToString().ToLower());
                    if (!string.IsNullOrWhiteSpace(TypeFormat.Format))
                    {
                        writer.WritePropertyName("format");
                        writer.WriteValue(TypeFormat.Format);
                    }
                }
            }
            if (AllowEmptyValue)
            {
                writer.WritePropertyName("allowEmptyValue");
                writer.WriteValue(AllowEmptyValue);
            }
            if (In != InType.Body && TypeFormat.Type == ParameterType.Array && Items != null)
            {
                writer.WritePropertyName("items");
                Items.Serialize(writer);
            }
            if (In != InType.Body && TypeFormat.Type == ParameterType.Array && Items != null)
            {
                writer.WritePropertyName("collectionFormat");
                writer.WriteValue(CollectionFormat);
            }
            if (!string.IsNullOrWhiteSpace(Default))
            {
                writer.WritePropertyName("default");
                writer.WriteValue(Default);
            }
            if (Maximum != decimal.MaxValue)
            {
                writer.WritePropertyName("maximum");
                writer.WriteValue(Maximum);
                writer.WritePropertyName("exclusiveMaximum");
                writer.WriteValue(ExclusiveMaximum);
            }
            if (Minimum != decimal.MinValue)
            {
                writer.WritePropertyName("minimum");
                writer.WriteValue(Minimum);
                writer.WritePropertyName("exclusiveMinimum");
                writer.WriteValue(ExclusiveMinimum);
            }
            if (MaxLength != int.MaxValue)
            {
                writer.WritePropertyName("maxLength");
                writer.WriteValue(MaxLength);
            }
            if (MinLength != int.MinValue)
            {
                writer.WritePropertyName("minLength");
                writer.WriteValue(MinLength);
            }
            if (!string.IsNullOrWhiteSpace(Pattern))
            {
                writer.WritePropertyName("pattern");
                writer.WriteValue(Pattern);
            }
            if (MaxItems != int.MaxValue)
            {
                writer.WritePropertyName("maxItems");
                writer.WriteValue(MaxItems);
            }
            if (MinItems != int.MinValue)
            {
                writer.WritePropertyName("minItems");
                writer.WriteValue(MinItems);
            }
            if (UniqueItems)
            {
                writer.WritePropertyName("uniqueItems");
                writer.WriteValue(UniqueItems);
            }
            if (Enum != null && Enum.Any())
            {
                writer.WritePropertyName("enum");
                writer.WriteStartArray();
                foreach (string e in Enum)
                {
                    writer.WriteValue(e);
                }
                writer.WriteEndArray();
            }
            if (MultipleOf != decimal.MinValue)
            {
                writer.WritePropertyName("multipleOf");
                writer.WriteValue(MultipleOf);
            }

            if (!skipStartEndObject)
            {
                writer.WriteEndObject();
            }
        }
Пример #3
0
        public void Serialize(JsonWriter writer)
        {
            writer.WritePropertyName(Title);
            writer.WriteStartObject();

            if (!string.IsNullOrWhiteSpace(Description))
            {
                writer.WritePropertyName("description");
                writer.WriteValue(Description);
            }
            if (TypeFormat.Type == ParameterType.Object)
            {
                writer.WritePropertyName("$ref");
                writer.WriteValue(string.Format("#/definitions/{0}", Ref));
            }
            else
            {
                if (TypeFormat.Type != ParameterType.Unknown)
                {
                    writer.WritePropertyName("type");
                    writer.WriteValue(TypeFormat.Type.ToString().ToLower());
                    if (!string.IsNullOrWhiteSpace(TypeFormat.Format))
                    {
                        writer.WritePropertyName("format");
                        writer.WriteValue(TypeFormat.Format);
                    }
                }
                if (TypeFormat.Type == ParameterType.Array && Items != null)
                {
                    writer.WritePropertyName("items");
                    Items.Serialize(writer);
                }
                if (!string.IsNullOrWhiteSpace(Default))
                {
                    writer.WritePropertyName("default");
                    writer.WriteValue(Default);
                }
                if (Example != null)
                {
                    writer.WritePropertyName("example");
                    if (Example.GetType().IsArray)
                    {
                        writer.WriteStartArray();
                        foreach (var value in (object[])Example)
                        {
                            writer.WriteValue(value);
                        }
                        writer.WriteEndArray();
                    }
                    else
                    {
                        writer.WriteValue(Example);
                    }
                }
                if (Maximum != decimal.MaxValue)
                {
                    writer.WritePropertyName("maximum");
                    writer.WriteValue(Maximum);
                    writer.WritePropertyName("exclusiveMaximum");
                    writer.WriteValue(ExclusiveMaximum);
                }
                if (Minimum != decimal.MinValue)
                {
                    writer.WritePropertyName("minimum");
                    writer.WriteValue(Minimum);
                    writer.WritePropertyName("exclusiveMinimum");
                    writer.WriteValue(ExclusiveMinimum);
                }
                if (MaxLength != int.MaxValue)
                {
                    writer.WritePropertyName("maxLength");
                    writer.WriteValue(MaxLength);
                }
                if (MinLength != int.MinValue)
                {
                    writer.WritePropertyName("minLength");
                    writer.WriteValue(MinLength);
                }
                if (!string.IsNullOrWhiteSpace(Pattern))
                {
                    writer.WritePropertyName("pattern");
                    writer.WriteValue(Pattern);
                }
                if (MaxItems != int.MaxValue)
                {
                    writer.WritePropertyName("maxItems");
                    writer.WriteValue(MaxItems);
                }
                if (MinItems != int.MinValue)
                {
                    writer.WritePropertyName("minItems");
                    writer.WriteValue(MinItems);
                }
                writer.WritePropertyName("uniqueItems");
                writer.WriteValue(UniqueItems);
                if (Enum != null && Enum.Any())
                {
                    writer.WritePropertyName("enum");
                    writer.WriteStartArray();
                    foreach (int e in Enum)
                    {
                        writer.WriteValue(e);
                    }
                    writer.WriteEndArray();
                }
                if (MultipleOf != decimal.MinValue)
                {
                    writer.WritePropertyName("multipleOf");
                    writer.WriteValue(MultipleOf);
                }
            }

            writer.WriteEndObject();
        }
        public override void Serialize(JsonWriter writer, bool skipStartEndObject)
        {
            if (!skipStartEndObject)
            {
                writer.WriteStartObject();
            }

            if (!string.IsNullOrWhiteSpace(Name))
            {
                writer.WritePropertyName("name");
                writer.WriteValue(Name);
            }
            if (In != InType.Unknown)
            {
                writer.WritePropertyName("in");
                writer.WriteValue(In.ToString().ToLower());
            }
            if (!string.IsNullOrWhiteSpace(Description))
            {
                writer.WritePropertyName("description");
                writer.WriteValue(Description);
            }
            if (Required)
            {
                writer.WritePropertyName("required");
                writer.WriteValue(Required);
            }
            if (TypeFormat.Type != ParameterType.Unknown)
            {
                writer.WritePropertyName("type");
                writer.WriteValue(TypeFormat.Type.ToString().ToLower());
                if (!string.IsNullOrWhiteSpace(TypeFormat.Format))
                {
                    writer.WritePropertyName("format");
                    writer.WriteValue(TypeFormat.Format);
                }
            }
            if (AllowEmptyValue)
            {
                writer.WritePropertyName("allowEmptyValue");
                writer.WriteValue(AllowEmptyValue);
            }
            if (TypeFormat.Type == ParameterType.Array && Items != null)
            {
                writer.WritePropertyName("items");
                Items.Serialize(writer);
            }
            if (TypeFormat.Type == ParameterType.Array)
            {
                writer.WritePropertyName("collectionFormat");
                writer.WriteValue(CollectionFormat);
            }
            if (!string.IsNullOrWhiteSpace(Default))
            {
                writer.WritePropertyName("default");

                // if it's primitive, then we need to render it as primitive
                switch (TypeFormat.Type)
                {
                case ParameterType.Boolean:
                    writer.WriteValue(Boolean.Parse(Default));
                    break;

                case ParameterType.Integer:
                    writer.WriteValue(Int64.Parse(Default));
                    break;

                case ParameterType.Number:
                    writer.WriteValue(Double.Parse(Default));
                    break;

                default:
                    writer.WriteValue(Default);
                    break;
                }
            }
            if (Maximum != decimal.MaxValue)
            {
                writer.WritePropertyName("maximum");
                writer.WriteValue(Maximum);
                writer.WritePropertyName("exclusiveMaximum");
                writer.WriteValue(ExclusiveMaximum);
            }
            if (Minimum != decimal.MinValue)
            {
                writer.WritePropertyName("minimum");
                writer.WriteValue(Minimum);
                writer.WritePropertyName("exclusiveMinimum");
                writer.WriteValue(ExclusiveMinimum);
            }
            if (MaxLength != int.MaxValue)
            {
                writer.WritePropertyName("maxLength");
                writer.WriteValue(MaxLength);
            }
            if (MinLength != int.MinValue)
            {
                writer.WritePropertyName("minLength");
                writer.WriteValue(MinLength);
            }
            if (!string.IsNullOrWhiteSpace(Pattern))
            {
                writer.WritePropertyName("pattern");
                writer.WriteValue(Pattern);
            }
            if (MaxItems != int.MaxValue)
            {
                writer.WritePropertyName("maxItems");
                writer.WriteValue(MaxItems);
            }
            if (MinItems != int.MinValue)
            {
                writer.WritePropertyName("minItems");
                writer.WriteValue(MinItems);
            }
            if (UniqueItems)
            {
                writer.WritePropertyName("uniqueItems");
                writer.WriteValue(UniqueItems);
            }
            if (Enum != null && Enum.Any())
            {
                writer.WritePropertyName("enum");
                writer.WriteStartArray();
                foreach (string e in Enum)
                {
                    writer.WriteValue(e);
                }
                writer.WriteEndArray();
            }
            if (MultipleOf != decimal.MinValue)
            {
                writer.WritePropertyName("multipleOf");
                writer.WriteValue(MultipleOf);
            }

            if (!skipStartEndObject)
            {
                writer.WriteEndObject();
            }
        }