|
1 |
| -using NUnit.Framework; |
| 1 | +using System.Collections.Generic; |
| 2 | +using NUnit.Framework; |
2 | 3 |
|
3 | 4 | namespace ServiceStack.Text.Tests.JsonTests
|
4 | 5 | {
|
@@ -147,13 +148,119 @@ public void Can_Serialize_StringContainerDto()
|
147 | 148 | Assert.That(src.Action.ElementId, Is.EqualTo(fromSrc.Action.ElementId));
|
148 | 149 | Assert.That(dst.Action, Is.EqualTo(fromDst.Action));
|
149 | 150 | }
|
150 |
| - |
| 151 | + |
| 152 | + |
| 153 | + readonly SimpleObj simple = new SimpleObj |
| 154 | + { |
| 155 | + value1 = "Foo", |
| 156 | + value2 = "Bar" |
| 157 | + }; |
| 158 | + |
| 159 | + [Test] |
| 160 | + public void Can_Deserialize_JsonValue() |
| 161 | + { |
| 162 | + var json = simple.ToJson(); |
| 163 | + var jsonValue = new JsonValue(json); |
| 164 | + |
| 165 | + var fromJson = jsonValue.As<SimpleObj>(); |
| 166 | + |
| 167 | + Assert.That(fromJson.value1, Is.EqualTo(simple.value1)); |
| 168 | + Assert.That(fromJson.value2, Is.EqualTo(simple.value2)); |
| 169 | + } |
| 170 | + |
| 171 | + [Test] |
| 172 | + public void Can_Serialize_JsonValue_Multiple_Times() |
| 173 | + { |
| 174 | + var json = simple.ToJson(); |
| 175 | + var jsonValue = new JsonValue(json); |
| 176 | + |
| 177 | + var jsonAfter = jsonValue.ToJson(); |
| 178 | + |
| 179 | + Assert.That(jsonAfter, Is.EqualTo(json)); |
| 180 | + } |
| 181 | + |
| 182 | + [Test] |
| 183 | + public void Can_Deserialize_JsonValue_After_Multiple_Serialize() |
| 184 | + { |
| 185 | + var json = simple.ToJson(); |
| 186 | + var jsonValue = new JsonValue(json); |
| 187 | + |
| 188 | + jsonValue = new JsonValue(jsonValue.ToJson()); |
| 189 | + |
| 190 | + var fromJson = jsonValue.As<SimpleObj>(); |
| 191 | + |
| 192 | + Assert.That(fromJson.value1, Is.EqualTo(simple.value1)); |
| 193 | + Assert.That(fromJson.value2, Is.EqualTo(simple.value2)); |
| 194 | + } |
| 195 | + |
| 196 | + [Test] |
| 197 | + public void Can_Deserialize_JsonValue_After_Multiple_Serialize_2() |
| 198 | + { |
| 199 | + var json = simple.ToJson(); |
| 200 | + var jsonValue = new JsonValue(json); |
| 201 | + |
| 202 | + json = jsonValue.ToJson(); |
| 203 | + |
| 204 | + var fromJson = json.FromJson<SimpleObj>(); |
| 205 | + |
| 206 | + Assert.That(fromJson.value1, Is.EqualTo(simple.value1)); |
| 207 | + Assert.That(fromJson.value2, Is.EqualTo(simple.value2)); |
| 208 | + } |
| 209 | + [Test] |
| 210 | + public void Can_Serialize_NestedJsonValueDto() |
| 211 | + { |
| 212 | + var scaffold = new List<JsonValue> { new JsonValue(text.ToJson()), new JsonValue(text.ToJson()) }; |
| 213 | + |
| 214 | + var container = new NestedJsonValueDto |
| 215 | + { |
| 216 | + ElementId = "container_1", |
| 217 | + ElementType = "container", |
| 218 | + // Raw nesting - won't be escaped |
| 219 | + Content = new ElementContentDto { ElementId = "container_1", Content = "container goes here" }, |
| 220 | + Action = new ElementActionDto { ElementId = "container_1", Action = "action goes here" }, |
| 221 | + Scaffolding = scaffold |
| 222 | + }; |
| 223 | + |
| 224 | + var json = container.ToJson(); |
| 225 | + |
| 226 | + var fromJson = json.FromJson<NestedJsonValueDto>(); |
| 227 | + |
| 228 | + foreach (var jsonValue in fromJson.Scaffolding) |
| 229 | + { |
| 230 | + var fromJsonValue = jsonValue.As<TextElementDto>(); |
| 231 | + Assert.That(fromJsonValue.ElementId, Is.EqualTo(text.ElementId)); |
| 232 | + Assert.That(fromJsonValue.Action.ElementId, Is.EqualTo(text.Action.ElementId)); |
| 233 | + Assert.That(fromJsonValue.Content.ElementId, Is.EqualTo(text.Content.ElementId)); |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + public class SimpleObj |
| 238 | + { |
| 239 | + public string value1 { get; set; } |
| 240 | + public string value2 { get; set; } |
| 241 | + } |
| 242 | + |
| 243 | + public class NestedSimpleJsonValue |
| 244 | + { |
| 245 | + public JsonValue Simple { get; set; } |
| 246 | + } |
| 247 | + |
| 248 | + public class NestedJsonValueDto |
| 249 | + { |
| 250 | + public string ElementType { get; set; } |
| 251 | + public string ElementId { get; set; } |
| 252 | + |
| 253 | + public ElementContentDto Content { get; set; } |
| 254 | + public ElementActionDto Action { get; set; } |
| 255 | + |
| 256 | + public List<JsonValue> Scaffolding { get; set; } |
| 257 | + } |
| 258 | + |
151 | 259 | public class TypedContainerDto
|
152 | 260 | {
|
153 | 261 | public TextElementDto Source { get; set; }
|
154 | 262 | public ImageElementDto Destination { get; set; }
|
155 | 263 | }
|
156 |
| - |
157 | 264 | // DTOs
|
158 | 265 | public class StringContainerDto // This is the request dto
|
159 | 266 | {
|
@@ -199,5 +306,6 @@ public class ElementActionDto
|
199 | 306 | public string Action { get; set; }
|
200 | 307 | // There can be more nested objects in here
|
201 | 308 | }
|
| 309 | + |
202 | 310 | }
|
203 | 311 | }
|
0 commit comments