Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Normalize line endings

  • Loading branch information...
commit 4a6c5e53589febf855a96666cff8c440cede9d97 1 parent 1c40278
@desunit desunit authored
Showing with 5,588 additions and 5,588 deletions.
  1. +226 −226 tests/ServiceStack.Text.Tests/DynamicObjectTests.cs
  2. +118 −118 tests/ServiceStack.Text.Tests/GenericCollectionTests.cs
  3. +251 −251 tests/ServiceStack.Text.Tests/InterfaceTests.cs
  4. +37 −37 tests/ServiceStack.Text.Tests/JsonTests/AnonymousDeserializationTests.cs
  5. +278 −278 tests/ServiceStack.Text.Tests/JsonTests/BasicJsonTests.cs
  6. +136 −136 tests/ServiceStack.Text.Tests/JsonTests/BasicPropertiesTests.cs
  7. +69 −69 tests/ServiceStack.Text.Tests/JsonTests/CamelCaseTests.cs
  8. +78 −78 tests/ServiceStack.Text.Tests/JsonTests/ContractByInterfaceTests.cs
  9. +91 −91 tests/ServiceStack.Text.Tests/JsonTests/CustomSerializerTests.cs
  10. +51 −51 tests/ServiceStack.Text.Tests/JsonTests/DictionaryTests.cs
  11. +215 −215 tests/ServiceStack.Text.Tests/JsonTests/EscapedCharsTests.cs
  12. +46 −46 tests/ServiceStack.Text.Tests/JsonTests/JsonDataContractCompatibilityTests.cs
  13. +435 −435 tests/ServiceStack.Text.Tests/JsonTests/JsonDateTimeTests.cs
  14. +430 −430 tests/ServiceStack.Text.Tests/JsonTests/PolymorphicListTests.cs
  15. +6 −6 tests/ServiceStack.Text.Tests/JsonTests/ThrowOnDeserializeErrorTest.cs
  16. +5 −5 tests/ServiceStack.Text.Tests/JsvTests/JsvDeserializeTypeTest.cs
  17. +239 −239 tests/ServiceStack.Text.Tests/JsvTests/TypeSerializerToStringDictionaryTests.cs
  18. +72 −72 tests/ServiceStack.Text.Tests/MessagingTests.cs
  19. +182 −182 tests/ServiceStack.Text.Tests/NullableTypesTests.cs
  20. +23 −23 tests/ServiceStack.Text.Tests/QueryStringSerializerTests.cs
  21. +276 −276 tests/ServiceStack.Text.Tests/ReportedIssues.cs
  22. +281 −281 tests/ServiceStack.Text.Tests/ServiceStack.Text.Tests.csproj
  23. +67 −67 tests/ServiceStack.Text.Tests/SpecialTypesTests.cs
  24. +91 −91 tests/ServiceStack.Text.Tests/StringConverterUtilsTests.cs
  25. +151 −151 tests/ServiceStack.Text.Tests/StringSerializerNorthwindDatabaseTests.cs
  26. +141 −141 tests/ServiceStack.Text.Tests/StringSerializerTranslationTests.cs
  27. +191 −191 tests/ServiceStack.Text.Tests/StringTests.cs
  28. +148 −148 tests/ServiceStack.Text.Tests/StructTests.cs
  29. +223 −223 tests/ServiceStack.Text.Tests/Support/BenchmarkTests.cs
  30. +106 −106 tests/ServiceStack.Text.Tests/Support/MovieDtos.cs
  31. +10 −10 tests/ServiceStack.Text.Tests/Support/TableItem.cs
  32. +132 −132 tests/ServiceStack.Text.Tests/TestBase.cs
  33. +221 −221 tests/ServiceStack.Text.Tests/UseCases/CentroidTests.cs
  34. +128 −128 tests/ServiceStack.Text.Tests/UseCases/GitHubRestTests.cs
  35. +159 −159 tests/ServiceStack.Text.Tests/UseCases/GithubV3ApiTests.cs
  36. +192 −192 tests/ServiceStack.Text.Tests/Utils/DateTimeSerializerTests.cs
  37. +83 −83 tests/ServiceStack.Text.Tests/Utils/JsvFormatterTests.cs
View
452 tests/ServiceStack.Text.Tests/DynamicObjectTests.cs
@@ -1,227 +1,227 @@
-using System;
-using System.Collections.Generic;
-using NUnit.Framework;
-
-namespace ServiceStack.Text.Tests
-{
- [TestFixture]
- public class DynamicObjectTests
- : TestBase
- {
- public class UrlStatus
- {
- public int Status { get; set; }
- public string Url { get; set; }
- }
-
- [TearDown]
- public void TearDown()
- {
- JsConfig.Reset();
- }
-
- [Test]
- public void Dictionary_Object_UrlStatus()
- {
- var urlStatus = new UrlStatus {
- Status = 301,
- Url = "http://www.ehow.com/how_5615409_create-pdfs-using-bean.html",
- };
-
- var map = new Dictionary<string, object>
- {
- {"Status","OK"},
- {"Url","http://www.ehow.com/m/how_5615409_create-pdfs-using-bean.html"},
- {"Parent Url","http://www.ehow.com/mobilearticle35.xml"},
- {"Redirect Chai", urlStatus},
- };
-
- var json = JsonSerializer.SerializeToString(map);
- var fromJson = JsonSerializer.DeserializeFromString<Dictionary<string, object>>(json);
-
- Assert.That(fromJson["Status"], Is.EqualTo(map["Status"]));
- Assert.That(fromJson["Url"], Is.EqualTo(map["Url"]));
- Assert.That(fromJson["Parent Url"], Is.EqualTo(map["Parent Url"]));
-
- var actualStatus = (UrlStatus)fromJson["Redirect Chai"];
- Assert.That(actualStatus.Status, Is.EqualTo(urlStatus.Status));
- Assert.That(actualStatus.Url, Is.EqualTo(urlStatus.Url));
-
- Console.WriteLine("JSON: " + json);
- }
-
- public class PocoWithKvp
- {
- public KeyValuePair<string, string>[] Values { get; set; }
- }
-
- [Test]
- public void Can_Serailize_KVP_array()
- {
- var kvpArray = new[] {
- new KeyValuePair<string, string>("Key", "Foo"),
- new KeyValuePair<string, string>("Value", "Bar"),
- };
- var dto = new PocoWithKvp {
- Values = kvpArray
- };
-
- Console.WriteLine(dto.ToJson());
-
- Serialize(dto, includeXml: false);
- }
-
- [Test]
- public void Can_deserialize_object_string()
- {
- JsConfig.TryToParsePrimitiveTypeValues = true;
- JsConfig.ConvertObjectTypesIntoStringDictionary = true;
-
- var json = "12345";
- var deserialized = JsonSerializer.DeserializeFromString<object>(json);
- Assert.That(deserialized, Is.EqualTo(json));
- }
-
- [Test]
- public void Can_deserialize_object_array()
- {
- JsConfig.TryToParsePrimitiveTypeValues = true;
- JsConfig.ConvertObjectTypesIntoStringDictionary = true;
-
- var json = "[1,2,3]";
- var deserialized = JsonSerializer.DeserializeFromString<object>(json);
- Assert.That(deserialized, Is.InstanceOf<List<object>>());
- Assert.That(((List<object>)deserialized)[0], Is.EqualTo(1));
- Assert.That(((List<object>)deserialized)[1], Is.EqualTo(2));
- Assert.That(((List<object>)deserialized)[2], Is.EqualTo(3));
- }
-
- [Test]
- public void Can_deserialize_object_dictionary()
- {
- JsConfig.TryToParsePrimitiveTypeValues = true;
- JsConfig.ConvertObjectTypesIntoStringDictionary = true;
-
- var json = "{\"foo\":\"bar\"}";
- var deserialized = JsonSerializer.DeserializeFromString<object>(json);
- Assert.That(deserialized, Is.InstanceOf<Dictionary<string, object>>());
- Assert.That(((Dictionary<string, object>)deserialized)["foo"], Is.EqualTo("bar"));
- }
-
- [Test]
- public void Can_deserialize_object_dictionary_with_mixed_values_and_nulls_and_empty_array()
- {
- JsConfig.TryToParsePrimitiveTypeValues = true;
- JsConfig.ConvertObjectTypesIntoStringDictionary = true;
-
- var json = "{\"stringIntValue\": \"-13\",\"intValue\": -13,\"nullValue\": null,\"stringDecimalValue\": \"5.9\",\"decimalValue\": 5.9,\"emptyArrayValue\": [],\"stringValue\": \"Foo\",\"stringWithDigitsValue\": \"OR345\",\"dateValue\":\"\\/Date(785635200000)\\/\"}";
- var deserialized = JsonSerializer.DeserializeFromString<object>(json);
- Assert.That(deserialized, Is.InstanceOf<Dictionary<string, object>>());
- var dict = (Dictionary<string, object>) deserialized;
- Assert.That(dict["stringIntValue"], Is.EqualTo("-13"));
- Assert.That(dict["intValue"], Is.EqualTo(-13));
- Assert.That(dict["intValue"], Is.Not.EqualTo(dict["stringIntValue"]));
- Assert.That(dict["nullValue"], Is.Null);
- Assert.That(dict["stringDecimalValue"], Is.EqualTo("5.9"));
- Assert.That(dict["decimalValue"], Is.EqualTo(5.9f));
- Assert.That(dict["decimalValue"], Is.Not.EqualTo(dict["stringDecimalValue"]));
- Assert.That(dict["emptyArrayValue"], Is.Not.Null);
- Assert.That(dict["stringValue"], Is.EqualTo("Foo"));
- Assert.That(dict["stringWithDigitsValue"], Is.EqualTo("OR345"));
- Assert.That(dict["dateValue"], Is.EqualTo(new DateTime(1994, 11, 24, 0, 0, 0, DateTimeKind.Utc)));
- }
-
- [Test]
- public void Can_deserialize_object_dictionary_with_line_breaks()
- {
- JsConfig.TryToParsePrimitiveTypeValues = true;
- JsConfig.ConvertObjectTypesIntoStringDictionary = true;
-
- var json = @"{
- ""value""
-:
- 5 ,
-
- }";
-
- var deserialized = JsonSerializer.DeserializeFromString<object>(json);
- Assert.That(deserialized, Is.InstanceOf<Dictionary<string, object>>());
- var dict = (Dictionary<string, object>) deserialized;
- Assert.That(dict["value"], Is.EqualTo(5));
- }
-
- [Test]
- public void Can_deserialize_object_array_with_line_breaks_before_first_element()
- {
- JsConfig.TryToParsePrimitiveTypeValues = true;
- JsConfig.ConvertObjectTypesIntoStringDictionary = true;
-
- var json = @"[
- {
- ""name"":""foo""
- }]";
-
- var deserialized = JsonSerializer.DeserializeFromString<object>(json);
- Assert.That(deserialized, Is.InstanceOf<List<object>>());
- var arrayValues = (List<object>) deserialized;
- Assert.That(arrayValues.Count, Is.EqualTo(1));
- Assert.That(arrayValues[0], Is.Not.Null);
- }
-
- [Test]
- public void Can_deserialize_object_array_with_line_breaks_after_last_element()
- {
- JsConfig.TryToParsePrimitiveTypeValues = true;
- JsConfig.ConvertObjectTypesIntoStringDictionary = true;
-
- var json = @"[{
- ""name"":""foo""
- }
- ]";
-
- var deserialized = JsonSerializer.DeserializeFromString<object>(json);
- Assert.That(deserialized, Is.InstanceOf<List<object>>());
- var arrayValues = (List<object>) deserialized;
- Assert.That(arrayValues.Count, Is.EqualTo(1));
- Assert.That(arrayValues[0], Is.Not.Null);
- }
-
- [Test]
- public void Can_deserialize_object_array_with_line_breaks_around_element()
- {
- JsConfig.TryToParsePrimitiveTypeValues = true;
- JsConfig.ConvertObjectTypesIntoStringDictionary = true;
-
- var json = @"[
- {
- ""name"":""foo""
- }
- ]";
-
- var deserialized = JsonSerializer.DeserializeFromString<object>(json);
- Assert.That(deserialized, Is.InstanceOf<List<object>>());
- var arrayValues = (List<object>) deserialized;
- Assert.That(arrayValues.Count, Is.EqualTo(1));
- Assert.That(arrayValues[0], Is.Not.Null);
- }
-
- [Test]
- public void Can_deserialize_object_array_with_line_breaks_around_number_element()
- {
- JsConfig.TryToParsePrimitiveTypeValues = true;
- JsConfig.ConvertObjectTypesIntoStringDictionary = true;
-
- var json = @"[
-
- 5
-
- ]";
-
- var deserialized = JsonSerializer.DeserializeFromString<object>(json);
- Assert.That(deserialized, Is.InstanceOf<List<object>>());
- var arrayValues = (List<object>) deserialized;
- Assert.That(arrayValues.Count, Is.EqualTo(1));
- Assert.That(arrayValues[0], Is.EqualTo(5));
- }
- }
+using System;
+using System.Collections.Generic;
+using NUnit.Framework;
+
+namespace ServiceStack.Text.Tests
+{
+ [TestFixture]
+ public class DynamicObjectTests
+ : TestBase
+ {
+ public class UrlStatus
+ {
+ public int Status { get; set; }
+ public string Url { get; set; }
+ }
+
+ [TearDown]
+ public void TearDown()
+ {
+ JsConfig.Reset();
+ }
+
+ [Test]
+ public void Dictionary_Object_UrlStatus()
+ {
+ var urlStatus = new UrlStatus {
+ Status = 301,
+ Url = "http://www.ehow.com/how_5615409_create-pdfs-using-bean.html",
+ };
+
+ var map = new Dictionary<string, object>
+ {
+ {"Status","OK"},
+ {"Url","http://www.ehow.com/m/how_5615409_create-pdfs-using-bean.html"},
+ {"Parent Url","http://www.ehow.com/mobilearticle35.xml"},
+ {"Redirect Chai", urlStatus},
+ };
+
+ var json = JsonSerializer.SerializeToString(map);
+ var fromJson = JsonSerializer.DeserializeFromString<Dictionary<string, object>>(json);
+
+ Assert.That(fromJson["Status"], Is.EqualTo(map["Status"]));
+ Assert.That(fromJson["Url"], Is.EqualTo(map["Url"]));
+ Assert.That(fromJson["Parent Url"], Is.EqualTo(map["Parent Url"]));
+
+ var actualStatus = (UrlStatus)fromJson["Redirect Chai"];
+ Assert.That(actualStatus.Status, Is.EqualTo(urlStatus.Status));
+ Assert.That(actualStatus.Url, Is.EqualTo(urlStatus.Url));
+
+ Console.WriteLine("JSON: " + json);
+ }
+
+ public class PocoWithKvp
+ {
+ public KeyValuePair<string, string>[] Values { get; set; }
+ }
+
+ [Test]
+ public void Can_Serailize_KVP_array()
+ {
+ var kvpArray = new[] {
+ new KeyValuePair<string, string>("Key", "Foo"),
+ new KeyValuePair<string, string>("Value", "Bar"),
+ };
+ var dto = new PocoWithKvp {
+ Values = kvpArray
+ };
+
+ Console.WriteLine(dto.ToJson());
+
+ Serialize(dto, includeXml: false);
+ }
+
+ [Test]
+ public void Can_deserialize_object_string()
+ {
+ JsConfig.TryToParsePrimitiveTypeValues = true;
+ JsConfig.ConvertObjectTypesIntoStringDictionary = true;
+
+ var json = "12345";
+ var deserialized = JsonSerializer.DeserializeFromString<object>(json);
+ Assert.That(deserialized, Is.EqualTo(json));
+ }
+
+ [Test]
+ public void Can_deserialize_object_array()
+ {
+ JsConfig.TryToParsePrimitiveTypeValues = true;
+ JsConfig.ConvertObjectTypesIntoStringDictionary = true;
+
+ var json = "[1,2,3]";
+ var deserialized = JsonSerializer.DeserializeFromString<object>(json);
+ Assert.That(deserialized, Is.InstanceOf<List<object>>());
+ Assert.That(((List<object>)deserialized)[0], Is.EqualTo(1));
+ Assert.That(((List<object>)deserialized)[1], Is.EqualTo(2));
+ Assert.That(((List<object>)deserialized)[2], Is.EqualTo(3));
+ }
+
+ [Test]
+ public void Can_deserialize_object_dictionary()
+ {
+ JsConfig.TryToParsePrimitiveTypeValues = true;
+ JsConfig.ConvertObjectTypesIntoStringDictionary = true;
+
+ var json = "{\"foo\":\"bar\"}";
+ var deserialized = JsonSerializer.DeserializeFromString<object>(json);
+ Assert.That(deserialized, Is.InstanceOf<Dictionary<string, object>>());
+ Assert.That(((Dictionary<string, object>)deserialized)["foo"], Is.EqualTo("bar"));
+ }
+
+ [Test]
+ public void Can_deserialize_object_dictionary_with_mixed_values_and_nulls_and_empty_array()
+ {
+ JsConfig.TryToParsePrimitiveTypeValues = true;
+ JsConfig.ConvertObjectTypesIntoStringDictionary = true;
+
+ var json = "{\"stringIntValue\": \"-13\",\"intValue\": -13,\"nullValue\": null,\"stringDecimalValue\": \"5.9\",\"decimalValue\": 5.9,\"emptyArrayValue\": [],\"stringValue\": \"Foo\",\"stringWithDigitsValue\": \"OR345\",\"dateValue\":\"\\/Date(785635200000)\\/\"}";
+ var deserialized = JsonSerializer.DeserializeFromString<object>(json);
+ Assert.That(deserialized, Is.InstanceOf<Dictionary<string, object>>());
+ var dict = (Dictionary<string, object>) deserialized;
+ Assert.That(dict["stringIntValue"], Is.EqualTo("-13"));
+ Assert.That(dict["intValue"], Is.EqualTo(-13));
+ Assert.That(dict["intValue"], Is.Not.EqualTo(dict["stringIntValue"]));
+ Assert.That(dict["nullValue"], Is.Null);
+ Assert.That(dict["stringDecimalValue"], Is.EqualTo("5.9"));
+ Assert.That(dict["decimalValue"], Is.EqualTo(5.9f));
+ Assert.That(dict["decimalValue"], Is.Not.EqualTo(dict["stringDecimalValue"]));
+ Assert.That(dict["emptyArrayValue"], Is.Not.Null);
+ Assert.That(dict["stringValue"], Is.EqualTo("Foo"));
+ Assert.That(dict["stringWithDigitsValue"], Is.EqualTo("OR345"));
+ Assert.That(dict["dateValue"], Is.EqualTo(new DateTime(1994, 11, 24, 0, 0, 0, DateTimeKind.Utc)));
+ }
+
+ [Test]
+ public void Can_deserialize_object_dictionary_with_line_breaks()
+ {
+ JsConfig.TryToParsePrimitiveTypeValues = true;
+ JsConfig.ConvertObjectTypesIntoStringDictionary = true;
+
+ var json = @"{
+ ""value""
+:
+ 5 ,
+
+ }";
+
+ var deserialized = JsonSerializer.DeserializeFromString<object>(json);
+ Assert.That(deserialized, Is.InstanceOf<Dictionary<string, object>>());
+ var dict = (Dictionary<string, object>) deserialized;
+ Assert.That(dict["value"], Is.EqualTo(5));
+ }
+
+ [Test]
+ public void Can_deserialize_object_array_with_line_breaks_before_first_element()
+ {
+ JsConfig.TryToParsePrimitiveTypeValues = true;
+ JsConfig.ConvertObjectTypesIntoStringDictionary = true;
+
+ var json = @"[
+ {
+ ""name"":""foo""
+ }]";
+
+ var deserialized = JsonSerializer.DeserializeFromString<object>(json);
+ Assert.That(deserialized, Is.InstanceOf<List<object>>());
+ var arrayValues = (List<object>) deserialized;
+ Assert.That(arrayValues.Count, Is.EqualTo(1));
+ Assert.That(arrayValues[0], Is.Not.Null);
+ }
+
+ [Test]
+ public void Can_deserialize_object_array_with_line_breaks_after_last_element()
+ {
+ JsConfig.TryToParsePrimitiveTypeValues = true;
+ JsConfig.ConvertObjectTypesIntoStringDictionary = true;
+
+ var json = @"[{
+ ""name"":""foo""
+ }
+ ]";
+
+ var deserialized = JsonSerializer.DeserializeFromString<object>(json);
+ Assert.That(deserialized, Is.InstanceOf<List<object>>());
+ var arrayValues = (List<object>) deserialized;
+ Assert.That(arrayValues.Count, Is.EqualTo(1));
+ Assert.That(arrayValues[0], Is.Not.Null);
+ }
+
+ [Test]
+ public void Can_deserialize_object_array_with_line_breaks_around_element()
+ {
+ JsConfig.TryToParsePrimitiveTypeValues = true;
+ JsConfig.ConvertObjectTypesIntoStringDictionary = true;
+
+ var json = @"[
+ {
+ ""name"":""foo""
+ }
+ ]";
+
+ var deserialized = JsonSerializer.DeserializeFromString<object>(json);
+ Assert.That(deserialized, Is.InstanceOf<List<object>>());
+ var arrayValues = (List<object>) deserialized;
+ Assert.That(arrayValues.Count, Is.EqualTo(1));
+ Assert.That(arrayValues[0], Is.Not.Null);
+ }
+
+ [Test]
+ public void Can_deserialize_object_array_with_line_breaks_around_number_element()
+ {
+ JsConfig.TryToParsePrimitiveTypeValues = true;
+ JsConfig.ConvertObjectTypesIntoStringDictionary = true;
+
+ var json = @"[
+
+ 5
+
+ ]";
+
+ var deserialized = JsonSerializer.DeserializeFromString<object>(json);
+ Assert.That(deserialized, Is.InstanceOf<List<object>>());
+ var arrayValues = (List<object>) deserialized;
+ Assert.That(arrayValues.Count, Is.EqualTo(1));
+ Assert.That(arrayValues[0], Is.EqualTo(5));
+ }
+ }
}
View
236 tests/ServiceStack.Text.Tests/GenericCollectionTests.cs
@@ -1,119 +1,119 @@
-using System;
-using System.Collections;
-using System.Collections.Generic;
-using NUnit.Framework;
-using ServiceStack.Common.Tests.Models;
-
-namespace ServiceStack.Text.Tests
-{
- [TestFixture]
- public class GenericCollectionTests
- : TestBase
- {
-
- [Test]
- public void Can_serialize_Queue_string()
- {
- var queue = new Queue<string>();
-
- queue.Enqueue("one");
- queue.Enqueue("two");
- queue.Enqueue("three");
-
- Serialize(queue);
-
- Assert.That(CsvSerializer.SerializeToString(queue), Is.EqualTo("one,two,three" + Environment.NewLine));
- }
-
- [Test]
- public void Can_serialize_Queue_int()
- {
- var queue = new Queue<int>();
-
- queue.Enqueue(1);
- queue.Enqueue(2);
- queue.Enqueue(3);
-
- Serialize(queue);
-
- Assert.That(CsvSerializer.SerializeToString(queue), Is.EqualTo("1,2,3" + Environment.NewLine));
- }
-
- [Test]
- public void Can_serialize_Queue_Generic()
- {
- var queue = new Queue<ModelWithIdAndName>();
-
- queue.Enqueue(ModelWithIdAndName.Create(1));
- queue.Enqueue(ModelWithIdAndName.Create(2));
- queue.Enqueue(ModelWithIdAndName.Create(3));
-
- Serialize(queue);
-
- Assert.That(CsvSerializer.SerializeToString(queue),
- Is.EqualTo(
- "Id,Name"
- + Environment.NewLine
- + "1,Name1"
- + Environment.NewLine
- + "2,Name2"
- + Environment.NewLine
- + "3,Name3"
- + Environment.NewLine
- ));
- }
-
- [Test]
- public void Can_serialize_Stack_string()
- {
- var stack = new Stack<string>();
-
- stack.Push("one");
- stack.Push("two");
- stack.Push("three");
-
- Serialize(stack);
-
- Assert.That(CsvSerializer.SerializeToString(stack), Is.EqualTo("three,two,one" + Environment.NewLine));
- }
-
- [Test]
- public void Can_serialize_Stack_int()
- {
- var stack = new Stack<int>();
-
- stack.Push(1);
- stack.Push(2);
- stack.Push(3);
-
- Serialize(stack);
-
- Assert.That(CsvSerializer.SerializeToString(stack), Is.EqualTo("3,2,1" + Environment.NewLine));
- }
-
- [Test]
- public void Can_serialize_Stack_Generic()
- {
- var stack = new Stack<ModelWithIdAndName>();
-
- stack.Push(ModelWithIdAndName.Create(1));
- stack.Push(ModelWithIdAndName.Create(2));
- stack.Push(ModelWithIdAndName.Create(3));
-
- Serialize(stack);
-
- Assert.That(CsvSerializer.SerializeToString(stack),
- Is.EqualTo(
- "Id,Name"
- + Environment.NewLine
- + "3,Name3"
- + Environment.NewLine
- + "2,Name2"
- + Environment.NewLine
- + "1,Name1"
- + Environment.NewLine
- ));
- }
- }
-
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using NUnit.Framework;
+using ServiceStack.Common.Tests.Models;
+
+namespace ServiceStack.Text.Tests
+{
+ [TestFixture]
+ public class GenericCollectionTests
+ : TestBase
+ {
+
+ [Test]
+ public void Can_serialize_Queue_string()
+ {
+ var queue = new Queue<string>();
+
+ queue.Enqueue("one");
+ queue.Enqueue("two");
+ queue.Enqueue("three");
+
+ Serialize(queue);
+
+ Assert.That(CsvSerializer.SerializeToString(queue), Is.EqualTo("one,two,three" + Environment.NewLine));
+ }
+
+ [Test]
+ public void Can_serialize_Queue_int()
+ {
+ var queue = new Queue<int>();
+
+ queue.Enqueue(1);
+ queue.Enqueue(2);
+ queue.Enqueue(3);
+
+ Serialize(queue);
+
+ Assert.That(CsvSerializer.SerializeToString(queue), Is.EqualTo("1,2,3" + Environment.NewLine));
+ }
+
+ [Test]
+ public void Can_serialize_Queue_Generic()
+ {
+ var queue = new Queue<ModelWithIdAndName>();
+
+ queue.Enqueue(ModelWithIdAndName.Create(1));
+ queue.Enqueue(ModelWithIdAndName.Create(2));
+ queue.Enqueue(ModelWithIdAndName.Create(3));
+
+ Serialize(queue);
+
+ Assert.That(CsvSerializer.SerializeToString(queue),
+ Is.EqualTo(
+ "Id,Name"
+ + Environment.NewLine
+ + "1,Name1"
+ + Environment.NewLine
+ + "2,Name2"
+ + Environment.NewLine
+ + "3,Name3"
+ + Environment.NewLine
+ ));
+ }
+
+ [Test]
+ public void Can_serialize_Stack_string()
+ {
+ var stack = new Stack<string>();
+
+ stack.Push("one");
+ stack.Push("two");
+ stack.Push("three");
+
+ Serialize(stack);
+
+ Assert.That(CsvSerializer.SerializeToString(stack), Is.EqualTo("three,two,one" + Environment.NewLine));
+ }
+
+ [Test]
+ public void Can_serialize_Stack_int()
+ {
+ var stack = new Stack<int>();
+
+ stack.Push(1);
+ stack.Push(2);
+ stack.Push(3);
+
+ Serialize(stack);
+
+ Assert.That(CsvSerializer.SerializeToString(stack), Is.EqualTo("3,2,1" + Environment.NewLine));
+ }
+
+ [Test]
+ public void Can_serialize_Stack_Generic()
+ {
+ var stack = new Stack<ModelWithIdAndName>();
+
+ stack.Push(ModelWithIdAndName.Create(1));
+ stack.Push(ModelWithIdAndName.Create(2));
+ stack.Push(ModelWithIdAndName.Create(3));
+
+ Serialize(stack);
+
+ Assert.That(CsvSerializer.SerializeToString(stack),
+ Is.EqualTo(
+ "Id,Name"
+ + Environment.NewLine
+ + "3,Name3"
+ + Environment.NewLine
+ + "2,Name2"
+ + Environment.NewLine
+ + "1,Name1"
+ + Environment.NewLine
+ ));
+ }
+ }
+
}
View
502 tests/ServiceStack.Text.Tests/InterfaceTests.cs
@@ -1,252 +1,252 @@
-using System;
-using System.Collections;
-using System.Collections.Generic;
-using NUnit.Framework;
-using ServiceStack.Messaging;
-using ServiceStack.ServiceInterface.Auth;
-using ServiceStack.Text.Tests.JsonTests;
-
-namespace ServiceStack.Text.Tests
-{
- [TestFixture]
- public class InterfaceTests : TestBase
- {
- [Test]
- public void Can_serialize_Message()
- {
- var message = new Message<string> { Id = new Guid(), CreatedDate = new DateTime(), Body = "test" };
- var messageString = TypeSerializer.SerializeToString(message);
-
- Assert.That(messageString, Is.EqualTo(
- "{Id:00000000000000000000000000000000,CreatedDate:0001-01-01,Priority:0,RetryAttempts:0,Body:test}"));
-
- Serialize(message);
- }
-
- [Test]
- public void Can_serialize_IMessage()
- {
- var message = new Message<string> { Id = new Guid(), CreatedDate = new DateTime(), Body = "test" };
- var messageString = TypeSerializer.SerializeToString((IMessage<string>)message);
-
- Assert.That(messageString, Is.EqualTo(
- "{__type:\"ServiceStack.Messaging.Message`1[[System.String, mscorlib]], ServiceStack.Interfaces\","
- + "Id:00000000000000000000000000000000,CreatedDate:0001-01-01,Priority:0,RetryAttempts:0,Body:test}"));
- }
-
- public class DtoWithObject
- {
- public object Results { get; set; }
- }
-
- [Test]
- public void Can_deserialize_dto_with_object()
- {
- var dto = Serialize(new DtoWithObject { Results = new Message<string>("Body") }, includeXml: false);
- Assert.That(dto.Results, Is.Not.Null);
- Assert.That(dto.Results.GetType(), Is.EqualTo(typeof(Message<string>)));
- }
-
- [Test]
- public void Can_serialize_ToString()
- {
- var type = Type.GetType(typeof(Message<string>).AssemblyQualifiedName);
- Assert.That(type, Is.Not.Null);
-
- type = AssemblyUtils.FindType(typeof(Message<string>).AssemblyQualifiedName);
- Assert.That(type, Is.Not.Null);
-
- type = Type.GetType("ServiceStack.Messaging.Message`1[[System.String, mscorlib]], ServiceStack.Interfaces");
- Assert.That(type, Is.Not.Null);
- }
-
- [Test, TestCaseSource(typeof(InterfaceTests), "EndpointExpectations")]
- public void Does_serialize_minimum_type_info_whilst_still_working(
- Type type, string expectedTypeString)
- {
- Assert.That(type.ToTypeString(), Is.EqualTo(expectedTypeString));
- var newType = AssemblyUtils.FindType(type.ToTypeString());
- Assert.That(newType, Is.Not.Null);
- Assert.That(newType, Is.EqualTo(type));
- }
-
- public static IEnumerable EndpointExpectations
- {
- get
- {
- yield return new TestCaseData(typeof(Message<string>),
- "ServiceStack.Messaging.Message`1[[System.String, mscorlib]], ServiceStack.Interfaces");
-
- yield return new TestCaseData(typeof(Cat),
- "ServiceStack.Text.Tests.JsonTests.Cat, ServiceStack.Text.Tests");
-
- yield return new TestCaseData(typeof(Zoo),
- "ServiceStack.Text.Tests.JsonTests.Zoo, ServiceStack.Text.Tests");
- }
- }
-
- [Test]
- public void Can_deserialize_interface_into_concrete_type()
- {
- var dto = Serialize(new MessagingTests.DtoWithInterface { Results = new Message<string>("Body") }, includeXml: false);
- Assert.That(dto.Results, Is.Not.Null);
- }
-
- public class UserSession
- {
- public UserSession()
- {
- this.ProviderOAuthAccess = new Dictionary<string, IOAuthTokens>();
- }
-
- public string ReferrerUrl { get; set; }
-
- public string Id { get; set; }
-
- public string TwitterUserId { get; set; }
-
- public string TwitterScreenName { get; set; }
-
- public string RequestTokenSecret { get; set; }
-
- public DateTime CreatedAt { get; set; }
-
- public DateTime LastModified { get; set; }
-
- public Dictionary<string, IOAuthTokens> ProviderOAuthAccess { get; set; }
- }
-
- [Test]
- public void Can_Serialize_User_OAuthSession_map()
- {
- var userSession = new UserSession {
- Id = "1",
- CreatedAt = DateTime.UtcNow,
- LastModified = DateTime.UtcNow,
- ReferrerUrl = "http://referrer.com",
- ProviderOAuthAccess = new Dictionary<string, IOAuthTokens>
- {
- {"twitter", new OAuthTokens { Provider = "twitter", AccessToken = "TAccessToken", Items = { {"a","1"}, {"b","2"}, }} },
- {"facebook", new OAuthTokens { Provider = "facebook", AccessToken = "FAccessToken", Items = { {"a","1"}, {"b","2"}, }} },
- }
- };
-
- var fromDto = Serialize(userSession, includeXml: false);
- Console.WriteLine(fromDto.Dump());
-
- Assert.That(fromDto.ProviderOAuthAccess.Count, Is.EqualTo(2));
- Assert.That(fromDto.ProviderOAuthAccess["twitter"].Provider, Is.EqualTo("twitter"));
- Assert.That(fromDto.ProviderOAuthAccess["facebook"].Provider, Is.EqualTo("facebook"));
- Assert.That(fromDto.ProviderOAuthAccess["twitter"].Items.Count, Is.EqualTo(2));
- Assert.That(fromDto.ProviderOAuthAccess["facebook"].Items.Count, Is.EqualTo(2));
- }
-
- [Test]
- public void Can_Serialize_User_OAuthSession_list()
- {
- var userSession = new OAuthUserSession {
- Id = "1",
- CreatedAt = DateTime.UtcNow,
- LastModified = DateTime.UtcNow,
- ReferrerUrl = "http://referrer.com",
- ProviderOAuthAccess = new List<IOAuthTokens>
- {
- new OAuthTokens { Provider = "twitter", AccessToken = "TAccessToken", Items = { {"a","1"}, {"b","2"}, }},
- new OAuthTokens { Provider = "facebook", AccessToken = "FAccessToken", Items = { {"a","1"}, {"b","2"}, }},
- }
- };
-
- var fromDto = Serialize(userSession, includeXml: false);
- Console.WriteLine(fromDto.Dump());
-
- Assert.That(fromDto.ProviderOAuthAccess.Count, Is.EqualTo(2));
- Assert.That(fromDto.ProviderOAuthAccess[0].Provider, Is.EqualTo("twitter"));
- Assert.That(fromDto.ProviderOAuthAccess[1].Provider, Is.EqualTo("facebook"));
- Assert.That(fromDto.ProviderOAuthAccess[0].Items.Count, Is.EqualTo(2));
- Assert.That(fromDto.ProviderOAuthAccess[1].Items.Count, Is.EqualTo(2));
- }
-
- [Test]
- public void Doesnt_serialize_TypeInfo_when_set()
- {
- try {
- JsConfig.ExcludeTypeInfo = true;
- var userSession = new OAuthUserSession {
- Id = "1",
- CreatedAt = DateTime.UtcNow,
- LastModified = DateTime.UtcNow,
- ReferrerUrl = "http://referrer.com",
- ProviderOAuthAccess = new List<IOAuthTokens>
- {
- new OAuthTokens { Provider = "twitter", AccessToken = "TAccessToken", Items = { {"a","1"}, {"b","2"}, }},
- new OAuthTokens { Provider = "facebook", AccessToken = "FAccessToken", Items = { {"a","1"}, {"b","2"}, }},
- }
- };
-
- Assert.That(userSession.ToJson().IndexOf("__type") == -1, Is.True);
- Assert.That(userSession.ToJsv().IndexOf("__type") == -1, Is.True);
- } finally {
- JsConfig.Reset();
- }
- }
-
- public class AggregateEvents
- {
- public Guid Id { get; set; }
- public List<DomainEvent> Events { get; set; }
- }
-
- public abstract class DomainEvent { }
-
- public class UserRegisteredEvent : DomainEvent
- {
- public Guid UserId { get; set; }
- public string Name { get; set; }
- }
-
- public class UserPromotedEvent : DomainEvent
- {
- public Guid UserId { get; set; }
- public string NewRole { get; set; }
- }
-
-
- [Test]
- public void Can_deserialize_DomainEvent_into_Concrete_Type()
- {
- var userId = Guid.NewGuid();
- var dto = (DomainEvent)new UserPromotedEvent { UserId = userId };
- var json = dto.ToJson();
- var userPromoEvent = (UserPromotedEvent)json.FromJson<DomainEvent>();
- Assert.That(userPromoEvent.UserId, Is.EqualTo(userId));
- }
-
- public class Habitat<T> where T : Animal
- {
- public string Continent { get; set; }
-
- public object Species { get; set; }
- }
-
- public class Animal
- {
- public string Name { get; set; }
- }
-
- public class CatAnimal : Animal
- {
- public string Color { get; set; }
- }
-
- [Test]
- public void Can_serialize_dependent_type_properties()
- {
- var jungle = new Habitat<Animal> {
- Continent = "South America",
- Species = new CatAnimal { Name = "Tiger", Color = "Orange" }
- };
-
- Console.WriteLine(jungle.ToJson());
- }
- }
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using NUnit.Framework;
+using ServiceStack.Messaging;
+using ServiceStack.ServiceInterface.Auth;
+using ServiceStack.Text.Tests.JsonTests;
+
+namespace ServiceStack.Text.Tests
+{
+ [TestFixture]
+ public class InterfaceTests : TestBase
+ {
+ [Test]
+ public void Can_serialize_Message()
+ {
+ var message = new Message<string> { Id = new Guid(), CreatedDate = new DateTime(), Body = "test" };
+ var messageString = TypeSerializer.SerializeToString(message);
+
+ Assert.That(messageString, Is.EqualTo(
+ "{Id:00000000000000000000000000000000,CreatedDate:0001-01-01,Priority:0,RetryAttempts:0,Body:test}"));
+
+ Serialize(message);
+ }
+
+ [Test]
+ public void Can_serialize_IMessage()
+ {
+ var message = new Message<string> { Id = new Guid(), CreatedDate = new DateTime(), Body = "test" };
+ var messageString = TypeSerializer.SerializeToString((IMessage<string>)message);
+
+ Assert.That(messageString, Is.EqualTo(
+ "{__type:\"ServiceStack.Messaging.Message`1[[System.String, mscorlib]], ServiceStack.Interfaces\","
+ + "Id:00000000000000000000000000000000,CreatedDate:0001-01-01,Priority:0,RetryAttempts:0,Body:test}"));
+ }
+
+ public class DtoWithObject
+ {
+ public object Results { get; set; }
+ }
+
+ [Test]
+ public void Can_deserialize_dto_with_object()
+ {
+ var dto = Serialize(new DtoWithObject { Results = new Message<string>("Body") }, includeXml: false);
+ Assert.That(dto.Results, Is.Not.Null);
+ Assert.That(dto.Results.GetType(), Is.EqualTo(typeof(Message<string>)));
+ }
+
+ [Test]
+ public void Can_serialize_ToString()
+ {
+ var type = Type.GetType(typeof(Message<string>).AssemblyQualifiedName);
+ Assert.That(type, Is.Not.Null);
+
+ type = AssemblyUtils.FindType(typeof(Message<string>).AssemblyQualifiedName);
+ Assert.That(type, Is.Not.Null);
+
+ type = Type.GetType("ServiceStack.Messaging.Message`1[[System.String, mscorlib]], ServiceStack.Interfaces");
+ Assert.That(type, Is.Not.Null);
+ }
+
+ [Test, TestCaseSource(typeof(InterfaceTests), "EndpointExpectations")]
+ public void Does_serialize_minimum_type_info_whilst_still_working(
+ Type type, string expectedTypeString)
+ {
+ Assert.That(type.ToTypeString(), Is.EqualTo(expectedTypeString));
+ var newType = AssemblyUtils.FindType(type.ToTypeString());
+ Assert.That(newType, Is.Not.Null);
+ Assert.That(newType, Is.EqualTo(type));
+ }
+
+ public static IEnumerable EndpointExpectations
+ {
+ get
+ {
+ yield return new TestCaseData(typeof(Message<string>),
+ "ServiceStack.Messaging.Message`1[[System.String, mscorlib]], ServiceStack.Interfaces");
+
+ yield return new TestCaseData(typeof(Cat),
+ "ServiceStack.Text.Tests.JsonTests.Cat, ServiceStack.Text.Tests");
+
+ yield return new TestCaseData(typeof(Zoo),
+ "ServiceStack.Text.Tests.JsonTests.Zoo, ServiceStack.Text.Tests");
+ }
+ }
+
+ [Test]
+ public void Can_deserialize_interface_into_concrete_type()
+ {
+ var dto = Serialize(new MessagingTests.DtoWithInterface { Results = new Message<string>("Body") }, includeXml: false);
+ Assert.That(dto.Results, Is.Not.Null);
+ }
+
+ public class UserSession
+ {
+ public UserSession()
+ {
+ this.ProviderOAuthAccess = new Dictionary<string, IOAuthTokens>();
+ }
+
+ public string ReferrerUrl { get; set; }
+
+ public string Id { get; set; }
+
+ public string TwitterUserId { get; set; }
+
+ public string TwitterScreenName { get; set; }
+
+ public string RequestTokenSecret { get; set; }
+
+ public DateTime CreatedAt { get; set; }
+
+ public DateTime LastModified { get; set; }
+
+ public Dictionary<string, IOAuthTokens> ProviderOAuthAccess { get; set; }
+ }
+
+ [Test]
+ public void Can_Serialize_User_OAuthSession_map()
+ {
+ var userSession = new UserSession {
+ Id = "1",
+ CreatedAt = DateTime.UtcNow,
+ LastModified = DateTime.UtcNow,
+ ReferrerUrl = "http://referrer.com",
+ ProviderOAuthAccess = new Dictionary<string, IOAuthTokens>
+ {
+ {"twitter", new OAuthTokens { Provider = "twitter", AccessToken = "TAccessToken", Items = { {"a","1"}, {"b","2"}, }} },
+ {"facebook", new OAuthTokens { Provider = "facebook", AccessToken = "FAccessToken", Items = { {"a","1"}, {"b","2"}, }} },
+ }
+ };
+
+ var fromDto = Serialize(userSession, includeXml: false);
+ Console.WriteLine(fromDto.Dump());
+
+ Assert.That(fromDto.ProviderOAuthAccess.Count, Is.EqualTo(2));
+ Assert.That(fromDto.ProviderOAuthAccess["twitter"].Provider, Is.EqualTo("twitter"));
+ Assert.That(fromDto.ProviderOAuthAccess["facebook"].Provider, Is.EqualTo("facebook"));
+ Assert.That(fromDto.ProviderOAuthAccess["twitter"].Items.Count, Is.EqualTo(2));
+ Assert.That(fromDto.ProviderOAuthAccess["facebook"].Items.Count, Is.EqualTo(2));
+ }
+
+ [Test]
+ public void Can_Serialize_User_OAuthSession_list()
+ {
+ var userSession = new OAuthUserSession {
+ Id = "1",
+ CreatedAt = DateTime.UtcNow,
+ LastModified = DateTime.UtcNow,
+ ReferrerUrl = "http://referrer.com",
+ ProviderOAuthAccess = new List<IOAuthTokens>
+ {
+ new OAuthTokens { Provider = "twitter", AccessToken = "TAccessToken", Items = { {"a","1"}, {"b","2"}, }},
+ new OAuthTokens { Provider = "facebook", AccessToken = "FAccessToken", Items = { {"a","1"}, {"b","2"}, }},
+ }
+ };
+
+ var fromDto = Serialize(userSession, includeXml: false);
+ Console.WriteLine(fromDto.Dump());
+
+ Assert.That(fromDto.ProviderOAuthAccess.Count, Is.EqualTo(2));
+ Assert.That(fromDto.ProviderOAuthAccess[0].Provider, Is.EqualTo("twitter"));
+ Assert.That(fromDto.ProviderOAuthAccess[1].Provider, Is.EqualTo("facebook"));
+ Assert.That(fromDto.ProviderOAuthAccess[0].Items.Count, Is.EqualTo(2));
+ Assert.That(fromDto.ProviderOAuthAccess[1].Items.Count, Is.EqualTo(2));
+ }
+
+ [Test]
+ public void Doesnt_serialize_TypeInfo_when_set()
+ {
+ try {
+ JsConfig.ExcludeTypeInfo = true;
+ var userSession = new OAuthUserSession {
+ Id = "1",
+ CreatedAt = DateTime.UtcNow,
+ LastModified = DateTime.UtcNow,
+ ReferrerUrl = "http://referrer.com",
+ ProviderOAuthAccess = new List<IOAuthTokens>
+ {
+ new OAuthTokens { Provider = "twitter", AccessToken = "TAccessToken", Items = { {"a","1"}, {"b","2"}, }},
+ new OAuthTokens { Provider = "facebook", AccessToken = "FAccessToken", Items = { {"a","1"}, {"b","2"}, }},
+ }
+ };
+
+ Assert.That(userSession.ToJson().IndexOf("__type") == -1, Is.True);
+ Assert.That(userSession.ToJsv().IndexOf("__type") == -1, Is.True);
+ } finally {
+ JsConfig.Reset();
+ }
+ }
+
+ public class AggregateEvents
+ {
+ public Guid Id { get; set; }
+ public List<DomainEvent> Events { get; set; }
+ }
+
+ public abstract class DomainEvent { }
+
+ public class UserRegisteredEvent : DomainEvent
+ {
+ public Guid UserId { get; set; }
+ public string Name { get; set; }
+ }
+
+ public class UserPromotedEvent : DomainEvent
+ {
+ public Guid UserId { get; set; }
+ public string NewRole { get; set; }
+ }
+
+
+ [Test]
+ public void Can_deserialize_DomainEvent_into_Concrete_Type()
+ {
+ var userId = Guid.NewGuid();
+ var dto = (DomainEvent)new UserPromotedEvent { UserId = userId };
+ var json = dto.ToJson();
+ var userPromoEvent = (UserPromotedEvent)json.FromJson<DomainEvent>();
+ Assert.That(userPromoEvent.UserId, Is.EqualTo(userId));
+ }
+
+ public class Habitat<T> where T : Animal
+ {
+ public string Continent { get; set; }
+
+ public object Species { get; set; }
+ }
+
+ public class Animal
+ {
+ public string Name { get; set; }
+ }
+
+ public class CatAnimal : Animal
+ {
+ public string Color { get; set; }
+ }
+
+ [Test]
+ public void Can_serialize_dependent_type_properties()
+ {
+ var jungle = new Habitat<Animal> {
+ Continent = "South America",
+ Species = new CatAnimal { Name = "Tiger", Color = "Orange" }
+ };
+
+ Console.WriteLine(jungle.ToJson());
+ }
+ }
}
View
74 tests/ServiceStack.Text.Tests/JsonTests/AnonymousDeserializationTests.cs
@@ -1,38 +1,38 @@
-using NUnit.Framework;
-
-namespace ServiceStack.Text.Tests.JsonTests
-{
- [TestFixture]
- public class AnonymousDeserializationTests
- : TestBase
- {
- private class Item
- {
- public int IntValue { get; set; }
- public string StringValue { get; set; }
-
- public static Item Create()
- {
- return new Item { IntValue = 42, StringValue = "Foo" };
- }
- }
-
- [Test]
- public void Can_deserialize_to_anonymous_type()
- {
- var original = Item.Create();
- var json = JsonSerializer.SerializeToString(original);
-
- var item = DeserializeAnonymousType(new { IntValue = default(int), StringValue = default(string) }, json);
-
- Assert.That(item.IntValue, Is.EqualTo(42));
- Assert.That(item.StringValue, Is.EqualTo("Foo"));
- }
-
- private static T DeserializeAnonymousType<T>(T template, string json) where T : class
- {
- TypeConfig<T>.EnableAnonymousFieldSetters = true;
- return JsonSerializer.DeserializeFromString(json, template.GetType()) as T;
- }
- }
+using NUnit.Framework;
+
+namespace ServiceStack.Text.Tests.JsonTests
+{
+ [TestFixture]
+ public class AnonymousDeserializationTests
+ : TestBase
+ {
+ private class Item
+ {
+ public int IntValue { get; set; }
+ public string StringValue { get; set; }
+
+ public static Item Create()
+ {
+ return new Item { IntValue = 42, StringValue = "Foo" };
+ }
+ }
+
+ [Test]
+ public void Can_deserialize_to_anonymous_type()
+ {
+ var original = Item.Create();
+ var json = JsonSerializer.SerializeToString(original);
+
+ var item = DeserializeAnonymousType(new { IntValue = default(int), StringValue = default(string) }, json);
+
+ Assert.That(item.IntValue, Is.EqualTo(42));
+ Assert.That(item.StringValue, Is.EqualTo("Foo"));
+ }
+
+ private static T DeserializeAnonymousType<T>(T template, string json) where T : class
+ {
+ TypeConfig<T>.EnableAnonymousFieldSetters = true;
+ return JsonSerializer.DeserializeFromString(json, template.GetType()) as T;
+ }
+ }
}
View
556 tests/ServiceStack.Text.Tests/JsonTests/BasicJsonTests.cs
@@ -1,79 +1,79 @@
-using System;
-using System.Collections.Generic;
-using System.Runtime.Serialization;
-using NUnit.Framework;
-using ServiceStack.Common.Tests.Models;
-
-namespace ServiceStack.Text.Tests.JsonTests
-{
- [TestFixture]
- public class BasicJsonTests
- : TestBase
- {
- public class JsonPrimitives
- {
- public int Int { get; set; }
- public long Long { get; set; }
- public float Float { get; set; }
- public double Double { get; set; }
- public bool Boolean { get; set; }
- public DateTime DateTime { get; set; }
- public string NullString { get; set; }
-
- public static JsonPrimitives Create(int i)
- {
- return new JsonPrimitives
- {
- Int = i,
- Long = i,
- Float = i,
- Double = i,
- Boolean = i % 2 == 0,
- DateTime = DateTimeExtensions.FromUnixTimeMs(1),
- };
- }
- }
-
- public class NullableValueTypes
- {
- public int? Int { get; set; }
- public long? Long { get; set; }
- public decimal? Decimal { get; set; }
- public double? Double { get; set; }
- public bool? Boolean { get; set; }
- public DateTime? DateTime { get; set; }
- }
-
- [Test]
- public void Can_parse_json_with_nullable_valuetypes()
- {
- var json = "{}";
-
- var item = JsonSerializer.DeserializeFromString<NullableValueTypes>(json);
-
- Assert.That(item.Int, Is.Null, "int");
- Assert.That(item.Long, Is.Null, "long");
- Assert.That(item.Decimal, Is.Null, "decimal");
- Assert.That(item.Double, Is.Null, "double");
- Assert.That(item.Boolean, Is.Null, "boolean");
- Assert.That(item.DateTime, Is.Null, "datetime");
- }
-
- [Test]
- public void Can_parse_json_with_nullable_valuetypes_that_has_included_null_values()
- {
- var json = "{\"Int\":null,\"Long\":null,\"Decimal\":null,\"Double\":null,\"Boolean\":null,\"DateTime\":null}";
-
- var item = JsonSerializer.DeserializeFromString<NullableValueTypes>(json);
-
- Assert.That(item.Int, Is.Null, "int");
- Assert.That(item.Long, Is.Null, "long");
- Assert.That(item.Decimal, Is.Null, "decimal");
- Assert.That(item.Double, Is.Null, "double");
- Assert.That(item.Boolean, Is.Null, "boolean");
- Assert.That(item.DateTime, Is.Null, "datetime");
- }
-
+using System;
+using System.Collections.Generic;
+using System.Runtime.Serialization;
+using NUnit.Framework;
+using ServiceStack.Common.Tests.Models;
+
+namespace ServiceStack.Text.Tests.JsonTests
+{
+ [TestFixture]
+ public class BasicJsonTests
+ : TestBase
+ {
+ public class JsonPrimitives
+ {
+ public int Int { get; set; }
+ public long Long { get; set; }
+ public float Float { get; set; }
+ public double Double { get; set; }
+ public bool Boolean { get; set; }
+ public DateTime DateTime { get; set; }
+ public string NullString { get; set; }
+
+ public static JsonPrimitives Create(int i)
+ {
+ return new JsonPrimitives
+ {
+ Int = i,
+ Long = i,
+ Float = i,
+ Double = i,
+ Boolean = i % 2 == 0,
+ DateTime = DateTimeExtensions.FromUnixTimeMs(1),
+ };
+ }
+ }
+
+ public class NullableValueTypes
+ {
+ public int? Int { get; set; }
+ public long? Long { get; set; }
+ public decimal? Decimal { get; set; }
+ public double? Double { get; set; }
+ public bool? Boolean { get; set; }
+ public DateTime? DateTime { get; set; }
+ }
+
+ [Test]
+ public void Can_parse_json_with_nullable_valuetypes()
+ {
+ var json = "{}";
+
+ var item = JsonSerializer.DeserializeFromString<NullableValueTypes>(json);
+
+ Assert.That(item.Int, Is.Null, "int");
+ Assert.That(item.Long, Is.Null, "long");
+ Assert.That(item.Decimal, Is.Null, "decimal");
+ Assert.That(item.Double, Is.Null, "double");
+ Assert.That(item.Boolean, Is.Null, "boolean");
+ Assert.That(item.DateTime, Is.Null, "datetime");
+ }
+
+ [Test]
+ public void Can_parse_json_with_nullable_valuetypes_that_has_included_null_values()
+ {
+ var json = "{\"Int\":null,\"Long\":null,\"Decimal\":null,\"Double\":null,\"Boolean\":null,\"DateTime\":null}";
+
+ var item = JsonSerializer.DeserializeFromString<NullableValueTypes>(json);
+
+ Assert.That(item.Int, Is.Null, "int");
+ Assert.That(item.Long, Is.Null, "long");
+ Assert.That(item.Decimal, Is.Null, "decimal");
+ Assert.That(item.Double, Is.Null, "double");
+ Assert.That(item.Boolean, Is.Null, "boolean");
+ Assert.That(item.DateTime, Is.Null, "datetime");
+ }
+
[Test]
public void Can_parse_json_with_nulls_or_empty_string_in_nullables()
{
@@ -82,205 +82,205 @@ public void Can_parse_json_with_nulls_or_empty_string_in_nullables()
Assert.That(value.Int, Is.EqualTo(null));
Assert.That(value.Boolean, Is.EqualTo(null));
- }
-
- [Test]
- public void Can_parse_json_with_nullable_valuetypes_that_has_no_value_specified()
- {
- var json = "{\"Int\":,\"Long\":,\"Decimal\":,\"Double\":,\"Boolean\":,\"DateTime\":}";
-
- var item = JsonSerializer.DeserializeFromString<NullableValueTypes>(json);
-
- Assert.That(item.Int, Is.Null, "int");
- Assert.That(item.Long, Is.Null, "long");
- Assert.That(item.Decimal, Is.Null, "decimal");
- Assert.That(item.Double, Is.Null, "double");
- Assert.That(item.Boolean, Is.Null, "boolean");
- Assert.That(item.DateTime, Is.Null, "datetime");
- }
-
- [Test]
- public void Can_handle_json_primitives()
- {
- var json = JsonSerializer.SerializeToString(JsonPrimitives.Create(1));
- Log(json);
-
- Assert.That(json, Is.EqualTo(
- "{\"Int\":1,\"Long\":1,\"Float\":1,\"Double\":1,\"Boolean\":false,\"DateTime\":\"\\/Date(1)\\/\"}"));
- }
-
- [Test]
- public void Can_parse_json_with_nulls()
- {
- const string json = "{\"Int\":1,\"NullString\":null}";
- var value = JsonSerializer.DeserializeFromString<JsonPrimitives>(json);
-
- Assert.That(value.Int, Is.EqualTo(1));
- Assert.That(value.NullString, Is.Null);
- }
-
- [Test]
- public void Can_serialize_dictionary_of_int_int()
- {
- var json = JsonSerializer.SerializeToString<IntIntDictionary>(new IntIntDictionary() { Dictionary = { { 10, 100 }, { 20, 200 } } });
- const string expected = "{\"Dictionary\":{\"10\":100,\"20\":200}}";
- Assert.That(json, Is.EqualTo(expected));
- }
-
- private class IntIntDictionary
- {
- public IntIntDictionary()
- {
- Dictionary = new Dictionary<int, int>();
- }
- public IDictionary<int, int> Dictionary { get; set; }
- }
-
- [Test]
- public void Serialize_skips_null_values_by_default()
- {
- var o = new NullValueTester
- {
- Name = "Brandon",
- Type = "Programmer",
- SampleKey = 12,
- Nothing = (string)null,
- NullableDateTime = null
- };
-
- var s = JsonSerializer.SerializeToString(o);
- Assert.That(s, Is.EqualTo("{\"Name\":\"Brandon\",\"Type\":\"Programmer\",\"SampleKey\":12}"));
- }
-
- [Test]
- public void Serialize_can_include_null_values()
- {
- var o = new NullValueTester
- {
- Name = "Brandon",
- Type = "Programmer",
- SampleKey = 12,
- Nothing = null,
- NullClass = null,
- NullableDateTime = null,
- };
-
- JsConfig.IncludeNullValues = true;
- var s = JsonSerializer.SerializeToString(o);
- JsConfig.Reset();
- Assert.That(s, Is.EqualTo("{\"Name\":\"Brandon\",\"Type\":\"Programmer\",\"SampleKey\":12,\"Nothing\":null,\"NullClass\":null,\"NullableDateTime\":null}"));
- }
-
- private class NullClass
- {
-
- }
-
- [Test]
- public void Deserialize_sets_null_values()
- {
- var s = "{\"Name\":\"Brandon\",\"Type\":\"Programmer\",\"SampleKey\":12,\"Nothing\":null}";
- var o = JsonSerializer.DeserializeFromString<NullValueTester>(s);
- Assert.That(o.Name, Is.EqualTo("Brandon"));
- Assert.That(o.Type, Is.EqualTo("Programmer"));
- Assert.That(o.SampleKey, Is.EqualTo(12));
- Assert.That(o.Nothing, Is.Null);
- }
-
- [Test]
- public void Deserialize_ignores_omitted_values()
- {
- var s = "{\"Type\":\"Programmer\",\"SampleKey\":2}";
- var o = JsonSerializer.DeserializeFromString<NullValueTester>(s);
- Assert.That(o.Name, Is.EqualTo("Miguel"));
- Assert.That(o.Type, Is.EqualTo("Programmer"));
- Assert.That(o.SampleKey, Is.EqualTo(2));
- Assert.That(o.Nothing, Is.EqualTo("zilch"));
- }
-
- private class NullValueTester
- {
- public string Name
- {
- get;
- set;
- }
-
- public string Type
- {
- get;
- set;
- }
-
- public int SampleKey
- {
- get;
- set;
- }
-
- public string Nothing
- {
- get;
- set;
- }
-
- public NullClass NullClass { get; set; }
-
- public DateTime? NullableDateTime { get; set; }
-
- public NullValueTester()
- {
- Name = "Miguel";
- Type = "User";
- SampleKey = 1;
- Nothing = "zilch";
- NullableDateTime = new DateTime(2012, 01, 01);
- }
- }
-
- [DataContract]
- class Person
- {
- [DataMember(Name = "MyID")]
- public int Id { get; set; }
- [DataMember]
- public string Name { get; set; }
- }
-
- [Test]
- public void Can_override_name()
- {
- var person = new Person
- {
- Id = 123,
- Name = "Abc"
- };
-
- Assert.That(TypeSerializer.SerializeToString(person), Is.EqualTo("{MyID:123,Name:Abc}"));
- Assert.That(JsonSerializer.SerializeToString(person), Is.EqualTo("{\"MyID\":123,\"Name\":\"Abc\"}"));
- }
-
- [Flags]
- public enum ExampleEnum : ulong
- {
- None = 0,
- One = 1,
- Two = 2,
- Four = 4,
- Eight = 8
- }
-
- [Test]
- public void Can_serialize_unsigned_flags_enum()
- {
- var anon = new
- {
- EnumProp1 = ExampleEnum.One | ExampleEnum.Two,
- EnumProp2 = ExampleEnum.Eight,
- };
-
- Assert.That(TypeSerializer.SerializeToString(anon), Is.EqualTo("{EnumProp1:3,EnumProp2:8}"));
- Assert.That(JsonSerializer.SerializeToString(anon), Is.EqualTo("{\"EnumProp1\":3,\"EnumProp2\":8}"));
- }
- }
-}
+ }
+
+ [Test]
+ public void Can_parse_json_with_nullable_valuetypes_that_has_no_value_specified()
+ {
+ var json = "{\"Int\":,\"Long\":,\"Decimal\":,\"Double\":,\"Boolean\":,\"DateTime\":}";
+
+ var item = JsonSerializer.DeserializeFromString<NullableValueTypes>(json);
+
+ Assert.That(item.Int, Is.Null, "int");
+ Assert.That(item.Long, Is.Null, "long");
+ Assert.That(item.Decimal, Is.Null, "decimal");
+ Assert.That(item.Double, Is.Null, "double");
+ Assert.That(item.Boolean, Is.Null, "boolean");
+ Assert.That(item.DateTime, Is.Null, "datetime");
+ }
+
+ [Test]
+ public void Can_handle_json_primitives()
+ {
+ var json = JsonSerializer.SerializeToString(JsonPrimitives.Create(1));
+ Log(json);
+
+ Assert.That(json, Is.EqualTo(
+ "{\"Int\":1,\"Long\":1,\"Float\":1,\"Double\":1,\"Boolean\":false,\"DateTime\":\"\\/Date(1)\\/\"}"));
+ }
+
+ [Test]
+ public void Can_parse_json_with_nulls()
+ {
+ const string json = "{\"Int\":1,\"NullString\":null}";
+ var value = JsonSerializer.DeserializeFromString<JsonPrimitives>(json);
+
+ Assert.That(value.Int, Is.EqualTo(1));
+ Assert.That(value.NullString, Is.Null);
+ }
+
+ [Test]
+ public void Can_serialize_dictionary_of_int_int()
+ {
+ var json = JsonSerializer.SerializeToString<IntIntDictionary>(new IntIntDictionary() { Dictionary = { { 10, 100 }, { 20, 200 } } });
+ const string expected = "{\"Dictionary\":{\"10\":100,\"20\":200}}";
+ Assert.That(json, Is.EqualTo(expected));
+ }
+
+ private class IntIntDictionary
+ {
+ public IntIntDictionary()
+ {
+ Dictionary = new Dictionary<int, int>();
+ }
+ public IDictionary<int, int> Dictionary { get; set; }
+ }
+
+ [Test]
+ public void Serialize_skips_null_values_by_default()
+ {
+ var o = new NullValueTester
+ {
+ Name = "Brandon",
+ Type = "Programmer",
+ SampleKey = 12,
+ Nothing = (string)null,
+ NullableDateTime = null
+ };
+
+ var s = JsonSerializer.SerializeToString(o);
+ Assert.That(s, Is.EqualTo("{\"Name\":\"Brandon\",\"Type\":\"Programmer\",\"SampleKey\":12}"));
+ }
+
+ [Test]
+ public void Serialize_can_include_null_values()
+ {
+ var o = new NullValueTester
+ {
+ Name = "Brandon",
+ Type = "Programmer",
+ SampleKey = 12,
+ Nothing = null,
+ NullClass = null,
+ NullableDateTime = null,
+ };
+
+ JsConfig.IncludeNullValues = true;
+ var s = JsonSerializer.SerializeToString(o);
+ JsConfig.Reset();
+ Assert.That(s, Is.EqualTo("{\"Name\":\"Brandon\",\"Type\":\"Programmer\",\"SampleKey\":12,\"Nothing\":null,\"NullClass\":null,\"NullableDateTime\":null}"));
+ }
+
+ private class NullClass
+ {
+
+ }
+
+ [Test]
+ public void Deserialize_sets_null_values()
+ {
+ var s = "{\"Name\":\"Brandon\",\"Type\":\"Programmer\",\"SampleKey\":12,\"Nothing\":null}";
+ var o = JsonSerializer.DeserializeFromString<NullValueTester>(s);
+ Assert.That(o.Name, Is.EqualTo("Brandon"));
+ Assert.That(o.Type, Is.EqualTo("Programmer"));
+ Assert.That(o.SampleKey, Is.EqualTo(12));
+ Assert.That(o.Nothing, Is.Null);
+ }
+
+ [Test]
+ public void Deserialize_ignores_omitted_values()
+ {
+ var s = "{\"Type\":\"Programmer\",\"SampleKey\":2}";
+ var o = JsonSerializer.DeserializeFromString<NullValueTester>(s);
+ Assert.That(o.Name, Is.EqualTo("Miguel"));
+ Assert.That(o.Type, Is.EqualTo("Programmer"));
+ Assert.That(o.SampleKey, Is.EqualTo(2));
+ Assert.That(o.Nothing, Is.EqualTo("zilch"));
+ }
+
+ private class NullValueTester
+ {
+ public string Name
+ {
+ get;
+ set;
+ }
+
+ public string Type
+ {
+ get;
+ set;
+ }
+
+ public int SampleKey
+ {
+ get;
+ set;
+ }
+
+ public string Nothing
+ {
+ get;
+ set;
+ }
+
+ public NullClass NullClass { get; set; }
+
+ public DateTime? NullableDateTime { get; set; }
+
+ public NullValueTester()
+ {
+ Name = "Miguel";
+ Type = "User";
+ SampleKey = 1;
+ Nothing = "zilch";
+ NullableDateTime = new DateTime(2012, 01, 01);
+ }
+ }
+
+ [DataContract]
+ class Person
+ {
+ [DataMember(Name = "MyID")]
+ public int Id { get; set; }
+ [DataMember]
+ public string Name { get; set; }
+ }
+
+ [Test]
+ public void Can_override_name()
+ {
+ var person = new Person
+ {
+ Id = 123,
+ Name = "Abc"
+ };
+
+ Assert.That(TypeSerializer.SerializeToString(person), Is.EqualTo("{MyID:123,Name:Abc}"));
+ Assert.That(JsonSerializer.SerializeToString(person), Is.EqualTo("{\"MyID\":123,\"Name\":\"Abc\"}"));
+ }
+
+ [Flags]
+ public enum ExampleEnum : ulong
+ {
+ None = 0,
+ One = 1,
+ Two = 2,
+ Four = 4,
+ Eight = 8
+ }
+
+ [Test]
+ public void Can_serialize_unsigned_flags_enum()
+ {
+ var anon = new
+ {
+ EnumProp1 = ExampleEnum.One | ExampleEnum.Two,
+ EnumProp2 = ExampleEnum.Eight,
+ };
+
+ Assert.That(TypeSerializer.SerializeToString(anon), Is.EqualTo("{EnumProp1:3,EnumProp2:8}"));
+ Assert.That(JsonSerializer.SerializeToString(anon), Is.EqualTo("{\"EnumProp1\":3,\"EnumProp2\":8}"));
+ }
+ }
+}
View
272 tests/ServiceStack.Text.Tests/JsonTests/BasicPropertiesTests.cs
@@ -1,164 +1,164 @@
-using System;
-using System.Collections;
-using System.Collections.Generic;
-using System.Text;
-using NUnit.Framework;
-
-namespace ServiceStack.Text.Tests.JsonTests {
- #region Test case types
-
- public class ContainsIDictionary {
- public IDictionary Container { get; set; }
- }
- public class ContainsGenericStringDictionary {
- public Dictionary<string, string> Container { get; set; }
- }
-
- public class SeveralTypesOfDictionary {
- public IDictionary GuidToInt { get; set; }
- public IDictionary DateTimeTo_DictStrStr { get; set; }
- }
-
- #endregion
-
- [TestFixture]
- public class BasicPropertiesTests {
-
- [Test]
- public void Generic_dictionary_backed_IDictionary_round_trips_ok () {
- var original = new ContainsIDictionary {
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Text;
+using NUnit.Framework;
+
+namespace ServiceStack.Text.Tests.JsonTests {
+ #region Test case types
+
+ public class ContainsIDictionary {
+ public IDictionary Container { get; set; }
+ }
+ public class ContainsGenericStringDictionary {
+ public Dictionary<string, string> Container { get; set; }
+ }
+
+ public class SeveralTypesOfDictionary {
+ public IDictionary GuidToInt { get; set; }
+ public IDictionary DateTimeTo_DictStrStr { get; set; }
+ }
+
+ #endregion
+
+ [TestFixture]
+ public class BasicPropertiesTests {
+
+ [Test]
+ public void Generic_dictionary_backed_IDictionary_round_trips_ok () {
+ var original = new ContainsIDictionary {
Container = new Dictionary<string, string>
{
{"one", "header one"},
{"two", "header two"}
- }
- };
-
- var str = JsonSerializer.SerializeToString(original);
- var obj = JsonSerializer.DeserializeFromString<ContainsIDictionary>(str);
-
- Console.WriteLine(DictStr(obj.Container));
- Assert.That(DictStr(obj.Container), Is.EqualTo(DictStr(original.Container)));
- }
-
- [Test]
- public void Generic_dictionary_backed_IDictionary_deserialises_to_generic_dictionary () {
- var original = new ContainsIDictionary // Using IDictionary backing
- {
+ }
+ };
+
+ var str = JsonSerializer.SerializeToString(original);
+ var obj = JsonSerializer.DeserializeFromString<ContainsIDictionary>(str);
+
+ Console.WriteLine(DictStr(obj.Container));
+ Assert.That(DictStr(obj.Container), Is.EqualTo(DictStr(original.Container)));
+ }
+
+ [Test]
+ public void Generic_dictionary_backed_IDictionary_deserialises_to_generic_dictionary () {
+ var original = new ContainsIDictionary // Using IDictionary backing
+ {
Container = new Dictionary<string, string>
{
{"one", "header one"},
{"two", "header two"}
- }
- };
-
- var str = JsonSerializer.SerializeToString(original);
- var obj = JsonSerializer.DeserializeFromString<ContainsGenericStringDictionary>(str); // decoding to Dictionary<,>
-
- Console.WriteLine(DictStr(obj.Container));
- Assert.That(DictStr(obj.Container), Is.EqualTo(DictStr(original.Container)));
- }
-
- [Test]
- public void Generic_dictionary_deserialises_to_IDictionary () {
- var original = new ContainsGenericStringDictionary // Using Dictionary<,> backing
- {
+ }
+ };
+
+ var str = JsonSerializer.SerializeToString(original);
+ var obj = JsonSerializer.DeserializeFromString<ContainsGenericStringDictionary>(str); // decoding to Dictionary<,>
+
+ Console.WriteLine(DictStr(obj.Container));
+ Assert.That(DictStr(obj.Container), Is.EqualTo(DictStr(original.Container)));
+ }
+
+ [Test]
+ public void Generic_dictionary_deserialises_to_IDictionary () {
+ var original = new ContainsGenericStringDictionary // Using Dictionary<,> backing
+ {
Container = new Dictionary<string, string>
{
{"one", "header one"},
{"two", "header two"}
- }
- };
-
- var str = JsonSerializer.SerializeToString(original);
- var obj = JsonSerializer.DeserializeFromString<ContainsIDictionary>(str); // decoding to IDictionary
-
- Console.WriteLine(DictStr(obj.Container));
- Assert.That(DictStr(obj.Container), Is.EqualTo(DictStr(original.Container)));
- }
-
- [Test]
- public void Generic_dictionary_round_trips_ok () {
- var original = new ContainsGenericStringDictionary {
+ }
+ };
+
+ var str = JsonSerializer.SerializeToString(original);
+ var obj = JsonSerializer.DeserializeFromString<ContainsIDictionary>(str); // decoding to IDictionary
+
+ Console.WriteLine(DictStr(obj.Container));
+ Assert.That(DictStr(obj.Container), Is.EqualTo(DictStr(original.Container)));
+ }
+
+ [Test]
+ public void Generic_dictionary_round_trips_ok () {
+ var original = new ContainsGenericStringDictionary {
Container = new Dictionary<string, string>
{
{"one", "header one"},
{"two", "header two"}
- }
- };
-
- var str = JsonSerializer.SerializeToString(original);
- var obj = JsonSerializer.DeserializeFromString<ContainsGenericStringDictionary>(str);
-
- Console.WriteLine(DictStr(obj.Container));
- Assert.That(DictStr(obj.Container), Is.EqualTo(DictStr(original.Container)));
- }
-
- [Test]
- public void Generic_dictionary_and_IDictionary_serialise_the_same () {
- JsConfig.PreferInterfaces = true;
- JsConfig.ExcludeTypeInfo = false;
- JsConfig.ConvertObjectTypesIntoStringDictionary = false;
-
- var genericStringDictionary = new ContainsGenericStringDictionary {
+ }
+ };
+
+ var str = JsonSerializer.SerializeToString(original);
+ var obj = JsonSerializer.DeserializeFromString<ContainsGenericStringDictionary>(str);
+
+ Console.WriteLine(DictStr(obj.Container));
+ Assert.That(DictStr(obj.Container), Is.EqualTo(DictStr(original.Container)));
+ }
+
+ [Test]
+ public void Generic_dictionary_and_IDictionary_serialise_the_same () {
+ JsConfig.PreferInterfaces = true;
+ JsConfig.ExcludeTypeInfo = false;
+ JsConfig.ConvertObjectTypesIntoStringDictionary = false;
+
+ var genericStringDictionary = new ContainsGenericStringDictionary {
Container = new Dictionary<string, string>
{
{"one", "header one"},
{"two", "header two"}
- }
- };
- var iDictionary = new ContainsIDictionary {
+ }
+ };
+ var iDictionary = new ContainsIDictionary {
Container = new Dictionary<string, string>
{
{"one", "header one"},
{"two", "header two"}
- }
- };
-
- var genDict = genericStringDictionary.ToJson();
- var iDict = iDictionary.ToJson();
-
- Console.WriteLine("Dictionary<string,string> --> " + genDict);
- Console.WriteLine();
- Console.WriteLine("IDictionary --> " + iDict);
-
- Assert.That(genDict, Is.EqualTo(iDict));
- }
-
- [Test]
- [Ignore("Very complex mappings, not needed for most tasks.")]
- public void Complex_dictionaries_round_trip () {
- var original = new SeveralTypesOfDictionary {
+ }
+ };
+
+ var genDict = genericStringDictionary.ToJson();
+ var iDict = iDictionary.ToJson();
+
+ Console.WriteLine("Dictionary<string,string> --> " + genDict);
+ Console.WriteLine();
+ Console.WriteLine("IDictionary --> " + iDict);
+
+ Assert.That(genDict, Is.EqualTo(iDict));
+ }
+
+ [Test]
+ [Ignore("Very complex mappings, not needed for most tasks.")]
+ public void Complex_dictionaries_round_trip () {
+ var original = new SeveralTypesOfDictionary {
GuidToInt = new Dictionary<Guid, int>
{
{Guid.Empty, 10},
{Guid.NewGuid(), 25}
- },
- DateTimeTo_DictStrStr = new Dictionary<DateTime, Dictionary<string, string>> {
- {DateTime.Today, new Dictionary<string, string> {{"a","b"},{"c","d"}}},
- {DateTime.Now, new Dictionary<string, string> {{"a","b"},{"c","d"}}}
- }
- };
- // see WriteDictionary.cs line 105
- // Problems:
- // - Int is turning into String on Deserialise
- // - Dictionary of dictionaries is totally failing on Deserialise
- var string_a = original.ToJson();
- var copy_a = string_a.FromJson<SeveralTypesOfDictionary>();
- var string_b = copy_a.ToJson();
- var copy_b = string_b.FromJson<SeveralTypesOfDictionary>();
-
- Console.WriteLine(string_a);
- Console.WriteLine(string_b);
- Assert.That(copy_a.GuidToInt[Guid.Empty], Is.EqualTo(10), "First copy was incorrect");
- Assert.That(copy_b.GuidToInt[Guid.Empty], Is.EqualTo(10), "Second copy was incorrect");
- Assert.That(string_a, Is.EqualTo(string_b), "Serialised forms not same");
- }
-
- static string DictStr (IDictionary d) {
- var sb = new StringBuilder();
- foreach (var key in d.Keys) { sb.AppendLine(key + " = " + d[key]); }
- return sb.ToString();
- }
- }
-}
+ },
+ DateTimeTo_DictStrStr = new Dictionary<DateTime, Dictionary<string, string>> {
+ {DateTime.Today, new Dictionary<string, string> {{"a","b"},{"c","d"}}},
+ {DateTime.Now, new Dictionary<string, string> {{"a","b"},{"c","d"}}}
+ }
+ };
+ // see WriteDictionary.cs line 105
+ // Problems:
+ // - Int is turning into String on Deserialise
+ // - Dictionary of dictionaries is totally failing on Deserialise
+ var string_a = original.ToJson();
+ var copy_a = string_a.FromJson<SeveralTypesOfDictionary>();
+ var string_b = copy_a.ToJson();
+ var copy_b = string_b.FromJson<SeveralTypesOfDictionary>();
+
+ Console.WriteLine(string_a);
+ Console.WriteLine(string_b);
+ Assert.That(copy_a.GuidToInt[Guid.Empty], Is.EqualTo(10), "First copy was incorrect");
+ Assert.That(copy_b.GuidToInt[Guid.Empty], Is.EqualTo(10), "Second copy was incorrect");
+ Assert.That(string_a, Is.EqualTo(string_b), "Serialised forms not same");
+ }
+
+ static string DictStr (IDictionary d) {
+ var sb = new StringBuilder();
+ foreach (var key in d.Keys) { sb.AppendLine(key + " = " + d[key]); }
+ return sb.ToString();
+ }
+ }
+}
View
138 tests/ServiceStack.Text.Tests/JsonTests/CamelCaseTests.cs
@@ -1,70 +1,70 @@
-using System;
-using System.Collections.Generic;
-using System.Runtime.Serialization;
-using NUnit.Framework;
-using ServiceStack.Text.Tests.Support;
-
-namespace ServiceStack.Text.Tests.JsonTests
-{
- [TestFixture]
- public class CamelCaseTests : TestBase
- {
- [SetUp]
- public void SetUp()
- {
- JsConfig.EmitCamelCaseNames = true;
- }
-
- [TearDown]
- public void TearDown()
- {
- JsConfig.Reset();
- }
-
- [Test]
- public void Does_serialize_To_CamelCase()
- {
- var dto = new Movie
- {
- Id = 1,
- ImdbId = "tt0111161",
- Title = "The Shawshank Redemption",
- Rating = 9.2m,
- Director = "Frank Darabont",
- ReleaseDate = new DateTime(1995, 2, 17, 0, 0, 0, DateTimeKind.Utc),
- TagLine = "Fear can hold you prisoner. Hope can set you free.",
- Genres = new List<string> { "Crime", "Drama" },
- };
-
- var json = dto.ToJson();
-
- Assert.That(json, Is.EqualTo(
- "{\"id\":1,\"imdbId\":\"tt0111161\",\"title\":\"The Shawshank Redemption\",\"rating\":9.2,\"director\":\"Frank Darabont\",\"releaseDate\":\"\\/Date(792979200000)\\/\",\"tagLine\":\"Fear can hold you prisoner. Hope can set you free.\",\"genres\":[\"Crime\",\"Drama\"]}"));
-
- Serialize(dto);
- }
-
- [DataContract]
- class Person
- {
- [DataMember(Name = "MyID")]
- public int Id { get; set; }
- [DataMember]
- public string Name { get; set; }
- }
-
- [Test]
- public void Can_override_name()
- {
- var person = new Person
- {
- Id = 123,
- Name = "Abc"
- };
-
- Assert.That(TypeSerializer.SerializeToString(person), Is.EqualTo("{MyID:123,name:Abc}"));
- Assert.That(JsonSerializer.SerializeToString(person), Is.EqualTo("{\"MyID\":123,\"name\":\"Abc\"}"));
- }
-
- }
+using System;
+using System.Collections.Generic;
+using System.Runtime.Serialization;
+using NUnit.Framework;
+using ServiceStack.Text.Tests.Support;
+
+namespace ServiceStack.Text.Tests.JsonTests
+{
+ [TestFixture]
+ public class CamelCaseTests : TestBase
+ {
+ [SetUp]
+ public void SetUp()
+ {
+ JsConfig.EmitCamelCaseNames = true;
+ }
+
+ [TearDown]
+ public void TearDown()
+ {
+ JsConfig.Reset();
+ }
+
+ [Test]
+ public void Does_serialize_To_CamelCase()
+ {
+ var dto = new Movie
+ {
+ Id = 1,
+ ImdbId = "tt0111161",
+ Title = "The Shawshank Redemption",
+ Rating = 9.2m,
+ Director = "Frank Darabont",
+ ReleaseDate = new DateTime(1995, 2, 17, 0, 0, 0, DateTimeKind.Utc),
+ TagLine = "Fear can hold you prisoner. Hope can set you free.",
+ Genres = new List<string> { "Crime", "Drama" },
+ };
+
+ var json = dto.ToJson();
+
+ Assert.That(json, Is.EqualTo(
+ "{\"id\":1,\"imdbId\":\"tt0111161\",\"title\":\"The Shawshank Redemption\",\"rating\":9.2,\"director\":\"Frank Darabont\",\"releaseDate\":\"\\/Date(792979200000)\\/\",\"tagLine\":\"Fear can hold you prisoner. Hope can set you free.\",\"genres\":[\"Crime\",\"Drama\"]}"));
+
+ Serialize(dto);
+ }
+
+ [DataContract]
+ class Person
+ {
+ [DataMember(Name = "MyID")]
+ public int Id { get; set; }
+ [DataMember]
+ public string Name { get; set; }
+ }
+
+ [Test]
+ public void Can_override_name()
+ {
+ var person = new Person
+ {
+ Id = 123,
+ Name = "Abc"
+ };
+
+ Assert.That(TypeSerializer.SerializeToString(person), Is.EqualTo("{MyID:123,name:Abc}"));
+ Assert.That(JsonSerializer.SerializeToString(person), Is.EqualTo("{\"MyID\":123,\"name\":\"Abc\"}"));
+ }
+
+ }
}
View
156 tests/ServiceStack.Text.Tests/JsonTests/ContractByInterfaceTests.cs
@@ -1,78 +1,78 @@
-using System;
-using NUnit.Framework;
-
-namespace ServiceStack.Text.Tests.JsonTests {
- /// <summary>
- /// Service Bus messaging works best if processes can share interface message contracts
- /// but not have to share concrete types.
- /// </summary>
- [TestFixture]
- public class ContractByInterfaceTests {
- [Test]
- public void Prefer_interfaces_should_work_on_top_level_object_using_extension_method () {
- JsConfig.PreferInterfaces = true;
- var json = new Concrete("boo", 1).ToJson();
-
- Assert.That(json, Is.StringContaining("\"ServiceStack.Text.Tests.JsonTests.IContract, ServiceStack.Text.Tests\""));
- }
-
- [Test]
- public void Should_be_able_to_serialise_based_on_an_interface () {
- JsConfig.PreferInterfaces = true;
- IContract myConcrete = new Concrete("boo", 1);
- var json = JsonSerializer.SerializeToString(myConcrete, typeof(IContract));
-
- Console.WriteLine(json);
- Assert.That(json, Is.StringContaining("\"ServiceStack.Text.Tests.JsonTests.IContract, ServiceStack.Text.Tests\""));
- }
-
- [Test]
- public void Should_not_use_interface_type_if_concrete_specified () {
- JsConfig.PreferInterfaces = false;
- IContract myConcrete = new Concrete("boo", 1);
- var json = JsonSerializer.SerializeToString(myConcrete, typeof(IContract));
-
- Console.WriteLine(json);
- Assert.That(json, Is.StringContaining("\"ServiceStack.Text.Tests.JsonTests.Concrete, ServiceStack.Text.Tests\""));
- }
-
- [Test]
- public void Should_be_able_to_deserialise_based_on_an_interface_with_no_concrete () {
- JsConfig.PreferInterfaces = true;
- var json = new Concrete("boo", 42).ToJson();
-
- // break the typing so we have to use the dynamic implementation
- json = json.Replace("ServiceStack.Text.Tests.JsonTests.IContract", "ServiceStack.Text.Tests.JsonTests.IIdenticalContract");
-
- var result = JsonSerializer.DeserializeFromString<IIdenticalContract>(json);
-
- Assert.That(result.StringValue, Is.EqualTo("boo"));
- Assert.That(result.ChildProp.IntValue, Is.EqualTo(42));
- }
- }
-
- class Concrete : IContract {
- public Concrete(string boo, int i) {
- StringValue = boo;
- ChildProp = new ConcreteChild { IntValue = i };
- }
-
- public string StringValue { get; set; }
- public IChildInterface ChildProp { get; set; }
- }
- class ConcreteChild : IChildInterface {
- public int IntValue { get; set; }
- }
-
- public interface IChildInterface {
- int IntValue { get; set; }
- }
- public interface IContract {
- string StringValue { get; set; }
- IChildInterface ChildProp { get; set; }
- }
- public interface IIdenticalContract {
- string StringValue { get; set; }
- IChildInterface ChildProp { get; set; }
- }
-}
+using System;
+using NUnit.Framework;
+
+namespace ServiceStack.Text.Tests.JsonTests {
+ /// <summary>
+ /// Service Bus messaging works best if processes can share interface message contracts
+ /// but not have to share concrete types.
+ /// </summary>
+ [TestFixture]
+ public class ContractByInterfaceTests {
+ [Test]
+ public void Prefer_interfaces_should_work_on_top_level_object_using_extension_method () {
+ JsConfig.PreferInterfaces = true;
+ var json = new Concrete("boo", 1).ToJson();
+
+ Assert.That(json, Is.StringContaining("\"ServiceStack.Text.Tests.JsonTests.IContract, ServiceStack.Text.Tests\""));
+ }
+
+ [Test]
+ public void Should_be_able_to_serialise_based_on_an_interface () {
+ JsConfig.PreferInterfaces = true;
+ IContract myConcrete = new Concrete("boo", 1);
+ var json = JsonSerializer.SerializeToString(myConcrete, typeof(IContract));
+
+ Console.WriteLine(json);
+ Assert.That(json, Is.StringContaining("\"ServiceStack.Text.Tests.JsonTests.IContract, ServiceStack.Text.Tests\""));
+ }
+
+ [Test]
+ public void Should_not_use_interface_type_if_concrete_specified () {
+ JsConfig.PreferInterfaces = false;
+ IContract myConcrete = new Concrete("boo", 1);
+ var json = JsonSerializer.SerializeToString(myConcrete, typeof(IContract));
+
+ Console.WriteLine(json);
+ Assert.That(json, Is.StringContaining("\"ServiceStack.Text.Tests.JsonTests.Concrete, ServiceStack.Text.Tests\""));
+ }
+
+ [Test]
+ public void Should_be_able_to_deserialise_based_on_an_interface_with_no_concrete () {
+ JsConfig.PreferInterfaces = true;
+ var json = new Concrete("boo", 42).ToJson();
+
+ // break the typing so we have to use the dynamic implementation
+ json = json.Replace("ServiceStack.Text.Tests.JsonTests.IContract", "ServiceStack.Text.Tests.JsonTests.IIdenticalContract");
+
+ var result = JsonSerializer.DeserializeFromString<IIdenticalContract>(json);
+
+ Assert.That(result.StringValue, Is.EqualTo("boo"));
+ Assert.That(result.ChildProp.IntValue, Is.EqualTo(42));
+ }
+ }
+
+ class Concrete : IContract {
+ public Concrete(string boo, int i) {
+ StringValue = boo;
+ ChildProp = new ConcreteChild { IntValue = i };
+ }
+
+ public string StringValue { get; set; }
+ public IChildInterface ChildProp { get; set; }
+ }
+ class ConcreteChild : IChildInterface {
+ public int IntValue { get; set; }
+ }
+
+ public interface IChildInterface {
+ int IntValue { get; set; }
+ }
+ public interface IContract {
+ string StringValue { get; set; }
+ IChildInterface ChildProp { get; set; }
+ }
+ public interface IIdenticalContract {
+ string StringValue { get; set; }
+ IChildInterface ChildProp { get; set; }
+ }
+}
View
182 tests/ServiceStack.Text.Tests/JsonTests/CustomSerializerTests.cs
@@ -1,92 +1,92 @@
-using System.Collections.Generic;
-using System.Globalization;
-using System.Linq;
-using NUnit.Framework;
-
-namespace ServiceStack.Text.Tests.JsonTests
-{
- public class CustomSerializerTests : TestBase
- {
- static CustomSerializerTests()
- {
- JsConfig<EntityWithValues>.RawSerializeFn = SerializeEntity;
- JsConfig<EntityWithValues>.RawDeserializeFn = DeserializeEntity;
- }
-
- [Test]
- public void Can_serialize_Entity()
- {
- var originalEntity = new EntityWithValues {id = 5, Values = new Dictionary<string, string> {{"dog", "bark"}, {"cat", "meow"}}};
- JsonSerializeAndCompare(originalEntity);
- }
-
- [Test]
- public void Can_serialize_arrays_of_entities()
- {
- var originalEntities = new[] { new EntityWithValues {id = 5, Values = new Dictionary<string, string> {{"dog", "bark"}}}, new EntityWithValues {id = 6, Values = new Dictionary<string, string> {{"cat", "meow"}}} };
- JsonSerializeAndCompare(originalEntities);
- }
-
- public class EntityWithValues
- {
- private Dictionary<string, string> _values;
-
- public int id { get; set; }
-
- public Dictionary<string, string> Values
- {
- get { return _values ?? (_values = new Dictionary<string, string>()); }
- set { _values = value; }
- }
-
- public override int GetHashCode()
- {
- return this.id.GetHashCode() ^ this.Values.GetHashCode();
- }
-
- public override bool Equals(object obj)
- {
- return Equals(obj as EntityWithValues);
- }
-
- public bool Equals(EntityWithValues other)
- {
- return ReferenceEquals(this, other)
- || (this.id == other.id && DictionaryEquality(Values, other.Values));
- }
-
- private bool DictionaryEquality(Dictionary<string, string> first, Dictionary<string, string> second)
- {
- return first.Count == second.Count
- && first.Keys.All(second.ContainsKey)
- && first.Keys.All(key => first[key] == second[key]);
- }
- }
-
- private static string SerializeEntity(EntityWithValues entity)
- {
- var dictionary = entity.Values.ToDictionary(pair => pair.Key, pair => pair.Value);
- if (entity.id > 0) {
- dictionary["id"] = entity.id.ToString(CultureInfo.InvariantCulture);
- }
- return JsonSerializer.SerializeToString(dictionary);
- }
-
- private static EntityWithValues DeserializeEntity(string value)
- {
- var dictionary = JsonSerializer.DeserializeFromString<Dictionary<string, string>>(value);
- if (dictionary == null) return null;
- var entity = new EntityWithValues();
- foreach (var pair in dictionary) {
- if (pair.Key == "id") {
- if (!string.IsNullOrEmpty(pair.Value)) {
- entity.id = int.Parse(pair.Value);
- }
- } else {
- entity.Values.Add(pair.Key, pair.Value);
- }
- }
- return entity;
- }
- }
+using System.Collections.Generic;
+using System.Globalization;
+using System.Linq;
+using NUnit.Framework;
+
+namespace ServiceStack.Text.Tests.JsonTests
+{
+ public class CustomSerializerTests : TestBase
+ {
+ static CustomSerializerTests()
+ {
+ JsConfig<EntityWithValues>.RawSerializeFn = SerializeEntity;
+ JsConfig<EntityWithValues>.RawDeserializeFn = DeserializeEntity;
+ }
+
+ [Test]
+ public void Can_serialize_Entity()
+ {
+ var originalEntity = new EntityWithValues {id = 5, Values = new Dictionary<string, string> {{"dog", "bark"}, {"cat", "meow"}}};
+ JsonSerializeAndCompare(originalEntity);
+ }
+
+ [Test]
+ public void Can_serialize_arrays_of_entities()
+ {
+ var originalEntities = new[] { new EntityWithValues {id = 5, Values = new Dictionary<string, string> {{"dog", "bark"}}}, new EntityWithValues {id = 6, Values = new Dictionary<string, string> {{"cat", "meow"}}} };
+ JsonSerializeAndCompare(originalEntities);
+ }
+
+ public class EntityWithValues
+ {
+ private Dictionary<string, string> _values;
+
+ public int id { get; set; }
+
+ public Dictionary<string, string> Values
+ {
+ get { return _values ?? (_values = new Dictionary<string, string>()); }
+ set { _values = value; }
+ }
+
+ public override int GetHashCode()
+ {
+ return this.id.GetHashCode() ^ this.Values.GetHashCode();
+ }
+
+ public override bool Equals(object obj)
+ {
+ return Equals(obj as EntityWithValues);
+ }
+
+ public bool Equals(EntityWithValues other)
+ {
+ return ReferenceEquals(this, other)
+ || (this.id == other.id && DictionaryEquality(Values, other.Values));
+ }
+
+ private bool DictionaryEquality(Dictionary<string, string> first, Dictionary<string, string> second)
+ {
+ return first.Count == second.Count
+ && first.Keys.All(second.ContainsKey)
+ && first.Keys.All(key => first[key] == second[key]);
+ }
+ }
+
+ private static string SerializeEntity(EntityWithValues entity)
+ {
+ var dictionary = entity.Values.ToDictionary(pair => pair.Key, pair => pair.Value);
+ if (entity.id > 0) {
+ dictionary["id"] = entity.id.ToString(CultureInfo.InvariantCulture);
+ }
+ return JsonSerializer.SerializeToString(dictionary);
+ }
+
+ private static EntityWithValues DeserializeEntity(string value)
+ {
+ var dictionary = JsonSerializer.DeserializeFromString<Dictionary<string, string>>(value);
+ if (dictionary == null) return null;
+ var entity = new EntityWithValues();
+ foreach (var pair in dictionary) {
+ if (pair.Key == "id") {
+ if (!string.IsNullOrEmpty(pair.Value)) {
+ entity.id = int.Parse(pair.Value);
+ }
+ } else {
+ entity.Values.Add(pair.Key, pair.Value);