本文整理汇总了C#中JsonSerializer.ToString方法的典型用法代码示例。如果您正苦于以下问题:C# JsonSerializer.ToString方法的具体用法?C# JsonSerializer.ToString怎么用?C# JsonSerializer.ToString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JsonSerializer
的用法示例。
在下文中一共展示了JsonSerializer.ToString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestAlias
public void TestAlias()
{
var js = new JsonSerializer();
js.JsonOptions.Indent = "";
js.JsonOptions.FieldSeparator = "";
js.Options.TagMode = TagMode.Aliases;
var v = new SampleTree { Value = 9 };
var result = js.ToString(v);
Assert.AreEqual("{\"a\":9,\"b\":null}", result);
js.Options.TagMode = TagMode.Names;
var result1 = js.ToString(v);
Assert.AreEqual("{\"Value\":9,\"Children\":null}", result1);
js.Options.TagMode = TagMode.Ids;
var result2 = js.ToString(v);
Assert.AreEqual("{\"AAAB\":9,\"AAAC\":null}", result2);
var prev = IdGenerator.GetNextId();
for (int i = 0; i < 2 * 52 - 5; ++i) {
var next = IdGenerator.GetNextId();
Assert.IsTrue(String.CompareOrdinal(prev, next) < 0);
prev = next;
}
Assert.AreEqual("AABz", IdGenerator.GetNextId());
var jd = new JsonDeserializer();
jd.Options.TagMode = TagMode.Aliases;
var w = new SampleTree();
jd.FromString(w, result);
Assert.AreEqual(9, w.Value);
Assert.AreEqual(null, w.Children);
}
示例2: TestJsonLongListInt
public void TestJsonLongListInt()
{
var list1 = new SampleMatrix { M = new List<List<int>>() };
for (int i = 0; i < 300; ++i) {
list1.M.Add(new List<int>());
for (int j = 0; j < 400; ++j)
list1.M[i].Add(i * j * 97);
}
var js = new JsonSerializer();
var result1 = js.ToString(list1);
Assert.IsTrue(result1 != "");
var list2 = new SampleMatrix();
var jd = new JsonDeserializer();
jd.FromString(list2, result1);
Assert.AreEqual(list1.M.Count, list2.M.Count);
for (int i = 0; i < list1.M.Count; ++i) {
for (int j = 0; j < list1.M[i].Count; ++j)
Assert.AreEqual(list1.M[i][j], list2.M[i][j]);
}
var jdg = new SampleMatrix_JsonDeserializer();
var list3 = (SampleMatrix)jdg.FromString(result1);
Assert.AreEqual(list1.M.Count, list3.M.Count);
}
示例3: TestJsonToBinarySimple
public void TestJsonToBinarySimple()
{
var bs = new BinarySerializer();
var bd = new BinaryDeserializer();
var js = new JsonSerializer();
js.JsonOptions.FieldSeparator = "";
js.JsonOptions.Indent = "";
js.JsonOptions.SaveRootClass = true;
var jd = new JsonDeserializer();
var data1 = "{\"class\":\"YuzuTest.Sample1, YuzuTest\",\"X\":98,\"Y\":\"\"}";
var result1 = js.ToString(bd.FromBytes(bs.ToBytes(jd.FromString(data1))));
Assert.AreEqual(data1, result1);
}
示例4: TestAbstract
public void TestAbstract()
{
var js = new JsonSerializer();
js.JsonOptions.Indent = "";
js.JsonOptions.FieldSeparator = "";
js.JsonOptions.SaveRootClass = true;
var jd = new JsonDeserializer();
SampleAbstract v1 = new SampleConcrete { XX = 81 };
var result1 = js.ToString(v1);
Assert.AreEqual("{\"class\":\"YuzuTest.SampleConcrete, YuzuTest\",\"XX\":81}", result1);
var w1 = jd.FromString<SampleAbstract>(result1);
Assert.AreEqual((v1 as SampleConcrete).XX, (w1 as SampleConcrete).XX);
var w1g = (SampleConcrete)SampleAbstract_JsonDeserializer.Instance.FromString(result1);
Assert.AreEqual((v1 as SampleConcrete).XX, w1g.XX);
var v2 = new List<SampleAbstract>();
v2.Add(new SampleConcrete { XX = 51 });
var w2 = jd.FromString<List<SampleAbstract>>(js.ToString(v2));
Assert.AreEqual(v2.Count, w2.Count);
Assert.AreEqual((v2[0] as SampleConcrete).XX, (w2[0] as SampleConcrete).XX);
}
示例5: TestBinaryToJsonUnknown
public void TestBinaryToJsonUnknown()
{
var bs = new BinarySerializer();
var bd = new BinaryDeserializer();
var js = new JsonSerializer();
js.JsonOptions.SaveRootClass = true;
var jd = new JsonDeserializer();
var n = "NewType".ToCharArray().Select(ch => (byte)ch).ToList();
var data1 =
new byte[] { 0x20, 0x01, 0x00, (byte)n.Count }.Concat(n).
Concat(new byte[] { 0x01, 0x00, 0x01, (byte)'X', (byte)RoughType.String, 0x01, 0x00,
0x03, (byte)'a', (byte)'b', (byte)'c', 0, 0
}).ToArray();
var result1 = bs.ToBytes(jd.FromString(js.ToString(bd.FromBytes(data1))));
CollectionAssert.AreEqual(data1, result1);
}
示例6: TestAfterDeserialization
public void TestAfterDeserialization()
{
var js = new JsonSerializer();
var v0 = new SampleAfter { X = "m" };
var result0 = js.ToString(v0);
Assert.AreEqual("{\n\t\"X\":\"m\"\n}", result0);
var jd = new JsonDeserializer();
var w0 = new SampleAfter();
jd.FromString(w0, result0);
Assert.AreEqual("m1", w0.X);
var w1 = new SampleAfter2();
jd.FromString(w1, result0);
Assert.AreEqual("m231", w1.X);
var w2 = (SampleAfter2)SampleAfter2_JsonDeserializer.Instance.FromString(result0);
Assert.AreEqual("m231", w2.X);
}
示例7: TestJsonLongArrayStr
public void TestJsonLongArrayStr()
{
var list1 = new SampleArray { A = new string[100000] };
for (int i = 0; i < list1.A.Length; ++i)
list1.A[i] = i.ToString();
var js = new JsonSerializer();
js.JsonOptions.ArrayLengthPrefix = true;
var result1 = js.ToString(list1);
Assert.IsTrue(result1 != "");
var list2 = new SampleArray();
var jd = new JsonDeserializer();
jd.JsonOptions.ArrayLengthPrefix = true;
jd.FromString(list2, result1);
Assert.AreEqual(list1.A.Length, list2.A.Length);
var jdg = new SampleArray_JsonDeserializer();
var list3 = (SampleArray)jdg.FromString(result1);
Assert.AreEqual(list1.A.Length, list3.A.Length);
}
示例8: TestJsonLongListDouble
public void TestJsonLongListDouble()
{
var list1 = new List<double>();
var rnd = new Random();
for (int i = 0; i < 50000; ++i)
list1.Add(i * 19234.73457);
for (int i = 0; i < 50000; ++i)
list1.Add(rnd.NextDouble());
for (int i = 0; i < 10000; ++i)
list1.Add(rnd.NextDouble() * 1e27);
for (int i = 0; i < 10000; ++i)
list1.Add(rnd.NextDouble() * 1e-27);
var js = new JsonSerializer();
js.JsonOptions.Indent = "";
js.JsonOptions.FieldSeparator = "";
var result1 = js.ToString(list1);
Assert.IsTrue(result1 != "");
var list2 = (new JsonDeserializer()).FromString<List<double>>(result1);
for (int i = 0; i < list1.Count; ++i)
Assert.AreEqual(list1[i], list2[i]);
}
示例9: TestFloat
public void TestFloat()
{
var js = new JsonSerializer();
js.Options.TagMode = TagMode.Names;
var v = new SampleFloat { F = 1e-20f, D = -3.1415e100d };
js.JsonOptions.Indent = "";
var result1 = js.ToString(v);
Assert.AreEqual("{\n\"F\":1E-20,\n\"D\":-3.1415E100\n}", result1);
var w = new SampleFloat();
var jd = new JsonDeserializer();
jd.Options.TagMode = TagMode.Names;
jd.FromString(w, result1);
Assert.AreEqual(v.F, w.F);
Assert.AreEqual(v.D, w.D);
Assert.AreEqual("NaN", js.ToString(Double.NaN));
Assert.AreEqual("Infinity", js.ToString(Double.PositiveInfinity));
Assert.AreEqual("-Infinity", js.ToString(Double.NegativeInfinity));
}
示例10: TestEscape
public void TestEscape()
{
var js = new JsonSerializer();
js.JsonOptions.Indent = "";
js.JsonOptions.FieldSeparator = "";
var s = "\"/{\u0001}\n\t\"\"";
var v = new Sample1 { Y = s };
var result = js.ToString(v);
Assert.AreEqual("{\"X\":0,\"Y\":\"\\\"/{\\u0001}\\n\\t\\\"\\\"\"}", result);
var w = new Sample1();
var jd = new JsonDeserializer();
jd.FromString(w, result);
Assert.AreEqual(s, w.Y);
v.Y = result;
var result1 = js.ToString(v);
jd.FromString(w, result1);
Assert.AreEqual(result, w.Y);
v.Y = "привет";
var result2 = js.ToString(v);
Assert.AreEqual("{\"X\":0,\"Y\":\"привет\"}", result2);
jd.FromString(w, result2);
Assert.AreEqual(v.Y, w.Y);
jd.FromString(w, "{\"X\":0,\"Y\":\"\u043F\u0440\u0438\u0432\u0435\u0442\"}");
Assert.AreEqual(v.Y, w.Y);
}
示例11: TestErrors
public void TestErrors()
{
var js = new JsonSerializer();
XAssert.Throws<YuzuException>(() => js.ToString(new Empty()), "Empty");
XAssert.Throws<YuzuException>(() => js.ToString(new SampleCollectionWithField<int>()), "collection");
XAssert.Throws<YuzuException>(() => js.ToString(new SampleInterfacedFieldDup()), "X");
XAssert.Throws<YuzuException>(() => js.ToString(new BadMerge1()), "merge");
XAssert.Throws<YuzuException>(() => js.ToString(new BadMerge2()), "merge");
var jd = new JsonDeserializer();
var w = new Sample1();
XAssert.Throws<YuzuException>(() => jd.FromString(w, "{}"), "X");
XAssert.Throws<YuzuException>(() => jd.FromString("{null:1}"), "'n'");
XAssert.Throws<YuzuException>(() => jd.FromString(w, "{ \"X\" }"), ":");
XAssert.Throws<YuzuException>(() => jd.FromString(w, "nn"), "'u'");
XAssert.Throws<YuzuException>(() => jd.FromString(w, "{ \"X\":1, \"Y\": \"\\z\" }"), "z");
XAssert.Throws<YuzuException>(() => jd.FromString(w, "{ \"X\":1, \"Y\": \"\\uQ\" }"), "Q");
XAssert.Throws<YuzuException>(() => jd.FromString(new SampleBool(), "{ \"B\": 1 }"), "1");
XAssert.Throws<YuzuException>(() => jd.FromString(w, "{ ,}"), ",");
XAssert.Throws<YuzuException>(() => jd.FromString(w, "{ \"Y\": \"q\" }"), "'X'");
XAssert.Throws<YuzuException>(() => jd.FromString(w, "[]"), "'Sample1'");
XAssert.Throws<YuzuException>(() => jd.FromString(w, "{ \"class\": \"Q\" }"), "'Q'");
XAssert.Throws<YuzuException>(() => jd.FromString(w, "{ \"class\": \"YuzuTest.Sample2, YuzuTest\" }"), ".Sample2");
XAssert.Throws<YuzuException>(() => jd.FromString(
new Sample2Allow(), "{ \"class\": \"YuzuTest.Sample1, YuzuTest\" }"), ".Sample1");
XAssert.Throws<YuzuException>(() => jd.FromString(new SamplePoint(), "[ \"QQ\" ]"), "");
XAssert.Throws<YuzuException>(() => jd.FromString(new object(), "{}"), "object");
XAssert.Throws<EndOfStreamException>(() => jd.FromString(""), "");
XAssert.Throws<EndOfStreamException>(() => jd.FromString(w, "{ \"X\": 1"));
XAssert.Throws<YuzuException>(() => jd.FromString(
"{\"class\":\"YuzuTest.SampleInterfaceField, YuzuTest\",\"I\":{}}"), "class");
XAssert.Throws<YuzuException>(() => jd.FromString(
"{\"class\":\"YuzuTest.SampleInterfaceField, YuzuTest\",\"I\":{\"class\":\"YuzuTest.Sample1, YuzuTest\"}}"),
"ISample");
jd.Options.ReportErrorPosition = true;
XAssert.Throws<YuzuException>(() => jd.FromString(w, " z"), "7");
jd.Options.ReportErrorPosition = false;
try {
jd.FromString(w, " z");
}
catch (YuzuException e) {
Assert.IsFalse(e.Message.Contains("7"));
}
}
示例12: TestCollection
public void TestCollection()
{
var js = new JsonSerializer();
js.JsonOptions.Indent = "";
var jd = new JsonDeserializer();
var v0 = new SampleWithCollection();
v0.A.Add(new SampleInterfaced { X = 9 });
v0.B.Add(7);
v0.B.Add(6);
var result0 = js.ToString(v0);
Assert.AreEqual(
"{\n" +
"\"A\":[\n{\n\"class\":\"YuzuTest.SampleInterfaced, YuzuTest\",\n\"X\":9\n}\n],\n" +
"\"B\":[\n7,\n6\n]\n" +
"}",
result0);
var w0 = new SampleWithCollection();
jd.FromString(w0, result0);
Assert.AreEqual(1, w0.A.Count);
Assert.IsInstanceOfType(w0.A.First(), typeof(SampleInterfaced));
Assert.AreEqual(9, w0.A.First().X);
CollectionAssert.AreEqual(new int[] { 7, 6 }, w0.B.ToList());
var w1 = (SampleWithCollection)SampleWithCollection_JsonDeserializer.Instance.FromString(result0);
Assert.AreEqual(1, w1.A.Count);
Assert.IsInstanceOfType(w1.A.First(), typeof(SampleInterfaced));
Assert.AreEqual(9, w1.A.First().X);
CollectionAssert.AreEqual(new int[] { 7, 6 }, w1.B.ToList());
var v2 = new SampleCollection<int> { 2, 5, 4 };
var result1 = js.ToString(v2);
Assert.AreEqual("[\n2,\n5,\n4\n]", result1);
var w2 = (SampleCollection<int>)SampleCollection_Int32_JsonDeserializer.Instance.FromString(result1);
CollectionAssert.AreEqual(v2.ToList(), w2.ToList());
var w2g = (SampleExplicitCollection<int>)
SampleExplicitCollection_Int32_JsonDeserializer.Instance.FromString(result1);
CollectionAssert.AreEqual(v2.ToList(), w2g.ToList());
var v3 = new SampleConcreteCollection { 8, 3, 1 };
var result3 = js.ToString(v3);
Assert.AreEqual("[\n8,\n3,\n1\n]", result3);
var w3 = new SampleConcreteCollection();
jd.FromString(w3, result3);
CollectionAssert.AreEqual(v3.ToList(), w3.ToList());
var w3g = (SampleConcreteCollection)
SampleConcreteCollection_JsonDeserializer.Instance.FromString(result3);
CollectionAssert.AreEqual(v3.ToList(), w3g.ToList());
}
示例13: TestLong
public void TestLong()
{
var js = new JsonSerializer();
var v1 = new SampleLong { S = -1L << 33, U = 1UL << 33 };
js.JsonOptions.Indent = "";
var result = js.ToString(v1);
Assert.AreEqual("{\n\"S\":-8589934592,\n\"U\":8589934592\n}", result);
var v2 = new SampleLong();
var jd = new JsonDeserializer();
jd.FromString(v2, result);
Assert.AreEqual(v1.S, v2.S);
Assert.AreEqual(v1.U, v2.U);
js.JsonOptions.Int64AsString = true;
var result1 = js.ToString(v1);
Assert.AreEqual("{\n\"S\":\"-8589934592\",\n\"U\":\"8589934592\"\n}", result1);
var jd1 = new JsonDeserializer();
jd1.JsonOptions.Int64AsString = true;
jd1.FromString(v2, result1);
Assert.AreEqual(v1.S, v2.S);
Assert.AreEqual(v1.U, v2.U);
js.JsonOptions.Int64AsString = false;
v1.S = long.MinValue;
v1.U = ulong.MaxValue;
jd.FromString(v2, js.ToString(v1));
Assert.AreEqual(v1.S, v2.S);
Assert.AreEqual(v1.U, v2.U);
}
示例14: TestInterface
public void TestInterface()
{
var js = new JsonSerializer();
js.JsonOptions.Indent = "";
js.JsonOptions.FieldSeparator = "";
var v1 = new SampleInterfaceField { I = new SampleInterfaced { X = 34 } };
var result1 = js.ToString(v1);
Assert.AreEqual("{\"I\":{\"class\":\"YuzuTest.SampleInterfaced, YuzuTest\",\"X\":34}}", result1);
var w1 = new SampleInterfaceField();
var jd = new JsonDeserializer();
jd.FromString(w1, result1);
Assert.IsInstanceOfType(w1.I, typeof(SampleInterfaced));
Assert.AreEqual(34, w1.I.X);
var w1g = (SampleInterfaceField)SampleInterfaceField_JsonDeserializer.Instance.FromString(result1);
Assert.IsInstanceOfType(w1g.I, typeof(SampleInterfaced));
Assert.AreEqual(34, w1g.I.X);
var w1n = (SampleInterfaceField)SampleInterfaceField_JsonDeserializer.Instance.FromString("{\"I\":null}");
Assert.AreEqual(null, w1n.I);
var v2 = new List<ISample> { null, new SampleInterfaced { X = 37 } };
var result2 = js.ToString(v2);
Assert.AreEqual("[null,{\"class\":\"YuzuTest.SampleInterfaced, YuzuTest\",\"X\":37}]", result2);
var w2 = new List<ISample>();
jd.FromString(w2, result2);
Assert.AreEqual(2, w2.Count);
Assert.IsNull(w2[0]);
Assert.AreEqual(37, w2[1].X);
ISampleField v3 = new SampleInterfacedField { X = 41 };
js.JsonOptions.SaveRootClass = true;
var result3 = js.ToString(v3);
Assert.AreEqual("{\"class\":\"YuzuTest.SampleInterfacedField, YuzuTest\",\"X\":41}", result3);
var w3 = (ISampleField)jd.FromString(result3);
Assert.AreEqual(41, w3.X);
}
示例15: TestMemberOfInterface
public void TestMemberOfInterface()
{
var js = new JsonSerializer();
js.JsonOptions.Indent = "";
js.JsonOptions.FieldSeparator = "";
var v1 = new List<ISampleMember>();
var result1 = js.ToString(v1);
Assert.AreEqual("[]", result1);
var jd = new JsonDeserializer();
var w1 = new List<ISampleMember>();
jd.FromString(w1, result1);
Assert.AreEqual(0, w1.Count);
v1.Add(new SampleMemberI());
var result1p = js.ToString(v1);
Assert.AreEqual("[{\"class\":\"YuzuTest.SampleMemberI, YuzuTest\"}]", result1p);
jd.FromString(w1, result1p);
Assert.AreEqual(71, w1[0].X);
var v2 = new List<ISampleMember> { new SampleMemberI(), new SampleMemberI { X = 99 } };
var result2 = js.ToString(v2);
var w2 = new List<ISampleMember>();
jd.FromString(w2, result2);
YuzuGen.System.Collections.Generic.List_ISampleMember_JsonDeserializer.Instance.FromString(w2, result2);
Assert.AreEqual(v2[0].X, w2[0].X);
Assert.AreEqual(v2[1].X, w2[1].X);
Assert.AreEqual("[]", js.ToString(new List<SampleMemberAbstract>()));
var v3 = new List<SampleMemberAbstract> { new SampleMemberConcrete() };
var result3 = js.ToString(v3);
Assert.AreEqual("[{\"class\":\"YuzuTest.SampleMemberConcrete, YuzuTest\"}]", result3);
var w3 = new List<SampleMemberAbstract>();
jd.FromString(w3, result3);
Assert.AreEqual(72, w3[0].X);
}