Example #1
0
        /// <summary>
        /// Converts an object to a <see cref="JsonValue"/>.
        /// </summary>
        /// <param name="serializer">The <see cref="JsonSerializer"/> instance to use for additional serialization of values.</param>
        /// <returns>The <see cref="JsonValue"/> representation of the object.</returns>
        public virtual JsonValue ToJson(JsonSerializer serializer)
        {
            if (BooleanSchemaDefinition != null)
            {
                return(BooleanSchemaDefinition);
            }

            serializer = serializer ?? _schemaSerializer;

            var json = new JsonObject();

            if (!string.IsNullOrWhiteSpace(Schema))
            {
                json["$schema"] = Schema;
            }
            if (Id != null)
            {
                json["$id"] = Id;
            }
            if (Comment != null)
            {
                json["$comment"] = Comment;
            }
            if (Title != null)
            {
                json["title"] = Title;
            }
            if (!string.IsNullOrWhiteSpace(Description))
            {
                json["description"] = Description;
            }
            if (Definitions != null)
            {
                json["definitions"] = Definitions.ToJson(serializer);
            }
            if (ReadOnly.HasValue)
            {
                json["readOnly"] = ReadOnly;
            }
            if (Type != JsonSchemaType.NotDefined)
            {
                var array = Type.ToJson();
                if (array.Type == JsonValueType.Array)
                {
                    array.Array.EqualityStandard = ArrayEquality.ContentsEqual;
                }
                json["type"] = array;
            }
            if (Properties != null)
            {
                json["properties"] = Properties.ToJson(serializer);
            }
            if (Maximum.HasValue)
            {
                json["maximum"] = Maximum;
            }
            if (ExclusiveMaximum.HasValue)
            {
                json["exclusiveMaximum"] = ExclusiveMaximum;
            }
            if (Minimum.HasValue)
            {
                json["minimum"] = Minimum;
            }
            if (ExclusiveMinimum.HasValue)
            {
                json["exclusiveMinimum"] = ExclusiveMinimum;
            }
            if (MultipleOf.HasValue)
            {
                json["multipleOf"] = MultipleOf;
            }
            if (MaxLength.HasValue)
            {
                json["maxLength"] = MaxLength;
            }
            if (MinLength.HasValue)
            {
                json["minLength"] = MinLength;
            }
            if (Pattern != null)
            {
                json["pattern"] = Pattern;
            }
            if (AdditionalItems != null)
            {
                json["additionalItems"] = AdditionalItems.ToJson(serializer);
            }
            if (Items != null)
            {
                json["items"] = Items.ToJson(serializer);
            }
            if (MaxItems.HasValue)
            {
                json["maxItems"] = MinItems;
            }
            if (MinItems.HasValue)
            {
                json["minItems"] = MinItems;
            }
            if (UniqueItems ?? false)
            {
                json["uniqueItems"] = UniqueItems;
            }
            if (Contains != null)
            {
                json["contains"] = Contains.ToJson(serializer);
            }
            if (MaxProperties.HasValue)
            {
                json["maxProperties"] = MaxProperties;
            }
            if (MinProperties.HasValue)
            {
                json["minProperties"] = MinProperties;
            }
            if (Required != null)
            {
                json["required"] = Required.ToJson();
            }
            if (AdditionalProperties != null)
            {
                json["additionalProperties"] = AdditionalProperties.ToJson(serializer);
            }
            if (PatternProperties != null && PatternProperties.Any())
            {
                json["patternProperties"] = PatternProperties.ToDictionary(kvp => kvp.Key.ToString(), kvp => kvp.Value).ToJson(serializer);
            }
            if (Dependencies != null && Dependencies.Any())
            {
                var jsonDependencies = new JsonObject();
                foreach (var dependency in Dependencies)
                {
                    jsonDependencies[dependency.PropertyName] = dependency.GetJsonData();
                }
                json["dependencies"] = jsonDependencies;
            }
            if (PropertyNames != null)
            {
                json["propertyNames"] = PropertyNames.ToJson(serializer);
            }
            if (Const != null)
            {
                json["const"] = Const;
            }
            if (Enum != null)
            {
                var array = Enum.ToJson(serializer);
                array.Array.EqualityStandard = ArrayEquality.ContentsEqual;
                json["enum"] = Enum.ToJson(serializer);
            }
            if (Format != null)
            {
                json["format"] = Format.Key;
            }
            if (ContentMediaType != null)
            {
                json["contentMediaType"] = ContentMediaType;
            }
            if (ContentEncoding != null)
            {
                json["contentEncoding"] = serializer.Serialize(ContentEncoding);
            }
            if (If != null)
            {
                json["if"] = If.ToJson(serializer);
            }
            if (Then != null)
            {
                json["then"] = Then.ToJson(serializer);
            }
            if (Else != null)
            {
                json["else"] = Else.ToJson(serializer);
            }
            if (AllOf != null)
            {
                var array = AllOf.Select(s => s.ToJson(serializer)).ToJson();
                array.EqualityStandard = ArrayEquality.ContentsEqual;
                json["allOf"]          = array;
            }
            if (AnyOf != null)
            {
                var array = AnyOf.Select(s => s.ToJson(serializer)).ToJson();
                array.EqualityStandard = ArrayEquality.ContentsEqual;
                json["anyOf"]          = array;
            }
            if (OneOf != null)
            {
                var array = OneOf.Select(s => s.ToJson(serializer)).ToJson();
                array.EqualityStandard = ArrayEquality.ContentsEqual;
                json["oneOf"]          = array;
            }
            if (Not != null)
            {
                json["not"] = Not.ToJson(serializer);
            }
            if (Default != null)
            {
                json["default"] = Default;
            }
            if (Examples != null)
            {
                json["examples"] = Examples;
            }
            if (ExtraneousDetails != null)
            {
                foreach (var kvp in ExtraneousDetails.Where(kvp => !_definedProperties.Contains(kvp.Key)))
                {
                    json[kvp.Key] = kvp.Value;
                }
            }
            return(json);
        }