JsonSchemaDefinitionAppender
Appends a JSON Schema to the Definitions of another JSON Schema.
using System;
namespace NJsonSchema
{
public class JsonSchemaDefinitionAppender : ISchemaDefinitionAppender
{
private readonly ITypeNameGenerator _typeNameGenerator;
public object RootObject { get; set; }
public JsonSchemaDefinitionAppender(object rootObject, ITypeNameGenerator typeNameGenerator)
{
RootObject = rootObject;
_typeNameGenerator = typeNameGenerator;
}
public void Append(JsonSchema4 objectToAppend)
{
JsonSchema4 jsonSchema = RootObject as JsonSchema4;
if (jsonSchema != null && objectToAppend != null) {
string typeName = objectToAppend.GetTypeName(_typeNameGenerator);
if (!string.IsNullOrEmpty(typeName) && !jsonSchema.Definitions.ContainsKey(typeName))
jsonSchema.Definitions[typeName] = objectToAppend;
else
jsonSchema.Definitions["ref_" + Guid.NewGuid().ToString().Replace("-", "_")] = objectToAppend;
return;
}
throw new InvalidOperationException("Could not find the JSON path of a child object.");
}
}
}