Skip to content

Commit 260d3e0

Browse files
author
Chris Santero
committed
serialize arrays
1 parent 3955509 commit 260d3e0

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;
@@ -117,6 +118,31 @@ public void SerializerIntegrationTest()
117118
//Assert.AreEqual("[2,3,4]", sw.ToString());
118119
}
119120

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

JSONAPI/Json/JsonApiFormatter.cs

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

760760
private bool IsMany(Type type)
761761
{
762-
//TODO: Should we check for arrays also? (They aren't generics.)
763-
return (type.GetInterfaces().Contains(typeof(IEnumerable)) && type.IsGenericType);
762+
return
763+
type.IsArray ||
764+
(type.GetInterfaces().Contains(typeof(IEnumerable)) && type.IsGenericType);
764765
}
765766

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

0 commit comments

Comments
 (0)