本文整理汇总了C#中Newtonsoft.Json.JsonTextWriter.WriteStartConstructor方法的典型用法代码示例。如果您正苦于以下问题:C# JsonTextWriter.WriteStartConstructor方法的具体用法?C# JsonTextWriter.WriteStartConstructor怎么用?C# JsonTextWriter.WriteStartConstructor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Newtonsoft.Json.JsonTextWriter
的用法示例。
在下文中一共展示了JsonTextWriter.WriteStartConstructor方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Path
public void Path()
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
string text = "Hello world.";
byte[] data = Encoding.UTF8.GetBytes(text);
using (JsonTextWriter writer = new JsonTextWriter(sw))
{
writer.Formatting = Formatting.Indented;
writer.WriteStartArray();
Assert.AreEqual("", writer.Path);
writer.WriteStartObject();
Assert.AreEqual("[0]", writer.Path);
writer.WritePropertyName("Property1");
Assert.AreEqual("[0].Property1", writer.Path);
writer.WriteStartArray();
Assert.AreEqual("[0].Property1", writer.Path);
writer.WriteValue(1);
Assert.AreEqual("[0].Property1[0]", writer.Path);
writer.WriteStartArray();
Assert.AreEqual("[0].Property1[1]", writer.Path);
writer.WriteStartArray();
Assert.AreEqual("[0].Property1[1][0]", writer.Path);
writer.WriteStartArray();
Assert.AreEqual("[0].Property1[1][0][0]", writer.Path);
writer.WriteEndObject();
Assert.AreEqual("[0]", writer.Path);
writer.WriteStartObject();
Assert.AreEqual("[1]", writer.Path);
writer.WritePropertyName("Property2");
Assert.AreEqual("[1].Property2", writer.Path);
writer.WriteStartConstructor("Constructor1");
Assert.AreEqual("[1].Property2", writer.Path);
writer.WriteNull();
Assert.AreEqual("[1].Property2[0]", writer.Path);
writer.WriteStartArray();
Assert.AreEqual("[1].Property2[1]", writer.Path);
writer.WriteValue(1);
Assert.AreEqual("[1].Property2[1][0]", writer.Path);
writer.WriteEnd();
Assert.AreEqual("[1].Property2[1]", writer.Path);
writer.WriteEndObject();
Assert.AreEqual("[1]", writer.Path);
writer.WriteEndArray();
Assert.AreEqual("", writer.Path);
}
Assert.AreEqual(@"[
{
""Property1"": [
1,
[
[
[]
]
]
]
},
{
""Property2"": new Constructor1(
null,
[
1
]
)
}
]", sb.ToString());
}
示例2: XmlToJson
public string XmlToJson(string Xml)
{
StringWriter JW = new StringWriter();
JsonTextWriter JTW = new JsonTextWriter(JW);
JTW.Formatting = Newtonsoft.Json.Formatting.Indented;
StringReader XSR = new StringReader(Xml.Trim());
XmlReader XR = XmlReader.Create(XSR);
string ValType = "";
bool InArray = false;
XR.Read();
if (!(XR.NodeType == XmlNodeType.Element && XR.Name == "xml"))
{
throw new Exception("Invalid XML Input");
}
bool Read = true;
bool NextRead = false;
try
{
if (XR.GetAttribute("in_array") == "1") InArray = true;
}
catch{}
while (Read)
{
if (! NextRead)
{
Read = XR.Read();
}
NextRead = false;
switch(XR.NodeType)
{
case (XmlNodeType.Element):
switch(XR.Name)
{
case("obj"):
JTW.WriteStartObject();
break;
case ("arr"):
JTW.WriteStartArray();
break;
case("cons" ):
JTW.WriteStartConstructor("");
break;
case ("num"):
case ("str"):
case ("bool"):
case ("undef" ):
ValType = XR.Name;
Read = XR.Read();
NextRead = true;
if (XR.NodeType == XmlNodeType.EndElement)
{
JTW.WriteValue("");
}
break;
case("prop"):
JTW.WritePropertyName(XR.GetAttribute("oname"));
break;
}
break;
case (XmlNodeType.EndElement ):
switch(XR.Name)
{
case("obj"):
JTW.WriteEndObject();
break;
case("arr"):
JTW.WriteEndArray();
break;
case("cons" ):
JTW.WriteEndConstructor();
break;
}
break;
case(XmlNodeType.Text):
switch(ValType)
{
case("num"):
try
{
JTW.WriteValue(Int32.Parse(XR.Value.Trim()));
}
catch
{
try
{
JTW.WriteValue(float.Parse(XR.Value.Trim()));
}
catch
{
JTW.WriteValue(XR.Value);
}
}
break;
case("str"):
JTW.WriteValue(XR.Value.ToString());
break;
//.........这里部分代码省略.........
示例3: WriteObjectNestedInConstructor
public void WriteObjectNestedInConstructor()
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
using (JsonWriter jsonWriter = new JsonTextWriter(sw))
{
jsonWriter.WriteStartObject();
jsonWriter.WritePropertyName("con");
jsonWriter.WriteStartConstructor("Ext.data.JsonStore");
jsonWriter.WriteStartObject();
jsonWriter.WritePropertyName("aa");
jsonWriter.WriteValue("aa");
jsonWriter.WriteEndObject();
jsonWriter.WriteEndConstructor();
jsonWriter.WriteEndObject();
}
Assert.AreEqual(@"{""con"":new Ext.data.JsonStore({""aa"":""aa""})}", sb.ToString());
}
示例4: SyncJsonObjectTypes
public static string SyncJsonObjectTypes(string OldJson, string NewJson)
{
StringReader NSR = new StringReader(NewJson);
JsonTextReader New = new JsonTextReader(NSR);
StringReader OSR = new StringReader(OldJson);
JsonTextReader Old = new JsonTextReader(OSR);
StringWriter SW = new StringWriter();
JsonTextWriter Writer = new JsonTextWriter(SW);
while (New.Read() && Old.Read())
{
if ((Old.TokenType == JsonToken.Integer) && (New.TokenType != JsonToken.Integer))
{
int NewIntValue = 0;
if (Int32.TryParse(New.Value.ToString(), out NewIntValue))
{
Writer.WriteValue(NewIntValue);
}
else
{
Writer.WriteValue(New.Value);
}
}
else if ((Old.TokenType == JsonToken.Float) && (New.TokenType != JsonToken.Float))
{
float NewFloatValue = 0;
if (float.TryParse(New.Value.ToString(), out NewFloatValue))
{
Writer.WriteValue(NewFloatValue);
}
else
{
Writer.WriteValue(New.Value);
}
}
else if ((Old.TokenType == JsonToken.Boolean) && (New.TokenType != JsonToken.Boolean))
{
string Value = New.Value.ToString();
if (Value.Equals("true"))
{
Writer.WriteValue(true);
}
else if (Value.Equals("false"))
{
Writer.WriteValue(false);
}
else
{
Writer.WriteValue(New.Value);
}
}
else if ((Old.TokenType == JsonToken.String) && (New.TokenType == JsonToken.Null))
{
Writer.WriteValue("");
}
else if ((Old.TokenType == JsonToken.StartArray) && (New.TokenType == JsonToken.String))
{
Old.Read();
if (Old.TokenType == JsonToken.EndArray)
{
Writer.WriteValue(JsonToken.StartArray);
if (New.Value.ToString() != "") Writer.WriteValue(New.Value);
Writer.WriteValue(JsonToken.EndArray);
}
else
{
Writer.WriteValue(New.Value);
}
}
else
{
if (Old.TokenType != New.TokenType)
{
Writer.Close();
New.Close();
Old.Close();
return NewJson;
}
else if (New.TokenType == JsonToken.PropertyName)
{
if (Old.Value == null || New.Value == null)
{
Writer.Close();
New.Close();
Old.Close();
return NewJson;
}
else if (!Old.Value.ToString().Equals(New.Value.ToString()))
{
Writer.Close();
New.Close();
Old.Close();
return NewJson;
}
}
switch (New.TokenType)
{
case JsonToken.StartConstructor:
Writer.WriteStartConstructor("");
break;
case JsonToken.EndConstructor:
//.........这里部分代码省略.........