Newtonsoft.Json.Schema.JsonSchemaModelBuilder.AddSchema C# (CSharp) Method

AddSchema() public method

public AddSchema ( Newtonsoft.Json.Schema.JsonSchemaNode existingNode, JsonSchema schema ) : Newtonsoft.Json.Schema.JsonSchemaNode
existingNode Newtonsoft.Json.Schema.JsonSchemaNode
schema JsonSchema
return Newtonsoft.Json.Schema.JsonSchemaNode
    public JsonSchemaNode AddSchema(JsonSchemaNode existingNode, JsonSchema schema)
    {
      string newId;
      if (existingNode != null)
      {
        if (existingNode.Schemas.Contains(schema))
          return existingNode;

        newId = JsonSchemaNode.GetId(existingNode.Schemas.Union(new[] { schema }));
      }
      else
      {
        newId = JsonSchemaNode.GetId(new[] { schema });
      }

      if (_nodes.Contains(newId))
        return _nodes[newId];

      JsonSchemaNode currentNode = (existingNode != null)
        ? existingNode.Combine(schema)
        : new JsonSchemaNode(schema);

      _nodes.Add(currentNode);

      if (schema.Properties != null)
      {
        foreach (KeyValuePair<string, JsonSchema> property in schema.Properties)
        {
          AddProperty(currentNode, property.Key, property.Value);
        }
      }

      if (schema.Items != null)
      {
        for (int i = 0; i < schema.Items.Count; i++)
        {
          AddItem(currentNode, i, schema.Items[i]);
        }
      }

      if (schema.AdditionalProperties != null)
        AddAdditionalProperties(currentNode, schema.AdditionalProperties);

      if (schema.Extends != null)
        currentNode = AddSchema(currentNode, schema.Extends);

      return currentNode;
    }

Usage Example

コード例 #1
0
        public void AddSchema()
        {
            string first = @"{
  ""id"":""first"",
  ""type"":""object"",
  ""properties"":
  {
    ""firstproperty"":{""type"":""string"",""maxLength"":10},
    ""secondproperty"":{
      ""type"":""object"",
      ""properties"":
      {
        ""secondproperty_firstproperty"":{""type"":""string"",""maxLength"":10,""minLength"":7}
      }
    }
  },
  ""additionalProperties"":{}
}";

            string second = @"{
  ""id"":""second"",
  ""type"":""object"",
  ""extends"":{""$ref"":""first""},
  ""properties"":
  {
    ""firstproperty"":{""type"":""string""},
    ""secondproperty"":{
      ""extends"":{
        ""properties"":
        {
          ""secondproperty_firstproperty"":{""maxLength"":9,""minLength"":6}
        }
      },
      ""type"":""object"",
      ""properties"":
      {
        ""secondproperty_firstproperty"":{}
      }
    },
    ""thirdproperty"":{""type"":""string""}
  },
  ""additionalProperties"":false
}";

            JsonSchemaResolver resolver = new JsonSchemaResolver();
            JsonSchema firstSchema = JsonSchema.Parse(first, resolver);
            JsonSchema secondSchema = JsonSchema.Parse(second, resolver);

            JsonSchemaModelBuilder modelBuilder = new JsonSchemaModelBuilder();

            JsonSchemaNode node = modelBuilder.AddSchema(null, secondSchema);

            Assert.AreEqual(2, node.Schemas.Count);
            Assert.AreEqual(2, node.Properties["firstproperty"].Schemas.Count);
            Assert.AreEqual(3, node.Properties["secondproperty"].Schemas.Count);
            Assert.AreEqual(3, node.Properties["secondproperty"].Properties["secondproperty_firstproperty"].Schemas.Count);
        }
All Usage Examples Of Newtonsoft.Json.Schema.JsonSchemaModelBuilder::AddSchema