Skip to content

Commit 84237b7

Browse files
committed
Merge pull request #4 from csantero/serialize-arrays
serialize arrays
2 parents 5fcd61e + 260d3e0 commit 84237b7

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

JSONAPI.Tests/Json/JsonApiMediaFormaterTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using Microsoft.VisualStudio.TestTools.UnitTesting;
34
using JSONAPI.Tests.Models;
45
using Newtonsoft.Json;
@@ -124,6 +125,31 @@ public void SerializerIntegrationTest()
124125
//Assert.AreEqual("[2,3,4]", sw.ToString());
125126
}
126127

128+
[TestMethod]
129+
[DeploymentItem(@"Data\SerializerIntegrationTest.json")]
130+
public void SerializeArrayIntegrationTest()
131+
{
132+
// Arrange
133+
//PayloadConverter pc = new PayloadConverter();
134+
//ModelConverter mc = new ModelConverter();
135+
//ContractResolver.PluralizationService = new PluralizationService();
136+
137+
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter();
138+
formatter.PluralizationService = new JSONAPI.Core.PluralizationService();
139+
MemoryStream stream = new MemoryStream();
140+
141+
// Act
142+
//Payload payload = new Payload(a.Posts);
143+
//js.Serialize(jw, payload);
144+
formatter.WriteToStreamAsync(typeof(Post), a.Posts.ToArray(), stream, (System.Net.Http.HttpContent)null, (System.Net.TransportContext)null);
145+
146+
// Assert
147+
string output = System.Text.Encoding.ASCII.GetString(stream.ToArray());
148+
Trace.WriteLine(output);
149+
Assert.AreEqual(output.Trim(), File.ReadAllText("SerializerIntegrationTest.json").Trim());
150+
//Assert.AreEqual("[2,3,4]", sw.ToString());
151+
}
152+
127153
[TestMethod]
128154
public void DeserializeCollectionIntegrationTest()
129155
{

JSONAPI/Json/JsonApiFormatter.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -763,8 +763,9 @@ private bool IsMany(dynamic value = null)
763763

764764
private bool IsMany(Type type)
765765
{
766-
//TODO: Should we check for arrays also? (They aren't generics.)
767-
return (type.GetInterfaces().Contains(typeof(IEnumerable)) && type.IsGenericType);
766+
return
767+
type.IsArray ||
768+
(type.GetInterfaces().Contains(typeof(IEnumerable)) && type.IsGenericType);
768769
}
769770

770771
private Type GetSingleType(Type type)//dynamic value = null)

0 commit comments

Comments
 (0)