本文整理汇总了C#中JsonWriter.WriteComment方法的典型用法代码示例。如果您正苦于以下问题:C# JsonWriter.WriteComment方法的具体用法?C# JsonWriter.WriteComment怎么用?C# JsonWriter.WriteComment使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JsonWriter
的用法示例。
在下文中一共展示了JsonWriter.WriteComment方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SerializeNode
private void SerializeNode(JsonWriter writer, IXmlNode node, XmlNamespaceManager manager, bool writePropertyName)
{
switch (node.NodeType)
{
case XmlNodeType.Document:
case XmlNodeType.DocumentFragment:
SerializeGroupedNodes(writer, node, manager, writePropertyName);
break;
case XmlNodeType.Element:
if (IsArray(node) && node.ChildNodes.All(n => n.LocalName == node.LocalName) && node.ChildNodes.Count > 0)
{
SerializeGroupedNodes(writer, node, manager, false);
}
else
{
foreach (IXmlNode attribute in node.Attributes)
{
if (attribute.NamespaceUri == "http://www.w3.org/2000/xmlns/")
{
string prefix = (attribute.LocalName != "xmlns")
? attribute.LocalName
: string.Empty;
manager.AddNamespace(prefix, attribute.Value);
}
}
if (writePropertyName)
writer.WritePropertyName(GetPropertyName(node, manager));
if (!ValueAttributes(node.Attributes).Any() && node.ChildNodes.Count == 1
&& node.ChildNodes[0].NodeType == XmlNodeType.Text)
{
// write elements with a single text child as a name value pair
writer.WriteValue(node.ChildNodes[0].Value);
}
else if (node.ChildNodes.Count == 0 && CollectionUtils.IsNullOrEmpty(node.Attributes))
{
// empty element
writer.WriteNull();
}
else
{
writer.WriteStartObject();
for (int i = 0; i < node.Attributes.Count; i++)
{
SerializeNode(writer, node.Attributes[i], manager, true);
}
SerializeGroupedNodes(writer, node, manager, true);
writer.WriteEndObject();
}
}
break;
case XmlNodeType.Comment:
if (writePropertyName)
writer.WriteComment(node.Value);
break;
case XmlNodeType.Attribute:
case XmlNodeType.Text:
case XmlNodeType.CDATA:
case XmlNodeType.ProcessingInstruction:
case XmlNodeType.Whitespace:
case XmlNodeType.SignificantWhitespace:
if (node.NamespaceUri == "http://www.w3.org/2000/xmlns/" && node.Value == JsonNamespaceUri)
return;
if (node.NamespaceUri == JsonNamespaceUri)
{
if (node.LocalName == "Array")
return;
}
if (writePropertyName)
writer.WritePropertyName(GetPropertyName(node, manager));
writer.WriteValue(node.Value);
break;
case XmlNodeType.XmlDeclaration:
IXmlDeclaration declaration = (IXmlDeclaration)node;
writer.WritePropertyName(GetPropertyName(node, manager));
writer.WriteStartObject();
if (!string.IsNullOrEmpty(declaration.Version))
{
writer.WritePropertyName("@version");
writer.WriteValue(declaration.Version);
}
if (!string.IsNullOrEmpty(declaration.Encoding))
{
writer.WritePropertyName("@encoding");
writer.WriteValue(declaration.Encoding);
}
if (!string.IsNullOrEmpty(declaration.Standalone))
{
writer.WritePropertyName("@standalone");
writer.WriteValue(declaration.Standalone);
}
//.........这里部分代码省略.........
示例2: WriteTo
/// <summary>
/// Writes this token to a <see cref="JsonWriter"/>.
/// </summary>
/// <param name="writer">A <see cref="JsonWriter"/> into which this method will write.</param>
/// <param name="converters">A collection of <see cref="JsonConverter"/> which will be used when writing the token.</param>
public override void WriteTo(JsonWriter writer, params JsonConverter[] converters)
{
switch (_valueType)
{
case JsonTokenType.Comment:
writer.WriteComment(_value.ToString());
break;
case JsonTokenType.Integer:
WriteConvertableValue(writer, converters, v => writer.WriteValue(Convert.ToInt64(v, CultureInfo.InvariantCulture)), _value);
break;
case JsonTokenType.Float:
WriteConvertableValue(writer, converters, v => writer.WriteValue(Convert.ToDouble(v, CultureInfo.InvariantCulture)), _value);
break;
case JsonTokenType.String:
WriteConvertableValue(writer, converters, v => writer.WriteValue(v.ToString()), _value);
break;
case JsonTokenType.Boolean:
WriteConvertableValue(writer, converters, v => writer.WriteValue(Convert.ToBoolean(v, CultureInfo.InvariantCulture)), _value);
break;
case JsonTokenType.Date:
WriteConvertableValue(writer, converters, v =>
{
if (v is DateTimeOffset)
writer.WriteValue((DateTimeOffset)v);
else
writer.WriteValue(Convert.ToDateTime(v, CultureInfo.InvariantCulture));
}, _value);
break;
case JsonTokenType.Raw:
writer.WriteRawValue(_value.ToString());
break;
case JsonTokenType.Null:
writer.WriteNull();
break;
case JsonTokenType.Undefined:
writer.WriteUndefined();
break;
default:
throw MiscellaneousUtils.CreateArgumentOutOfRangeException("TokenType", _valueType, "Unexpected token type.");
}
}
示例3: SerializeNode
private void SerializeNode(JsonWriter writer, XmlNode node, bool writePropertyName)
{
switch (node.NodeType)
{
case XmlNodeType.Document:
case XmlNodeType.DocumentFragment:
SerializeGroupedNodes(writer, node);
break;
case XmlNodeType.Element:
if (writePropertyName)
writer.WritePropertyName(node.Name);
if (ValueAttributes(node.Attributes).Count() == 0 && node.ChildNodes.Count == 1
&& node.ChildNodes[0].NodeType == XmlNodeType.Text)
{
// write elements with a single text child as a name value pair
writer.WriteValue(node.ChildNodes[0].Value);
}
else if (node.ChildNodes.Count == 0 && CollectionUtils.IsNullOrEmpty(node.Attributes))
{
// empty element
writer.WriteNull();
}
else if (node.ChildNodes.OfType<XmlElement>().Where(x => x.Name.StartsWith("-")).Count() > 1)
{
XmlElement constructorValueElement = node.ChildNodes.OfType<XmlElement>().Where(x => x.Name.StartsWith("-")).First();
string constructorName = constructorValueElement.Name.Substring(1);
writer.WriteStartConstructor(constructorName);
for (int i = 0; i < node.ChildNodes.Count; i++)
{
SerializeNode(writer, node.ChildNodes[i], false);
}
writer.WriteEndConstructor();
}
else
{
writer.WriteStartObject();
for (int i = 0; i < node.Attributes.Count; i++)
{
SerializeNode(writer, node.Attributes[i], true);
}
SerializeGroupedNodes(writer, node);
writer.WriteEndObject();
}
break;
case XmlNodeType.Comment:
if (writePropertyName)
writer.WriteComment(node.Value);
break;
case XmlNodeType.Attribute:
case XmlNodeType.Text:
case XmlNodeType.CDATA:
case XmlNodeType.ProcessingInstruction:
case XmlNodeType.Whitespace:
case XmlNodeType.SignificantWhitespace:
if (node.Prefix == "xmlns" && node.Value == JsonNamespaceUri)
break;
else if (node.NamespaceURI == JsonNamespaceUri)
break;
if (writePropertyName)
writer.WritePropertyName(GetPropertyName(node));
writer.WriteValue(node.Value);
break;
case XmlNodeType.XmlDeclaration:
XmlDeclaration declaration = (XmlDeclaration)node;
writer.WritePropertyName(GetPropertyName(node));
writer.WriteStartObject();
if (!string.IsNullOrEmpty(declaration.Version))
{
writer.WritePropertyName("@version");
writer.WriteValue(declaration.Version);
}
if (!string.IsNullOrEmpty(declaration.Encoding))
{
writer.WritePropertyName("@encoding");
writer.WriteValue(declaration.Encoding);
}
if (!string.IsNullOrEmpty(declaration.Standalone))
{
writer.WritePropertyName("@standalone");
writer.WriteValue(declaration.Standalone);
}
writer.WriteEndObject();
break;
default:
throw new JsonSerializationException("Unexpected XmlNodeType when serializing nodes: " + node.NodeType);
}
}
示例4: Indenting
public void Indenting()
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
using (JsonWriter jsonWriter = new JsonWriter(sw))
{
jsonWriter.Formatting = Formatting.Indented;
jsonWriter.WriteStartObject();
jsonWriter.WritePropertyName("CPU");
jsonWriter.WriteValue("Intel");
jsonWriter.WritePropertyName("PSU");
jsonWriter.WriteValue("500W");
jsonWriter.WritePropertyName("Drives");
jsonWriter.WriteStartArray();
jsonWriter.WriteValue("DVD read/writer");
jsonWriter.WriteComment("(broken)");
jsonWriter.WriteValue("500 gigabyte hard drive");
jsonWriter.WriteValue("200 gigabype hard drive");
jsonWriter.WriteEnd();
jsonWriter.WriteEndObject();
}
string expected = @"{
""CPU"": ""Intel"",
""PSU"": ""500W"",
""Drives"": [
""DVD read/writer""
/*(broken)*/,
""500 gigabyte hard drive"",
""200 gigabype hard drive""
]
}";
string result = sb.ToString();
Console.WriteLine("Indenting");
Console.WriteLine(result);
Assert.AreEqual(expected, result);
}
示例5: WriteTo
public override void WriteTo(JsonWriter writer)
{
switch (_valueType)
{
case JsonTokenType.Comment:
writer.WriteComment(_value.ToString());
break;
case JsonTokenType.Integer:
writer.WriteValue(Convert.ToInt64(_value));
break;
case JsonTokenType.Float:
writer.WriteValue(Convert.ToDouble(_value));
break;
case JsonTokenType.String:
writer.WriteValue(_value.ToString());
break;
case JsonTokenType.Boolean:
writer.WriteValue(Convert.ToBoolean(_value));
break;
case JsonTokenType.Null:
writer.WriteNull();
break;
case JsonTokenType.Undefined:
writer.WriteUndefined();
break;
case JsonTokenType.Date:
writer.WriteValue(Convert.ToDateTime(_value));
break;
default:
throw new ArgumentOutOfRangeException("TokenType", _valueType, "Unexpected token type.");
}
}
示例6: SerializeNode
// Token: 0x060006E1 RID: 1761
// RVA: 0x00037C10 File Offset: 0x00035E10
private void SerializeNode(JsonWriter writer, IXmlNode node, XmlNamespaceManager manager, bool writePropertyName)
{
switch (node.NodeType)
{
case XmlNodeType.Element:
if (this.IsArray(node))
{
if (Enumerable.All<IXmlNode>(node.ChildNodes, (IXmlNode n) => n.LocalName == node.LocalName) && node.ChildNodes.Count > 0)
{
this.SerializeGroupedNodes(writer, node, manager, false);
return;
}
}
manager.PushScope();
foreach (IXmlNode current in node.Attributes)
{
if (current.NamespaceUri == "http://www.w3.org/2000/xmlns/")
{
string prefix = (current.LocalName != "xmlns") ? current.LocalName : string.Empty;
string value = current.Value;
manager.AddNamespace(prefix, value);
}
}
if (writePropertyName)
{
writer.WritePropertyName(this.GetPropertyName(node, manager));
}
if (!Enumerable.Any<IXmlNode>(this.ValueAttributes(node.Attributes)) && node.ChildNodes.Count == 1 && node.ChildNodes[0].NodeType == XmlNodeType.Text)
{
writer.WriteValue(node.ChildNodes[0].Value);
}
else if (node.ChildNodes.Count == 0 && CollectionUtils.IsNullOrEmpty<IXmlNode>(node.Attributes))
{
IXmlElement xmlElement = (IXmlElement)node;
if (xmlElement.IsEmpty)
{
writer.WriteNull();
}
else
{
writer.WriteValue(string.Empty);
}
}
else
{
writer.WriteStartObject();
for (int i = 0; i < node.Attributes.Count; i++)
{
this.SerializeNode(writer, node.Attributes[i], manager, true);
}
this.SerializeGroupedNodes(writer, node, manager, true);
writer.WriteEndObject();
}
manager.PopScope();
return;
case XmlNodeType.Attribute:
case XmlNodeType.Text:
case XmlNodeType.CDATA:
case XmlNodeType.ProcessingInstruction:
case XmlNodeType.Whitespace:
case XmlNodeType.SignificantWhitespace:
if (node.NamespaceUri == "http://www.w3.org/2000/xmlns/" && node.Value == "http://james.newtonking.com/projects/json")
{
return;
}
if (node.NamespaceUri == "http://james.newtonking.com/projects/json" && node.LocalName == "Array")
{
return;
}
if (writePropertyName)
{
writer.WritePropertyName(this.GetPropertyName(node, manager));
}
writer.WriteValue(node.Value);
return;
case XmlNodeType.Comment:
if (writePropertyName)
{
writer.WriteComment(node.Value);
return;
}
return;
case XmlNodeType.Document:
case XmlNodeType.DocumentFragment:
this.SerializeGroupedNodes(writer, node, manager, writePropertyName);
return;
case XmlNodeType.DocumentType:
{
IXmlDocumentType xmlDocumentType = (IXmlDocumentType)node;
writer.WritePropertyName(this.GetPropertyName(node, manager));
writer.WriteStartObject();
if (!string.IsNullOrEmpty(xmlDocumentType.Name))
{
writer.WritePropertyName("@name");
writer.WriteValue(xmlDocumentType.Name);
}
if (!string.IsNullOrEmpty(xmlDocumentType.Public))
{
//.........这里部分代码省略.........
示例7: SerializeNode
private void SerializeNode(JsonWriter writer, IXmlNode node, XmlNamespaceManager manager, bool writePropertyName)
{
switch (node.NodeType)
{
case XmlNodeType.Document:
case XmlNodeType.DocumentFragment:
SerializeGroupedNodes(writer, node, manager, writePropertyName);
break;
case XmlNodeType.Element:
if (IsArray(node) && node.ChildNodes.All(n => n.LocalName == node.LocalName) && node.ChildNodes.Count > 0)
SerializeGroupedNodes(writer, node, manager, false);
else
{
manager.PushScope();
foreach (var attribute in node.Attributes)
{
if (attribute.NamespaceUri != "http://www.w3.org/2000/xmlns/") continue;
var namespacePrefix = (attribute.LocalName != "xmlns") ? attribute.LocalName : string.Empty;
var namespaceUri = attribute.Value;
manager.AddNamespace(namespacePrefix, namespaceUri);
}
if (writePropertyName)
writer.WritePropertyName(GetPropertyName(node, manager));
if (!ValueAttributes(node.Attributes).Any() && node.ChildNodes.Count == 1 && node.ChildNodes[0].NodeType == XmlNodeType.Text)
// write elements with a single text child as a name value pair
writer.WriteValue(node.ChildNodes[0].Value);
else if (node.ChildNodes.Count == 0 && CollectionUtils.IsNullOrEmpty(node.Attributes))
{
var element = (IXmlElement) node;
// empty element
if (element.IsEmpty)
writer.WriteNull();
else
writer.WriteValue(string.Empty);
}
else
{
writer.WriteStartObject();
for (var i = 0; i < node.Attributes.Count; i++)
SerializeNode(writer, node.Attributes[i], manager, true);
SerializeGroupedNodes(writer, node, manager, true);
writer.WriteEndObject();
}
manager.PopScope();
}
break;
case XmlNodeType.Comment:
if (writePropertyName)
writer.WriteComment(node.Value);
break;
case XmlNodeType.Attribute:
case XmlNodeType.Text:
case XmlNodeType.CDATA:
case XmlNodeType.ProcessingInstruction:
case XmlNodeType.Whitespace:
case XmlNodeType.SignificantWhitespace:
if (node.NamespaceUri == "http://www.w3.org/2000/xmlns/" && node.Value == JsonNamespaceUri)
return;
if (node.NamespaceUri == JsonNamespaceUri)
if (node.LocalName == "Array")
return;
if (writePropertyName)
writer.WritePropertyName(GetPropertyName(node, manager));
writer.WriteValue(node.Value);
break;
case XmlNodeType.XmlDeclaration:
var declaration = (IXmlDeclaration) node;
writer.WritePropertyName(GetPropertyName(node, manager));
writer.WriteStartObject();
if (!string.IsNullOrEmpty(declaration.Version))
{
writer.WritePropertyName("@version");
writer.WriteValue(declaration.Version);
}
if (!string.IsNullOrEmpty(declaration.Encoding))
{
writer.WritePropertyName("@encoding");
writer.WriteValue(declaration.Encoding);
}
if (!string.IsNullOrEmpty(declaration.Standalone))
{
writer.WritePropertyName("@standalone");
writer.WriteValue(declaration.Standalone);
}
writer.WriteEndObject();
break;
case XmlNodeType.DocumentType:
//.........这里部分代码省略.........
示例8: SerializeNode
private void SerializeNode(JsonWriter writer, XmlNode node, bool writePropertyName)
{
switch (node.NodeType)
{
case XmlNodeType.Document:
case XmlNodeType.DocumentFragment:
SerializeGroupedNodes(writer, node);
break;
case XmlNodeType.Element:
if (writePropertyName)
writer.WritePropertyName(node.Name);
if (CollectionUtils.IsNullOrEmpty(node.Attributes) && node.ChildNodes.Count == 1
&& node.ChildNodes[0].NodeType == XmlNodeType.Text)
{
// write elements with a single text child as a name value pair
writer.WriteValue(node.ChildNodes[0].Value);
}
else if (node.ChildNodes.Count == 0 && CollectionUtils.IsNullOrEmpty(node.Attributes))
{
// empty element
writer.WriteNull();
}
else
{
writer.WriteStartObject();
for (int i = 0; i < node.Attributes.Count; i++)
{
SerializeNode(writer, node.Attributes[i], true);
}
SerializeGroupedNodes(writer, node);
writer.WriteEndObject();
}
break;
case XmlNodeType.Comment:
if (writePropertyName)
writer.WriteComment(node.Value);
break;
case XmlNodeType.Attribute:
case XmlNodeType.Text:
case XmlNodeType.CDATA:
case XmlNodeType.ProcessingInstruction:
case XmlNodeType.Whitespace:
case XmlNodeType.SignificantWhitespace:
if (writePropertyName)
writer.WritePropertyName(GetPropertyName(node));
writer.WriteValue(node.Value);
break;
case XmlNodeType.XmlDeclaration:
XmlDeclaration declaration = (XmlDeclaration)node;
writer.WritePropertyName(GetPropertyName(node));
writer.WriteStartObject();
if (!string.IsNullOrEmpty(declaration.Version))
{
writer.WritePropertyName("@version");
writer.WriteValue(declaration.Version);
}
if (!string.IsNullOrEmpty(declaration.Encoding))
{
writer.WritePropertyName("@encoding");
writer.WriteValue(declaration.Encoding);
}
if (!string.IsNullOrEmpty(declaration.Standalone))
{
writer.WritePropertyName("@standalone");
writer.WriteValue(declaration.Standalone);
}
writer.WriteEndObject();
break;
default:
throw new Exception("Unexpected XmlNodeType when serializing nodes: " + node.NodeType);
}
}