本文整理汇总了C#中Newtonsoft.Json.Schema.JsonSchemaModel类的典型用法代码示例。如果您正苦于以下问题:C# JsonSchemaModel类的具体用法?C# JsonSchemaModel怎么用?C# JsonSchemaModel使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
JsonSchemaModel类属于Newtonsoft.Json.Schema命名空间,在下文中一共展示了JsonSchemaModel类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Create
public static JsonSchemaModel Create(IList<JsonSchema> schemata)
{
JsonSchemaModel model = new JsonSchemaModel();
foreach (JsonSchema schema in (IEnumerable<JsonSchema>) schemata)
JsonSchemaModel.Combine(model, schema);
return model;
}
示例2: GetRequiredProperties
private IEnumerable<string> GetRequiredProperties(JsonSchemaModel schema)
{
if (schema == null || schema.Properties == null)
return Enumerable.Empty<string>();
return schema.Properties.Where(p => p.Value.Required).Select(p => p.Key);
}
示例3: Combine
private static void Combine(JsonSchemaModel model, JsonSchema schema)
{
model.Optional = model.Optional && (schema.Optional ?? false);
model.Type = model.Type & (schema.Type ?? JsonSchemaType.Any);
model.MinimumLength = MathUtils.Max(model.MinimumLength, schema.MinimumLength);
model.MaximumLength = MathUtils.Min(model.MaximumLength, schema.MaximumLength);
model.MaximumDecimals = MathUtils.Min(model.MaximumDecimals, schema.MaximumDecimals);
model.Minimum = MathUtils.Max(model.Minimum, schema.Minimum);
model.Maximum = MathUtils.Max(model.Maximum, schema.Maximum);
model.MinimumItems = MathUtils.Max(model.MinimumItems, schema.MinimumItems);
model.MaximumItems = MathUtils.Min(model.MaximumItems, schema.MaximumItems);
model.AllowAdditionalProperties = model.AllowAdditionalProperties && schema.AllowAdditionalProperties;
if (schema.Enum != null)
{
if (model.Enum == null)
model.Enum = new List<JToken>();
model.Enum.AddRangeDistinct(schema.Enum, new JTokenEqualityComparer());
}
model.Disallow = model.Disallow | (schema.Disallow ?? JsonSchemaType.None);
if (schema.Pattern != null)
{
if (model.Patterns == null)
model.Patterns = new List<string>();
model.Patterns.AddDistinct(schema.Pattern);
}
}
示例4: SchemaScope
public SchemaScope(JTokenType tokenType, JsonSchemaModel schema)
{
_tokenType = tokenType;
_schema = schema;
if (_schema != null && _schema.Properties != null)
_requiredProperties = GetRequiredProperties(_schema).Distinct().ToDictionary(p => p, p => false);
else
_requiredProperties = new Dictionary<string, bool>();
}
示例5: Create
public static JsonSchemaModel Create(IList<JsonSchema> schemata)
{
JsonSchemaModel model = new JsonSchemaModel();
foreach (JsonSchema schema in schemata)
{
Combine(model, schema);
}
return model;
}
示例6: Combine
private static void Combine(JsonSchemaModel model, JsonSchema schema)
{
// Version 3 of the Draft JSON Schema has the default value of Not Required
/* STH:
model.Required = model.Required || (schema.Required ?? false);
*/
if (schema.Required != null)
{
var required = schema.Required.Select(t => (string)t).Distinct().ToList();
if (model.Required != null)
model.Required.AddRangeDistinct(required, null);
else
model.Required = required;
}
model.Type = model.Type & (schema.Type ?? JsonSchemaType.Any);
model.MinimumLength = MathUtils.Max(model.MinimumLength, schema.MinimumLength);
model.MaximumLength = MathUtils.Min(model.MaximumLength, schema.MaximumLength);
// not sure what is the best way to combine divisibleBy
model.DivisibleBy = MathUtils.Max(model.DivisibleBy, schema.DivisibleBy);
model.Minimum = MathUtils.Max(model.Minimum, schema.Minimum);
model.Maximum = MathUtils.Max(model.Maximum, schema.Maximum);
model.ExclusiveMinimum = model.ExclusiveMinimum || (schema.ExclusiveMinimum ?? false);
model.ExclusiveMaximum = model.ExclusiveMaximum || (schema.ExclusiveMaximum ?? false);
model.MinimumItems = MathUtils.Max(model.MinimumItems, schema.MinimumItems);
model.MaximumItems = MathUtils.Min(model.MaximumItems, schema.MaximumItems);
model.PositionalItemsValidation = model.PositionalItemsValidation || schema.PositionalItemsValidation;
model.AllowAdditionalProperties = model.AllowAdditionalProperties && schema.AllowAdditionalProperties;
model.AllowAdditionalItems = model.AllowAdditionalItems && schema.AllowAdditionalItems;
model.UniqueItems = model.UniqueItems || schema.UniqueItems;
if (schema.Enum != null)
{
if (model.Enum == null)
model.Enum = new List<JToken>();
model.Enum.AddRangeDistinct(schema.Enum, JToken.EqualityComparer);
}
model.Disallow = model.Disallow | (schema.Disallow ?? JsonSchemaType.None);
if (schema.Pattern != null)
{
if (model.Patterns == null)
model.Patterns = new List<string>();
model.Patterns.AddDistinct(schema.Pattern);
}
}
示例7: Combine
private static void Combine(JsonSchemaModel model, JsonSchema schema)
{
// Version 3 of the Draft JSON Schema has the default value of Not Required
model.Required = model.Required || (schema.Required ?? false);
model.Type = model.Type & (schema.Type ?? JsonSchemaType.Any);
model.MinimumLength = Newtonsoft.Json.Utilities.MathUtils.Max(model.MinimumLength, schema.MinimumLength);
model.MaximumLength = Newtonsoft.Json.Utilities.MathUtils.Min(model.MaximumLength, schema.MaximumLength);
// not sure what is the best way to combine divisibleBy
model.DivisibleBy = Newtonsoft.Json.Utilities.MathUtils.Max(model.DivisibleBy, schema.DivisibleBy);
model.Minimum = Newtonsoft.Json.Utilities.MathUtils.Max(model.Minimum, schema.Minimum);
model.Maximum = Newtonsoft.Json.Utilities.MathUtils.Max(model.Maximum, schema.Maximum);
model.ExclusiveMinimum = model.ExclusiveMinimum || (schema.ExclusiveMinimum ?? false);
model.ExclusiveMaximum = model.ExclusiveMaximum || (schema.ExclusiveMaximum ?? false);
model.MinimumItems = Newtonsoft.Json.Utilities.MathUtils.Max(model.MinimumItems, schema.MinimumItems);
model.MaximumItems = Newtonsoft.Json.Utilities.MathUtils.Min(model.MaximumItems, schema.MaximumItems);
model.AllowAdditionalProperties = model.AllowAdditionalProperties && schema.AllowAdditionalProperties;
if (schema.Enum != null)
{
if (model.Enum == null)
model.Enum = new List<JToken>();
model.Enum.AddRangeDistinct(schema.Enum, new JTokenEqualityComparer());
}
model.Disallow = model.Disallow | (schema.Disallow ?? JsonSchemaType.None);
if (schema.Pattern != null)
{
if (model.Patterns == null)
model.Patterns = new List<string>();
model.Patterns.AddDistinct(schema.Pattern);
}
}
示例8: ValidateBoolean
private void ValidateBoolean(JsonSchemaModel schema)
{
if (schema == null)
return;
if (!TestType(schema, JsonSchemaType.Boolean))
return;
ValidateNotDisallowed(schema);
}
示例9: ValidateEndArray
private void ValidateEndArray(JsonSchemaModel schema)
{
if (schema == null)
return;
int arrayItemCount = _currentScope.ArrayItemCount;
if (schema.MaximumItems != null && arrayItemCount > schema.MaximumItems)
RaiseError("Array item count {0} exceeds maximum count of {1}.".FormatWith(CultureInfo.InvariantCulture, arrayItemCount, schema.MaximumItems), schema);
if (schema.MinimumItems != null && arrayItemCount < schema.MinimumItems)
RaiseError("Array item count {0} is less than minimum count of {1}.".FormatWith(CultureInfo.InvariantCulture, arrayItemCount, schema.MinimumItems), schema);
}
示例10: ValidateEndObject
private void ValidateEndObject(JsonSchemaModel schema)
{
if (schema == null)
return;
Dictionary<string, bool> requiredProperties = _currentScope.RequiredProperties;
if (requiredProperties != null)
{
List<string> unmatchedRequiredProperties =
requiredProperties.Where(kv => !kv.Value).Select(kv => kv.Key).ToList();
if (unmatchedRequiredProperties.Count > 0)
RaiseError("Required properties are missing from object: {0}.".FormatWith(CultureInfo.InvariantCulture, string.Join(", ", unmatchedRequiredProperties.ToArray())), schema);
}
}
示例11: ValidateCurrentToken
private void ValidateCurrentToken()
{
// first time validate has been called. build model
if (_model == null)
{
JsonSchemaModelBuilder builder = new JsonSchemaModelBuilder();
_model = builder.Build(_schema);
if (!JsonTokenUtils.IsStartToken(_reader.TokenType))
Push(new SchemaScope(JTokenType.None, CurrentMemberSchemas));
}
switch (_reader.TokenType)
{
case JsonToken.StartObject:
ProcessValue();
IList<JsonSchemaModel> objectSchemas = CurrentMemberSchemas.Where(ValidateObject).ToList();
Push(new SchemaScope(JTokenType.Object, objectSchemas));
WriteToken(CurrentSchemas);
break;
case JsonToken.StartArray:
ProcessValue();
IList<JsonSchemaModel> arraySchemas = CurrentMemberSchemas.Where(ValidateArray).ToList();
Push(new SchemaScope(JTokenType.Array, arraySchemas));
WriteToken(CurrentSchemas);
break;
case JsonToken.StartConstructor:
ProcessValue();
Push(new SchemaScope(JTokenType.Constructor, null));
WriteToken(CurrentSchemas);
break;
case JsonToken.PropertyName:
WriteToken(CurrentSchemas);
foreach (JsonSchemaModel schema in CurrentSchemas)
{
ValidatePropertyName(schema);
}
break;
case JsonToken.Raw:
ProcessValue();
break;
case JsonToken.Integer:
ProcessValue();
WriteToken(CurrentMemberSchemas);
foreach (JsonSchemaModel schema in CurrentMemberSchemas)
{
ValidateInteger(schema);
}
break;
case JsonToken.Float:
ProcessValue();
WriteToken(CurrentMemberSchemas);
foreach (JsonSchemaModel schema in CurrentMemberSchemas)
{
ValidateFloat(schema);
}
break;
case JsonToken.String:
ProcessValue();
WriteToken(CurrentMemberSchemas);
foreach (JsonSchemaModel schema in CurrentMemberSchemas)
{
ValidateString(schema);
}
break;
case JsonToken.Boolean:
ProcessValue();
WriteToken(CurrentMemberSchemas);
foreach (JsonSchemaModel schema in CurrentMemberSchemas)
{
ValidateBoolean(schema);
}
break;
case JsonToken.Null:
ProcessValue();
WriteToken(CurrentMemberSchemas);
foreach (JsonSchemaModel schema in CurrentMemberSchemas)
{
ValidateNull(schema);
}
break;
case JsonToken.EndObject:
WriteToken(CurrentSchemas);
foreach (JsonSchemaModel schema in CurrentSchemas)
{
ValidateEndObject(schema);
}
Pop();
break;
case JsonToken.EndArray:
WriteToken(CurrentSchemas);
foreach (JsonSchemaModel schema in CurrentSchemas)
{
ValidateEndArray(schema);
}
Pop();
break;
case JsonToken.EndConstructor:
WriteToken(CurrentSchemas);
Pop();
//.........这里部分代码省略.........
示例12: ValidateFloat
private void ValidateFloat(JsonSchemaModel schema)
{
if (schema == null)
return;
if (!TestType(schema, JsonSchemaType.Float))
return;
ValidateNotDisallowed(schema);
double value = Convert.ToDouble(_reader.Value, CultureInfo.InvariantCulture);
if (schema.Maximum != null)
{
if (value > schema.Maximum)
RaiseError("Float {0} exceeds maximum value of {1}.".FormatWith(CultureInfo.InvariantCulture, JsonConvert.ToString(value), schema.Maximum), schema);
if (schema.ExclusiveMaximum && value == schema.Maximum)
RaiseError("Float {0} equals maximum value of {1} and exclusive maximum is true.".FormatWith(CultureInfo.InvariantCulture, JsonConvert.ToString(value), schema.Maximum), schema);
}
if (schema.Minimum != null)
{
if (value < schema.Minimum)
RaiseError("Float {0} is less than minimum value of {1}.".FormatWith(CultureInfo.InvariantCulture, JsonConvert.ToString(value), schema.Minimum), schema);
if (schema.ExclusiveMinimum && value == schema.Minimum)
RaiseError("Float {0} equals minimum value of {1} and exclusive minimum is true.".FormatWith(CultureInfo.InvariantCulture, JsonConvert.ToString(value), schema.Minimum), schema);
}
if (schema.DivisibleBy != null)
{
double remainder = FloatingPointRemainder(value, schema.DivisibleBy.Value);
if (!IsZero(remainder))
RaiseError("Float {0} is not evenly divisible by {1}.".FormatWith(CultureInfo.InvariantCulture, JsonConvert.ToString(value), schema.DivisibleBy), schema);
}
}
示例13: IsPropertyDefinied
private bool IsPropertyDefinied(JsonSchemaModel schema, string propertyName)
{
if (schema.Properties != null && schema.Properties.ContainsKey(propertyName))
return true;
if (schema.PatternProperties != null)
{
foreach (string pattern in schema.PatternProperties.Keys)
{
if (Regex.IsMatch(propertyName, pattern))
return true;
}
}
return false;
}
示例14: ValidatePropertyName
private void ValidatePropertyName(JsonSchemaModel schema)
{
if (schema == null)
return;
string propertyName = Convert.ToString(_reader.Value, CultureInfo.InvariantCulture);
if (_currentScope.RequiredProperties.ContainsKey(propertyName))
_currentScope.RequiredProperties[propertyName] = true;
if (schema.Properties != null && !schema.Properties.ContainsKey(propertyName))
{
IList<string> definedProperties = schema.Properties.Select(p => p.Key).ToList();
if (!schema.AllowAdditionalProperties && !definedProperties.Contains(propertyName))
{
RaiseError("Property '{0}' has not been defined and the schema does not allow additional properties.".FormatWith(CultureInfo.InvariantCulture, propertyName), schema);
}
}
_currentScope.CurrentPropertyName = propertyName;
}
示例15: ValidateObject
private bool ValidateObject(JsonSchemaModel schema)
{
if (schema == null)
return true;
return (TestType(schema, JsonSchemaType.Object));
}