Skip to content

Commit 100edc3

Browse files
committed
Merge branch 'pull-test'
2 parents 84237b7 + bd21270 commit 100edc3

File tree

3 files changed

+42
-27
lines changed

3 files changed

+42
-27
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
{"posts":[{"id":"1","title":"Linkbait!","links":{"comments":["2","3","4"],"author":"1"}},{"id":"2","title":"Rant #1023","links":{"comments":["5"],"author":"1"}},{"id":"3","title":"Polemic in E-flat minor #824","links":{"comments":null,"author":"1"}}],"linked":{"comments":[{"id":"2","body":"Nuh uh!","links":{"post":"1"}},{"id":"3","body":"Yeah huh!","links":{"post":"1"}},{"id":"4","body":"Third Reich.","links":{"post":"1"}},{"id":"5","body":"I laughed, I cried!","links":{"post":"2"}}],"authors":[{"id":"1","name":"Jason Hater","links":{"posts":["1","2","3"]}}]}}
1+
{"posts":[{"id":"1","title":"Linkbait!","links":{"comments":["2","3","4"],"author":"1"}},{"id":"2","title":"Rant #1023","links":{"comments":["5"],"author":"1"}},{"id":"3","title":"Polemic in E-flat minor #824","links":{"comments":null,"author":"1"}},{"id":"4","title":"This post has no author.","links":{"comments":null,"author":null}}],"linked":{"comments":[{"id":"2","body":"Nuh uh!","links":{"post":"1"}},{"id":"3","body":"Yeah huh!","links":{"post":"1"}},{"id":"4","body":"Third Reich.","links":{"post":"1"}},{"id":"5","body":"I laughed, I cried!","links":{"post":"2"}}],"authors":[{"id":"1","name":"Jason Hater","links":{"posts":["1","2","3"]}}]}}
22

33

JSONAPI.Tests/Json/JsonApiMediaFormaterTests.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace JSONAPI.Tests.Json
1515
public class JsonApiMediaFormaterTests
1616
{
1717
Author a;
18-
Post p, p2, p3;
18+
Post p, p2, p3, p4;
1919

2020
[TestInitialize]
2121
public void SetupModels()
@@ -44,6 +44,11 @@ public void SetupModels()
4444
Title = "Polemic in E-flat minor #824",
4545
Author = a
4646
};
47+
p4 = new Post
48+
{
49+
Id = 4,
50+
Title = "This post has no author."
51+
};
4752

4853
a.Posts = new List<Post> { p, p2, p3 };
4954

@@ -116,7 +121,7 @@ public void SerializerIntegrationTest()
116121
// Act
117122
//Payload payload = new Payload(a.Posts);
118123
//js.Serialize(jw, payload);
119-
formatter.WriteToStreamAsync(typeof(Post), a.Posts, stream, (System.Net.Http.HttpContent)null, (System.Net.TransportContext)null);
124+
formatter.WriteToStreamAsync(typeof(Post), new[] { p, p2, p3, p4 }.ToList(), stream, (System.Net.Http.HttpContent)null, (System.Net.TransportContext)null);
120125

121126
// Assert
122127
string output = System.Text.Encoding.ASCII.GetString(stream.ToArray());
@@ -141,7 +146,7 @@ public void SerializeArrayIntegrationTest()
141146
// Act
142147
//Payload payload = new Payload(a.Posts);
143148
//js.Serialize(jw, payload);
144-
formatter.WriteToStreamAsync(typeof(Post), a.Posts.ToArray(), stream, (System.Net.Http.HttpContent)null, (System.Net.TransportContext)null);
149+
formatter.WriteToStreamAsync(typeof(Post), new[] { p, p2, p3, p4 }, stream, (System.Net.Http.HttpContent)null, (System.Net.TransportContext)null);
145150

146151
// Assert
147152
string output = System.Text.Encoding.ASCII.GetString(stream.ToArray());

JSONAPI/Json/JsonApiFormatter.cs

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -253,30 +253,40 @@ protected void Serialize(object value, Stream writeStream, JsonWriter writer, Js
253253
}
254254
else
255255
{
256-
string objId;
257-
258-
objId = GetIdFor(prop.GetValue(value, null));
259-
260-
switch (sa)
256+
var propertyValue = prop.GetValue(value, null);
257+
if (propertyValue == null)
261258
{
262-
case SerializeAsOptions.Ids:
263-
//writer.WritePropertyName(ContractResolver.FormatPropertyName(prop.Name));
264-
serializer.Serialize(writer, objId);
265-
if (iip)
266-
if (aggregator != null) aggregator.Add(prop.PropertyType, prop.GetValue(value, null));
267-
break;
268-
case SerializeAsOptions.Link:
269-
if (lt == null) throw new JsonSerializationException("A property was decorated with SerializeAs(SerializeAsOptions.Link) but no LinkTemplate attribute was provided.");
270-
string link = String.Format(lt, objId, value.GetType().GetProperty("Id").GetValue(value, null));
271-
//writer.WritePropertyName(ContractResolver.FormatPropertyName(prop.Name));
272-
writer.WriteValue(link);
273-
break;
274-
case SerializeAsOptions.Embedded:
275-
// Not really supported by Ember Data yet, incidentally...but easy to implement here.
276-
//writer.WritePropertyName(ContractResolver.FormatPropertyName(prop.Name));
277-
//serializer.Serialize(writer, prop.GetValue(value, null));
278-
this.Serialize(prop.GetValue(value, null), writeStream, writer, serializer, aggregator);
279-
break;
259+
writer.WriteNull();
260+
}
261+
else
262+
{
263+
string objId = GetIdFor(propertyValue);
264+
265+
switch (sa)
266+
{
267+
case SerializeAsOptions.Ids:
268+
//writer.WritePropertyName(ContractResolver.FormatPropertyName(prop.Name));
269+
serializer.Serialize(writer, objId);
270+
if (iip)
271+
if (aggregator != null)
272+
aggregator.Add(prop.PropertyType, prop.GetValue(value, null));
273+
break;
274+
case SerializeAsOptions.Link:
275+
if (lt == null)
276+
throw new JsonSerializationException(
277+
"A property was decorated with SerializeAs(SerializeAsOptions.Link) but no LinkTemplate attribute was provided.");
278+
string link = String.Format(lt, objId,
279+
value.GetType().GetProperty("Id").GetValue(value, null));
280+
//writer.WritePropertyName(ContractResolver.FormatPropertyName(prop.Name));
281+
writer.WriteValue(link);
282+
break;
283+
case SerializeAsOptions.Embedded:
284+
// Not really supported by Ember Data yet, incidentally...but easy to implement here.
285+
//writer.WritePropertyName(ContractResolver.FormatPropertyName(prop.Name));
286+
//serializer.Serialize(writer, prop.GetValue(value, null));
287+
this.Serialize(prop.GetValue(value, null), writeStream, writer, serializer, aggregator);
288+
break;
289+
}
280290
}
281291
}
282292

0 commit comments

Comments
 (0)