comparison Implab.Test/JsonTests.cs @ 264:3a6e18c432be v3

Added XmlToJson xsl transformation. Added JsonXmlReader.CreateJsonXmlReader(...) methods Added SerializationHelpers.SerializeJson/DeserializeJson methods
author cin
date Mon, 16 Apr 2018 18:43:49 +0300
parents f1696cdc3d7a
children 74e048cbaac8
comparison
equal deleted inserted replaced
263:711572866e0c 264:3a6e18c432be
4 using Implab.Xml; 4 using Implab.Xml;
5 using System.Xml; 5 using System.Xml;
6 using Implab.Formats; 6 using Implab.Formats;
7 using Implab.Formats.Json; 7 using Implab.Formats.Json;
8 using System.IO; 8 using System.IO;
9 using Implab.Test.Model;
9 10
10 namespace Implab.Test { 11 namespace Implab.Test {
11 public class JsonTests { 12 public class JsonTests {
12 13
13 [Fact] 14 [Fact]
112 DumpJsonParse("[1,\"\",2,3]"); 113 DumpJsonParse("[1,\"\",2,3]");
113 DumpJsonParse("[{\"info\": [7,8,9]}]"); 114 DumpJsonParse("[{\"info\": [7,8,9]}]");
114 DumpJsonFlatParse("[1,2,\"\",[3,4],{\"info\": [5,6]},{\"num\": [7,8,null]}, null,[null]]"); 115 DumpJsonFlatParse("[1,2,\"\",[3,4],{\"info\": [5,6]},{\"num\": [7,8,null]}, null,[null]]");
115 } 116 }
116 117
118 [Fact]
119 public void XmlToJsonTransform() {
120 var person = new Person {
121 FirstName = "Charlie",
122 LastName = "Brown",
123 Age = 19,
124 AgeSpecified = true
125 };
126
127 var doc = SerializationHelpers.SerializeAsXmlDocument(person);
128
129 using (var writer = new StringWriter()) {
130 XmlToJson.Default.Transform(doc,null, writer);
131 Console.WriteLine(writer.ToString());
132 }
133 }
134
135 [Fact]
136 public void JsonSerialization() {
137 var person = new Person {
138 FirstName = "Charlie",
139 LastName = "Brown",
140 Age = 19,
141 AgeSpecified = true,
142 Tags = new [] { "brave", "stupid" }
143 };
144
145 var data = SerializationHelpers.SerializeJsonAsString(person);
146 Console.WriteLine(data);
147 var clone = SerializationHelpers.DeserializeJsonFromString<Person>(data);
148
149 Assert.Equal(person.FirstName, clone.FirstName);
150 Assert.Equal(person.LastName, clone.LastName);
151 Assert.Equal(person.Age, clone.Age);
152 Assert.Equal(person.AgeSpecified, clone.AgeSpecified);
153 Assert.Equal(person.Tags, person.Tags);
154 }
155
117 void AssertRead(XmlReader reader, XmlNodeType expected) { 156 void AssertRead(XmlReader reader, XmlNodeType expected) {
118 Assert.True(reader.Read()); 157 Assert.True(reader.Read());
119 Console.WriteLine($"{new string(' ', reader.Depth * 2)}{reader}"); 158 Console.WriteLine($"{new string(' ', reader.Depth * 2)}{reader}");
120 Assert.Equal(expected, reader.NodeType); 159 Assert.Equal(expected, reader.NodeType);
121 } 160 }