Permalink
Browse files

Fixed incorrect serializing/parsing of null values

  • Loading branch information...
1 parent 83ee5a6 commit d6d130c796dbe7c4b70e9fec059d7a9bbf43f916 @maddenpj maddenpj committed Dec 21, 2012
Showing with 23 additions and 65 deletions.
  1. +7 −28 Client.Tests/JsonDeserializationTests.cs
  2. +4 −32 Client.Tests/JsonSerializationTests.cs
  3. +12 −5 Client/Model/Series.cs
View
35 Client.Tests/JsonDeserializationTests.cs
@@ -50,7 +50,7 @@ public void SmokeTest()
{
var jsonResponse = new RestResponse
{
- Content = "{\"id\":\"series-id\",\"key\":\"series-key\",\"name\":null,\"attributes\":null,\"tags\":null}"
+ Content = "{\"id\":\"series-id\",\"key\":\"series-key\",\"name\":\"\",\"attributes\":{},\"tags\":[]}"
};
var series = new Series
{
@@ -59,6 +59,9 @@ public void SmokeTest()
};
var result = JsonDeserializationTests.deserializer.Deserialize<Series>(jsonResponse);
+ Console.WriteLine(series.Name == null);
+ Console.WriteLine(series.Tags == null);
+ Console.WriteLine(result.Tags == null);
Assert.AreEqual(series, result);
}
@@ -67,7 +70,7 @@ public void Name()
{
var jsonResponse = new RestResponse
{
- Content = "{\"id\":\"series-id\",\"key\":\"series-key\",\"name\":\"series-name\",\"attributes\":null,\"tags\":null}"
+ Content = "{\"id\":\"series-id\",\"key\":\"series-key\",\"name\":\"series-name\",\"attributes\":{},\"tags\":[]}"
};
var result = JsonDeserializationTests.deserializer.Deserialize<Series>(jsonResponse);
@@ -79,7 +82,7 @@ public void Tags()
{
var jsonResponse = new RestResponse
{
- Content = "{\"id\":\"series-id\",\"key\":\"series-key\",\"name\":null,\"attributes\":null,\"tags\":[\"tag1\",\"tag2\"]}"
+ Content = "{\"id\":\"series-id\",\"key\":\"series-key\",\"name\":\"\",\"attributes\":{},\"tags\":[\"tag1\",\"tag2\"]}"
};
var result = JsonDeserializationTests.deserializer.Deserialize<Series>(jsonResponse);
@@ -93,7 +96,7 @@ public void Attributes()
{
var jsonResponse = new RestResponse
{
- Content = "{\"id\":\"series-id\",\"key\":\"series-key\",\"name\":null,\"attributes\":{\"key1\":\"val1\",\"key2\":\"val2\"},\"tags\":null}"
+ Content = "{\"id\":\"series-id\",\"key\":\"series-key\",\"name\":\"\",\"attributes\":{\"key1\":\"val1\",\"key2\":\"val2\"},\"tags\":[]}"
};
var result = JsonDeserializationTests.deserializer.Deserialize<Series>(jsonResponse);
@@ -102,30 +105,6 @@ public void Attributes()
Assert.AreEqual("val2", result.Attributes["key2"]);
}
-
- [Test]
- public void EmptyTags()
- {
- var jsonResponse = new RestResponse
- {
- Content = "{\"id\":\"series-id\",\"key\":\"series-key\",\"name\":null,\"attributes\":null,\"tags\":[]}"
- };
-
- var result = JsonDeserializationTests.deserializer.Deserialize<Series>(jsonResponse);
- Assert.AreEqual(0, result.Tags.Count);
- }
-
- [Test]
- public void EmptyAttributes()
- {
- var jsonResponse = new RestResponse
- {
- Content = "{\"id\":\"series-id\",\"key\":\"series-key\",\"name\":null,\"attributes\":{},\"tags\":null}"
- };
-
- var result = JsonDeserializationTests.deserializer.Deserialize<Series>(jsonResponse);
- Assert.AreEqual(0, result.Attributes.Count);
- }
}
[TestFixture]
View
36 Client.Tests/JsonSerializationTests.cs
@@ -48,7 +48,7 @@ public void SmokeTest()
};
var result = JsonSerializationTests.serializer.Serialize(series);
- Assert.AreEqual("{\"id\":\"series-id\",\"key\":\"series-key\",\"name\":null,\"attributes\":null,\"tags\":null}", result);
+ Assert.AreEqual("{\"id\":\"series-id\",\"key\":\"series-key\",\"name\":\"\",\"attributes\":{},\"tags\":[]}", result);
}
[Test]
@@ -62,7 +62,7 @@ public void Name()
};
var result = JsonSerializationTests.serializer.Serialize(series);
- Assert.AreEqual("{\"id\":\"series-id\",\"key\":\"series-key\",\"name\":\"series-name\",\"attributes\":null,\"tags\":null}", result);
+ Assert.AreEqual("{\"id\":\"series-id\",\"key\":\"series-key\",\"name\":\"series-name\",\"attributes\":{},\"tags\":[]}", result);
}
[Test]
@@ -76,7 +76,7 @@ public void Tags()
};
var result = JsonSerializationTests.serializer.Serialize(series);
- Assert.AreEqual("{\"id\":\"series-id\",\"key\":\"series-key\",\"name\":null,\"attributes\":null,\"tags\":[\"tag1\",\"tag2\"]}", result);
+ Assert.AreEqual("{\"id\":\"series-id\",\"key\":\"series-key\",\"name\":\"\",\"attributes\":{},\"tags\":[\"tag1\",\"tag2\"]}", result);
}
[Test]
@@ -90,35 +90,7 @@ public void Attributes()
};
var result = JsonSerializationTests.serializer.Serialize(series);
- Assert.AreEqual("{\"id\":\"series-id\",\"key\":\"series-key\",\"name\":null,\"attributes\":{\"key1\":\"val1\",\"key2\":\"val2\"},\"tags\":null}", result);
- }
-
- [Test]
- public void EmptyTags()
- {
- Series series = new Series
- {
- Id = "series-id",
- Key = "series-key",
- Tags = new List<string>()
- };
-
- var result = JsonSerializationTests.serializer.Serialize(series);
- Assert.AreEqual("{\"id\":\"series-id\",\"key\":\"series-key\",\"name\":null,\"attributes\":null,\"tags\":[]}", result);
- }
-
- [Test]
- public void EmptyAttributes()
- {
- Series series = new Series
- {
- Id = "series-id",
- Key = "series-key",
- Attributes = new Dictionary<string, string>()
- };
-
- var result = JsonSerializationTests.serializer.Serialize(series);
- Assert.AreEqual("{\"id\":\"series-id\",\"key\":\"series-key\",\"name\":null,\"attributes\":{},\"tags\":null}", result);
+ Assert.AreEqual("{\"id\":\"series-id\",\"key\":\"series-key\",\"name\":\"\",\"attributes\":{\"key1\":\"val1\",\"key2\":\"val2\"},\"tags\":[]}", result);
}
}
View
17 Client/Model/Series.cs
@@ -15,19 +15,26 @@ namespace Client.Model
/// </summary>
public class Series
{
- [JsonProperty(PropertyName = "id")]
+ public Series()
+ {
+ Name = "";
+ Attributes = new Dictionary<string, string>();
+ Tags = new List<string>();
+ }
+
+ [JsonProperty(PropertyName = "id", Required = Required.Always)]
public string Id { get; set; }
- [JsonProperty(PropertyName = "key")]
+ [JsonProperty(PropertyName = "key", Required = Required.Always)]
public string Key { get; set; }
- [JsonProperty(PropertyName = "name")]
+ [JsonProperty(PropertyName = "name", Required = Required.Always)]
public string Name { get; set; }
- [JsonProperty(PropertyName = "attributes")]
+ [JsonProperty(PropertyName = "attributes", Required = Required.Always)]
public Dictionary<string, string> Attributes { get; set; }
- [JsonProperty(PropertyName = "tags")]
+ [JsonProperty(PropertyName = "tags", Required = Required.Always)]
public List<string> Tags { get; set; }
public override string ToString()

0 comments on commit d6d130c

Please sign in to comment.