public void Simple()
    {
      string json = @"
{
  ""description"": ""A person"",
  ""type"": ""object"",
  ""properties"":
  {
    ""name"": {""type"":""string""},
    ""hobbies"": {
      ""type"": ""array"",
      ""items"": {""type"":""string""}
    }
  }
}
";

      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));

      Assert.AreEqual("A person", schema.Description);
      Assert.AreEqual(JsonSchemaType.Object, schema.Type);

      Assert.AreEqual(2, schema.Properties.Count);

      Assert.AreEqual(JsonSchemaType.String, schema.Properties["name"].Type);
      Assert.AreEqual(JsonSchemaType.Array, schema.Properties["hobbies"].Type);
      Assert.AreEqual(JsonSchemaType.String, schema.Properties["hobbies"].Items[0].Type);
    }
    public void MultipleTypes()
    {
      string json = @"{
  ""description"":""Age"",
  ""type"":[""string"", ""integer""]
}";

      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));

      Assert.AreEqual("Age", schema.Description);
      Assert.AreEqual(JsonSchemaType.String | JsonSchemaType.Integer, schema.Type);
    }
示例#3
0
        private void WriteType(string propertyName, JsonWriter writer, JsonSchemaType type)
        {
            IList <JsonSchemaType> types;

            if (System.Enum.IsDefined(typeof(JsonSchemaType), type))
            {
                types = new List <JsonSchemaType> {
                    type
                };
            }
            else
            {
                List <JsonSchemaType> tempTypesList = new List <JsonSchemaType> ();
                var iterator = EnumUtils.GetFlagsValues(type).GetEnumerator();
                while (iterator.MoveNext())
                {
                    if (iterator.Current != JsonSchemaType.None)
                    {
                        tempTypesList.Add(iterator.Current);
                    }
                }

                types = tempTypesList;
                //types = EnumUtils.GetFlagsValues (type).Where (v => v != JsonSchemaType.None).ToList ();
            }
            if (types.Count == 0)
            {
                return;
            }

            writer.WritePropertyName(propertyName);

            if (types.Count == 1)
            {
                writer.WriteValue(JsonSchemaBuilder.MapType(types[0]));
                return;
            }

            writer.WriteStartArray();
            foreach (JsonSchemaType jsonSchemaType in types)
            {
                writer.WriteValue(JsonSchemaBuilder.MapType(jsonSchemaType));
            }
            writer.WriteEndArray();
        }
示例#4
0
        // Token: 0x06001174 RID: 4468 RVA: 0x000612A4 File Offset: 0x0005F4A4
        private JsonSchemaType?ProcessType(JToken token)
        {
            JTokenType type = token.Type;

            if (type == JTokenType.Array)
            {
                JsonSchemaType?jsonSchemaType = new JsonSchemaType?(JsonSchemaType.None);
                foreach (JToken jtoken in ((IEnumerable <JToken>)token))
                {
                    if (jtoken.Type != JTokenType.String)
                    {
                        throw JsonException.Create(jtoken, jtoken.Path, "Expected JSON schema type string token, got {0}.".FormatWith(CultureInfo.InvariantCulture, token.Type));
                    }
                    jsonSchemaType |= JsonSchemaBuilder.MapType((string)jtoken);
                }
                return(jsonSchemaType);
            }
            if (type != JTokenType.String)
            {
                throw JsonException.Create(token, token.Path, "Expected array or JSON schema type string token, got {0}.".FormatWith(CultureInfo.InvariantCulture, token.Type));
            }
            return(new JsonSchemaType?(JsonSchemaBuilder.MapType((string)token)));
        }
        private void WriteType(string propertyName, JsonWriter writer, JsonSchemaType type)
        {
            IList <JsonSchemaType> list;

            if (Enum.IsDefined(typeof(JsonSchemaType), type))
            {
                List <JsonSchemaType> list1 = new List <JsonSchemaType> {
                    type
                };
                list = list1;
            }
            else
            {
                if (< > c.< > 9__7_0 == null)
                {
                }
                list = EnumUtils.GetFlagsValues <JsonSchemaType>(type).Where <JsonSchemaType>((< > c.< > 9__7_0 = new Func <JsonSchemaType, bool>(< > c.< > 9. < WriteType > b__7_0))).ToList <JsonSchemaType>();
            }
            if (list.Count != 0)
            {
                writer.WritePropertyName(propertyName);
                if (list.Count == 1)
                {
                    writer.WriteValue(JsonSchemaBuilder.MapType(list[0]));
                }
                else
                {
                    writer.WriteStartArray();
                    foreach (JsonSchemaType type2 in list)
                    {
                        writer.WriteValue(JsonSchemaBuilder.MapType(type2));
                    }
                    writer.WriteEndArray();
                }
            }
        }
示例#6
0
        private void WriteType(string propertyName, JsonWriter writer, JsonSchemaType type)
        {
            IList <JsonSchemaType> types;

            if (System.Enum.IsDefined(typeof(JsonSchemaType), type))
            {
                types = new List <JsonSchemaType> {
                    type
                }
            }
            ;
            else
            {
                types = EnumUtils.GetFlagsValues(type).Where(v => v != JsonSchemaType.None).ToList();
            }

            if (types.Count == 0)
            {
                return;
            }

            writer.WritePropertyName(propertyName);

            if (types.Count == 1)
            {
                writer.WriteValue(JsonSchemaBuilder.MapType(types[0]));
                return;
            }

            writer.WriteStartArray();
            foreach (JsonSchemaType jsonSchemaType in types)
            {
                writer.WriteValue(JsonSchemaBuilder.MapType(jsonSchemaType));
            }
            writer.WriteEndArray();
        }
    public void IdentityMultiple()
    {
      string json = @"{
  ""description"":""Identity"",
  ""identity"":[""PurpleMonkeyDishwasher"",""Antelope""]
}";

      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));

      Assert.AreEqual("Identity", schema.Description);
      Assert.AreEqual(2, schema.Identity.Count);
      Assert.AreEqual("PurpleMonkeyDishwasher", schema.Identity[0]);
      Assert.AreEqual("Antelope", schema.Identity[1]);
    }
    public void Requires()
    {
      string json = @"{
  ""description"":""Requires"",
  ""requires"":""PurpleMonkeyDishwasher""
}";

      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));

      Assert.AreEqual("Requires", schema.Description);
      Assert.AreEqual("PurpleMonkeyDishwasher", schema.Requires);
    }
    public void Format()
    {
      string json = @"{
  ""description"":""Format"",
  ""format"":""testformat""
}";

      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));

      Assert.AreEqual("Format", schema.Description);
      Assert.AreEqual("testformat", schema.Format);
    }
    public void ExclusiveMinimum_ExclusiveMaximum()
    {
      string json = @"{
  ""exclusiveMinimum"":true,
  ""exclusiveMaximum"":true
}";

      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
      JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));

      Assert.AreEqual(true, schema.ExclusiveMinimum);
      Assert.AreEqual(true, schema.ExclusiveMaximum);
    }
    public void AllowAdditionalItems()
    {
      string json = @"{
    ""items"": {},
    ""additionalItems"": false
}";

      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
      JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));

      Assert.IsNull(schema.AdditionalItems);
      Assert.AreEqual(false, schema.AllowAdditionalItems);
    }
    public void UnresolvedReference()
    {
      ExceptionAssert.Throws<Exception>(@"Could not resolve schema reference 'MyUnresolvedReference'.",
        () =>
          {
            string json = @"{
  ""id"":""CircularReferenceArray"",
  ""description"":""CircularReference"",
  ""type"":[""array""],
  ""items"":{""$ref"":""MyUnresolvedReference""}
}";

            JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
            JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
          });
    }
    public void DefaultComplexType()
    {
      string json = @"{
  ""description"":""DefaultComplexType"",
  ""default"":{""pie"":true}
}";

      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));

      Assert.AreEqual("DefaultComplexType", schema.Description);
      Assert.IsTrue(JToken.DeepEquals(JObject.Parse(@"{""pie"":true}"), schema.Default));
    }
    public void Enum()
    {
      string json = @"{
  ""description"":""Type"",
  ""type"":[""string"",""array""],
  ""enum"":[""string"",""object"",""array"",""boolean"",""number"",""integer"",""null"",""any""]
}";

      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));

      Assert.AreEqual("Type", schema.Description);
      Assert.AreEqual(JsonSchemaType.String | JsonSchemaType.Array, schema.Type);

      Assert.AreEqual(8, schema.Enum.Count);
      Assert.AreEqual("string", (string)schema.Enum[0]);
      Assert.AreEqual("any", (string)schema.Enum[schema.Enum.Count - 1]);
    }
    public void PatternProperties()
    {
      string json = @"{
  ""patternProperties"": {
    ""[abc]"": { ""id"":""Blah"" }
  }
}";

      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
      JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));

      Assert.IsNotNull(schema.PatternProperties);
      Assert.AreEqual(1, schema.PatternProperties.Count);
      Assert.AreEqual("Blah", schema.PatternProperties["[abc]"].Id);
    }
    public void DisallowMultipleTypes()
    {
      string json = @"{
  ""description"":""DisallowMultipleTypes"",
  ""disallow"":[""string"",""number""]
}";

      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));

      Assert.AreEqual("DisallowMultipleTypes", schema.Description);
      Assert.AreEqual(JsonSchemaType.String | JsonSchemaType.Float, schema.Disallow);
    }
    public void EscapedReferences()
    {
      string json = @"{
            ""tilda~field"": {""type"": ""integer""},
            ""slash/field"": {""type"": ""object""},
            ""percent%field"": {""type"": ""array""},
            ""properties"": {
                ""tilda"": {""$ref"": ""#/tilda~0field""},
                ""slash"": {""$ref"": ""#/slash~1field""},
                ""percent"": {""$ref"": ""#/percent%25field""}
            }
        }";

      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
      JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));

      Assert.AreEqual(JsonSchemaType.Integer, schema.Properties["tilda"].Type);
      Assert.AreEqual(JsonSchemaType.Object, schema.Properties["slash"].Type);
      Assert.AreEqual(JsonSchemaType.Array, schema.Properties["percent"].Type);
    }
    public void Reference_NonStandardLocation()
    {
      string json = @"{
  ""properties"": {
    ""bar"": {""$ref"": ""#/common/foo""},
    ""foo"": {""$ref"": ""#/common/foo""}
  },
  ""common"": {
    ""foo"": {""type"": ""integer""}
  }
}";

      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
      JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));

      Assert.AreEqual(schema.Properties["foo"], schema.Properties["bar"]);
    }
    public void Location()
    {
      string json = @"{
  ""properties"":{
    ""foo"":{
      ""type"":""array"",
      ""items"":[
        {
          ""type"":""integer""
        },
        {
          ""properties"":{
            ""foo"":{
              ""type"":""integer""
            }
          }
        }
      ]
    }
  }
}";

      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
      JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));

      Assert.AreEqual("#", schema.Location);
      Assert.AreEqual("#/properties/foo", schema.Properties["foo"].Location);
      Assert.AreEqual("#/properties/foo/items/1/properties/foo", schema.Properties["foo"].Items[1].Properties["foo"].Location);
    }
    public void MinimumMaximum()
    {
      string json = @"{
  ""description"":""MinimumMaximum"",
  ""minimum"":1.1,
  ""maximum"":1.2,
  ""minItems"":1,
  ""maxItems"":2,
  ""minLength"":5,
  ""maxLength"":50,
  ""maxDecimal"":3,
}";

      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));

      Assert.AreEqual("MinimumMaximum", schema.Description);
      Assert.AreEqual(1.1, schema.Minimum);
      Assert.AreEqual(1.2, schema.Maximum);
      Assert.AreEqual(1, schema.MinimumItems);
      Assert.AreEqual(2, schema.MaximumItems);
      Assert.AreEqual(5, schema.MinimumLength);
      Assert.AreEqual(50, schema.MaximumLength);
      Assert.AreEqual(3, schema.MaximumDecimals);
    }
    public void CircularReference()
    {
      string json = @"{
  ""id"":""CircularReferenceArray"",
  ""description"":""CircularReference"",
  ""type"":[""array""],
  ""items"":{""$ref"":""CircularReferenceArray""}
}";

      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));

      Assert.AreEqual("CircularReference", schema.Description);
      Assert.AreEqual("CircularReferenceArray", schema.Id);
      Assert.AreEqual(JsonSchemaType.Array, schema.Type);

      Assert.AreEqual(schema, schema.Items[0]);
    }
    public void DisallowSingleType()
    {
      string json = @"{
  ""description"":""DisallowSingleType"",
  ""disallow"":""string""
}";

      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));

      Assert.AreEqual("DisallowSingleType", schema.Description);
      Assert.AreEqual(JsonSchemaType.String, schema.Disallow);
    }
    public void AdditionalItems()
    {
      string json = @"{
    ""items"": [],
    ""additionalItems"": {""type"": ""integer""}
}";

      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
      JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));

      Assert.IsNotNull(schema.AdditionalItems);
      Assert.AreEqual(JsonSchemaType.Integer, schema.AdditionalItems.Type);
      Assert.AreEqual(true, schema.AllowAdditionalItems);
    }
    public void DefaultPrimitiveType()
    {
      string json = @"{
  ""description"":""DefaultPrimitiveType"",
  ""default"":1.1
}";

      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));

      Assert.AreEqual("DefaultPrimitiveType", schema.Description);
      Assert.AreEqual(1.1, (double)schema.Default);
    }
    public void AdditionalProperties()
    {
      string json = @"{
  ""description"":""AdditionalProperties"",
  ""type"":[""string"", ""integer""],
  ""additionalProperties"":{""type"":[""object"", ""boolean""]}
}";

      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));

      Assert.AreEqual("AdditionalProperties", schema.Description);
      Assert.AreEqual(JsonSchemaType.Object | JsonSchemaType.Boolean, schema.AdditionalProperties.Type);
    }
    public void Options()
    {
      string json = @"{
  ""description"":""NZ Island"",
  ""type"":""string"",
  ""options"":
  [
    {""value"":""NI"",""label"":""North Island""},
    {""value"":""SI"",""label"":""South Island""}
  ]
}";

      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));

      Assert.AreEqual("NZ Island", schema.Description);
      Assert.AreEqual(JsonSchemaType.String, schema.Type);

      Assert.AreEqual(2, schema.Options.Count);
      Assert.AreEqual("North Island", schema.Options[new JValue("NI")]);
      Assert.AreEqual("South Island", schema.Options[new JValue("SI")]);
    }
    public void ReadOnly()
    {
      string json = @"{
  ""description"":""ReadOnly"",
  ""readonly"":true
}";

      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));

      Assert.AreEqual("ReadOnly", schema.Description);
      Assert.AreEqual(true, schema.ReadOnly);
    }
    public void Hidden()
    {
      string json = @"{
  ""description"":""Hidden"",
  ""hidden"":true
}";

      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));

      Assert.AreEqual("Hidden", schema.Description);
      Assert.AreEqual(true, schema.Hidden);
    }
    public void Id()
    {
      string json = @"{
  ""description"":""Id"",
  ""id"":""testid""
}";

      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));

      Assert.AreEqual("Id", schema.Description);
      Assert.AreEqual("testid", schema.Id);
    }
    public void UnresolvedReference()
    {
      string json = @"{
  ""id"":""CircularReferenceArray"",
  ""description"":""CircularReference"",
  ""type"":[""array""],
  ""items"":{""$ref"":""MyUnresolvedReference""}
}";

      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
    }
    public void Title()
    {
      string json = @"{
  ""description"":""Title"",
  ""title"":""testtitle""
}";

      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));

      Assert.AreEqual("Title", schema.Description);
      Assert.AreEqual("testtitle", schema.Title);
    }
    public void MultipleItems()
    {
      string json = @"{
  ""description"":""MultipleItems"",
  ""type"":""array"",
  ""items"": [{""type"":""string""},{""type"":""array""}]
}";

      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));

      Assert.AreEqual("MultipleItems", schema.Description);
      Assert.AreEqual(JsonSchemaType.String, schema.Items[0].Type);
      Assert.AreEqual(JsonSchemaType.Array, schema.Items[1].Type);
    }
    public void Pattern()
    {
      string json = @"{
  ""description"":""Pattern"",
  ""pattern"":""testpattern""
}";

      JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
      JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));

      Assert.AreEqual("Pattern", schema.Description);
      Assert.AreEqual("testpattern", schema.Pattern);
    }
示例#34
0
        /// <summary>
        /// Reads a <see cref="JsonSchema"/> from the specified <see cref="JsonReader"/>.
        /// </summary>
        /// <param name="reader">The <see cref="JsonReader"/> containing the JSON Schema to read.</param>
        /// <param name="resolver">The <see cref="JsonSchemaResolver"/> to use when resolving schema references.</param>
        /// <returns>The <see cref="JsonSchema"/> object representing the JSON Schema.</returns>
        public static JsonSchema Read(JsonReader reader, JsonSchemaResolver resolver)
        {
            ValidationUtils.ArgumentNotNull(reader, "reader");
              ValidationUtils.ArgumentNotNull(resolver, "resolver");

              JsonSchemaBuilder builder = new JsonSchemaBuilder(resolver);
              return builder.Parse(reader);
        }
        private JsonSchemaType?ProcessType()
        {
            JsonToken tokenType = this._reader.TokenType;

            if (tokenType == JsonToken.StartArray)
            {
                JsonSchemaType?result = new JsonSchemaType?(JsonSchemaType.None);
                while (this._reader.Read() && this._reader.TokenType != JsonToken.EndArray)
                {
                    if (this._reader.TokenType != JsonToken.String)
                    {
                        throw new Exception("Exception JSON schema type string token, got {0}.".FormatWith(CultureInfo.get_InvariantCulture(), new object[]
                        {
                            this._reader.TokenType
                        }));
                    }
                    result = ((!result.get_HasValue()) ? default(JsonSchemaType?) : new JsonSchemaType?(result.GetValueOrDefault() | JsonSchemaBuilder.MapType(this._reader.Value.ToString())));
                }
                return(result);
            }
            if (tokenType != JsonToken.String)
            {
                throw new Exception("Expected array or JSON schema type string token, got {0}.".FormatWith(CultureInfo.get_InvariantCulture(), new object[]
                {
                    this._reader.TokenType
                }));
            }
            return(new JsonSchemaType?(JsonSchemaBuilder.MapType(this._reader.Value.ToString())));
        }