本文整理汇总了C#中Newtonsoft.Json.JsonWriter.WriteObject方法的典型用法代码示例。如果您正苦于以下问题:C# JsonWriter.WriteObject方法的具体用法?C# JsonWriter.WriteObject怎么用?C# JsonWriter.WriteObject使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Newtonsoft.Json.JsonWriter
的用法示例。
在下文中一共展示了JsonWriter.WriteObject方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddJsonProperties
private void AddJsonProperties(JsonWriter writer, PropertyInfo[] properties, bool inArray)
{
writer.WriteObject();
foreach (PropertyInfo property in properties)
{
if (IsValidProperty(property))
{
string jsonPropertyType = GetPropertyTypeName(property.PropertyType, TDataExchangeFormat.Json);
if (!inArray)
{
writer.WriteMember(property.Name);
writer.WriteObject();
}
writer.WriteMember("type");
writer.WriteValue(jsonPropertyType);
if (jsonPropertyType.Equals("object") || jsonPropertyType.Equals("array"))
{
bool inArray2 = false;
if (jsonPropertyType.Equals("object"))
{
writer.WriteMember("properties");
inArray2 = false;
}
else
{
writer.WriteMember("items");
inArray2 = true;
}
AddJsonProperties(writer, property.PropertyType.GetProperties(), inArray2);
}
if (!inArray)
writer.WriteEndObject();
}
}
writer.WriteEndObject();
}
示例2: GenerateJsonSchema
private string GenerateJsonSchema()
{
List<string> required = new List<string>();
MemoryStream stream = new MemoryStream();
JsonWriter writer = new JsonWriter(stream);
writer.WriteObject();
writer.WriteMember("$schema");
writer.WriteValue("http://json-schema.org/draft-04/schema");
writer.WriteMember("title");
writer.WriteValue(Object.Name);
writer.WriteMember("type");
writer.WriteValue("object");
writer.WriteMember("properties");
AddJsonProperties(writer, Object.GetProperties(), false);
if (required.Count > 0)
{
writer.WriteMember("required");
writer.WriteArray();
foreach (string requirement in required)
{
writer.WriteValue(requirement);
}
writer.WriteEndArray();
}
writer.WriteEndObject();
writer.Flush();
stream.Position = 0;
string unformattedJsonBody = new StreamReader(stream).ReadToEnd();
object parsedJson = JsonConvert.DeserializeObject(unformattedJsonBody);
return JsonConvert.SerializeObject(parsedJson, Newtonsoft.Json.Formatting.Indented);
}