本文整理汇总了C#中Newtonsoft.Json.Schema.JsonSchema类的典型用法代码示例。如果您正苦于以下问题:C# JsonSchema类的具体用法?C# JsonSchema怎么用?C# JsonSchema使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
JsonSchema类属于Newtonsoft.Json.Schema命名空间,在下文中一共展示了JsonSchema类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: IsValid
public static bool IsValid(this JToken source, JsonSchema schema, out IList<string> errorMessages)
{
IList<string> errors = (IList<string>) new List<string>();
Extensions.Validate(source, schema, (ValidationEventHandler) ((sender, args) => errors.Add(args.Message)));
errorMessages = errors;
return errorMessages.Count == 0;
}
示例2: Push
private void Push(JsonSchema value)
{
_currentSchema = value;
_stack.Add(value);
_resolver.LoadedSchemas.Add(value);
_documentSchemas.Add(value.Location, value);
}
示例3: AddProperty
public void AddProperty(IDictionary<string, JsonSchemaNode> target, string propertyName, JsonSchema schema)
{
JsonSchemaNode propertyNode;
target.TryGetValue(propertyName, out propertyNode);
target[propertyName] = AddSchema(propertyNode, schema);
}
示例4: Build
public JsonSchemaModel Build(JsonSchema schema)
{
this._nodes = new JsonSchemaNodeCollection();
this._node = this.AddSchema((JsonSchemaNode) null, schema);
this._nodeModels = new Dictionary<JsonSchemaNode, JsonSchemaModel>();
return this.BuildNodeModel(this._node);
}
示例5: GetCodeType
/// <summary>
/// Returns a code type references for the specified json schema.
/// Generates the appropriate references.
/// </summary>
internal static CodeTypeReference GetCodeType(JsonSchema propertySchema,
SchemaImplementationDetails details,
INestedClassProvider internalClassProvider)
{
propertySchema.ThrowIfNull("propertySchema");
internalClassProvider.ThrowIfNull("internalClassProvider");
if (propertySchema.Type.HasValue == false)
{
throw new NotSupportedException("propertySchema has no Type. " + propertySchema);
}
switch (propertySchema.Type.Value)
{
case JsonSchemaType.String:
return new CodeTypeReference(typeof(string));
case JsonSchemaType.Integer:
return new CodeTypeReference(typeof(long?));
case JsonSchemaType.Boolean:
return new CodeTypeReference(typeof(bool?));
case JsonSchemaType.Float:
return new CodeTypeReference(typeof(double?));
case JsonSchemaType.Array:
return GetArrayTypeReference(propertySchema, details, internalClassProvider);
case JsonSchemaType.Object:
return GetObjectTypeReference(propertySchema, details, internalClassProvider);
case JsonSchemaType.Any:
return new CodeTypeReference(typeof(string));
default:
logger.Warning(
"Found currently unsupported type {0} as part of {1}", propertySchema.Type.Value,
propertySchema);
return new CodeTypeReference(typeof(object));
}
}
示例6: AddSchema
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);
AddProperties(schema.Properties, currentNode.Properties);
AddProperties(schema.PatternProperties, currentNode.PatternProperties);
if (schema.Items != null)
{
for (int i = 0; i < schema.Items.Count; i++)
{
AddItem(currentNode, i, schema.Items[i]);
}
}
if (schema.AdditionalItems != null)
{
AddAdditionalItems(currentNode, schema.AdditionalItems);
}
if (schema.AdditionalProperties != null)
{
AddAdditionalProperties(currentNode, schema.AdditionalProperties);
}
if (schema.Extends != null)
{
foreach (JsonSchema jsonSchema in schema.Extends)
{
currentNode = AddSchema(currentNode, jsonSchema);
}
}
return currentNode;
}
示例7: AsMarkdown
/// <summary>
/// 转化为markdown格式。
/// </summary>
/// <param name="schema"></param>
/// <returns></returns>
public static string AsMarkdown(JsonSchema schema)
{
var sb = new StringBuilder();
// 生成表格
sb.AppendLine(AsMarkdown(schema, 1).Trim());
// 生成例子
sb.AppendLine();
sb.AppendLine("例子");
sb.AppendLine();
JToken demoToken = JsonDemoGenerator.Generate(schema);
string demo = JsonConvert.SerializeObject(demoToken, Formatting.Indented);
// 每一行前+4个空格,以适应markdown的code格式。
var sr = new StringReader(demo);
string line = sr.ReadLine();
while (!string.IsNullOrEmpty(line))
{
sb.AppendLine(" " + line);
line = sr.ReadLine();
}
return sb.ToString();
}
示例8: 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);
}
}
示例9: GenerateAllProperties
internal IList<CodeMemberProperty> GenerateAllProperties(string name,
JsonSchema schema,
IDictionary<JsonSchema, SchemaImplementationDetails>
implDetails,
INestedClassProvider internalClassProvider,
params string[] usedWordsInContext)
{
schema.ThrowIfNull("schema");
name.ThrowIfNullOrEmpty("name");
logger.Debug("Adding properties for {0}", name);
var fields = new List<CodeMemberProperty>();
if (schema.Properties.IsNullOrEmpty())
{
logger.Debug("No properties found for schema " + name);
return fields;
}
IEnumerable<string> allUsedWordsInContext = usedWordsInContext.Concat(schema.Properties.Keys);
int index = 0;
foreach (var propertyPair in schema.Properties)
{
SchemaImplementationDetails details = implDetails[propertyPair.Value];
CodeMemberProperty property = GenerateProperty(
propertyPair.Key, propertyPair.Value, details, index++, internalClassProvider,
allUsedWordsInContext.Except(new[] { propertyPair.Key }));
fields.Add(property);
}
return fields;
}
示例10: CreateInstanceFromSchema
protected string CreateInstanceFromSchema(JsonSchema schema, int indent)
{
if (schema.Type == JsonSchemaType.Array)
{
var items = schema.Items != null
? schema.Items.Select(i => CreateInstanceFromSchema(i, indent+1))
: Enumerable.Empty<string>();
return Indent(indent) + "[\n" + string.Join(",\n", items) + Indent(indent) + "]\n";
}
if (schema.Type == JsonSchemaType.Object || schema.Type == null)
{
var properties = schema.Properties != null
? schema.Properties.Select(i => CreateInstanceFromProperty(i, indent+1))
: Enumerable.Empty<string>();
return Indent(indent) + "{\n" + string.Join(",\n", properties) + "\n" + Indent(indent) + "}\n";
}
if (schema.Type == JsonSchemaType.String)
{
return Indent(indent) + "\"\"";
}
if (schema.Type == JsonSchemaType.Integer)
{
return Indent(indent) + "0";
}
if (schema.Type == JsonSchemaType.Boolean)
{
return Indent(indent) + "false";
}
return "";
}
示例11: AddSchema
public JsonSchemaNode AddSchema(JsonSchemaNode existingNode, JsonSchema schema)
{
string id;
if (existingNode != null)
{
if (existingNode.Schemas.Contains(schema))
return existingNode;
id = JsonSchemaNode.GetId(Enumerable.Union<JsonSchema>((IEnumerable<JsonSchema>) existingNode.Schemas, (IEnumerable<JsonSchema>) new JsonSchema[1]
{
schema
}));
}
else
id = JsonSchemaNode.GetId((IEnumerable<JsonSchema>) new JsonSchema[1]
{
schema
});
if (this._nodes.Contains(id))
return this._nodes[id];
JsonSchemaNode jsonSchemaNode = existingNode != null ? existingNode.Combine(schema) : new JsonSchemaNode(schema);
this._nodes.Add(jsonSchemaNode);
this.AddProperties(schema.Properties, (IDictionary<string, JsonSchemaNode>) jsonSchemaNode.Properties);
this.AddProperties(schema.PatternProperties, (IDictionary<string, JsonSchemaNode>) jsonSchemaNode.PatternProperties);
if (schema.Items != null)
{
for (int index = 0; index < schema.Items.Count; ++index)
this.AddItem(jsonSchemaNode, index, schema.Items[index]);
}
if (schema.AdditionalProperties != null)
this.AddAdditionalProperties(jsonSchemaNode, schema.AdditionalProperties);
if (schema.Extends != null)
jsonSchemaNode = this.AddSchema(jsonSchemaNode, schema.Extends);
return jsonSchemaNode;
}
示例12: GetArrayTypeReference
internal static CodeTypeReference GetArrayTypeReference(JsonSchema propertySchema,
SchemaImplementationDetails details,
INestedClassProvider internalClassProvider)
{
propertySchema.ThrowIfNull("propertySchema");
if (propertySchema.Type != JsonSchemaType.Array)
{
throw new ArgumentException("Must be of JsonSchemaType.Array", "propertySchema");
}
var arrayItems = propertySchema.Items;
if (arrayItems != null && arrayItems.Count == 1)
{
CodeTypeReference itemType = arrayItems[0].Id.IsNotNullOrEmpty()
? new CodeTypeReference(arrayItems[0].Id)
: GetCodeType(arrayItems[0], details, internalClassProvider);
logger.Debug("type for array {0}", itemType.BaseType);
return new CodeTypeReference(typeof(IList<>))
{
TypeArguments = { itemType }
};
}
logger.Warning("Found Array of unhandled type. {0}", propertySchema);
return new CodeTypeReference(typeof(System.Collections.IList));
}
示例13: GenerateAllFields
internal IList<CodeMemberField> GenerateAllFields(string name,
JsonSchema schema,
IDictionary<JsonSchema, SchemaImplementationDetails>
implDetails,
INestedClassProvider internalClassProvider)
{
schema.ThrowIfNull("schema");
name.ThrowIfNull("name");
implDetails.ThrowIfNull("details");
internalClassProvider.ThrowIfNull("internalClassProvider");
var fields = new List<CodeMemberField>();
if (schema.Properties.IsNullOrEmpty())
{
logger.Debug("No Properties found for " + name);
return fields;
}
int index = 0;
foreach (var propertyPair in schema.Properties)
{
SchemaImplementationDetails details = implDetails[propertyPair.Value];
fields.Add(
GenerateField(
propertyPair.Key, propertyPair.Value, details, index, internalClassProvider,
schema.Properties.Keys.Without(propertyPair.Key)));
index++;
}
return fields;
}
示例14: Pop
private JsonSchema Pop()
{
JsonSchema jsonSchema = this._currentSchema;
this._stack.RemoveAt(this._stack.Count - 1);
this._currentSchema = Enumerable.LastOrDefault<JsonSchema>((IEnumerable<JsonSchema>) this._stack);
return jsonSchema;
}
示例15: AddProperty
public void AddProperty(JsonSchemaNode parentNode, string propertyName, JsonSchema schema)
{
JsonSchemaNode propertyNode;
parentNode.Properties.TryGetValue(propertyName, out propertyNode);
parentNode.Properties[propertyName] = AddSchema(propertyNode, schema);
}