Skip to content

Commit 3ade38b

Browse files
committed
Merge pull request #49 from csantero/backport-more-types
backport new types for 0.2.2
2 parents 9259ae9 + 141bdd2 commit 3ade38b

8 files changed

Lines changed: 359 additions & 54 deletions

File tree

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
{
2+
"samples": [
3+
{
4+
"id": "1",
5+
"booleanField": false,
6+
"nullableBooleanField": false,
7+
"sByteField": 0,
8+
"nullableSByteField": null,
9+
"byteField": 0,
10+
"nullableByteField": null,
11+
"int16Field": 0,
12+
"nullableInt16Field": null,
13+
"uInt16Field": 0,
14+
"nullableUInt16Field": null,
15+
"int32Field": 0,
16+
"nullableInt32Field": null,
17+
"uInt32Field": 0,
18+
"nullableUInt32Field": null,
19+
"int64Field": 0,
20+
"nullableInt64Field": null,
21+
"uInt64Field": 0,
22+
"nullableUInt64Field": null,
23+
"doubleField": 0.0,
24+
"nullableDoubleField": null,
25+
"singleField": 0.0,
26+
"nullableSingleField": null,
27+
"decimalField": "0",
28+
"nullableDecimalField": null,
29+
"dateTimeField": "0001-01-01T00:00:00",
30+
"nullableDateTimeField": null,
31+
"dateTimeOffsetField": "0001-01-01T00:00:00+00:00",
32+
"nullableDateTimeOffsetField": null,
33+
"guidField": "00000000-0000-0000-0000-000000000000",
34+
"nullableGuidField": null,
35+
"stringField": null,
36+
"enumField": 0,
37+
"nullableEnumField": null
38+
}, {
39+
"id": "2",
40+
"booleanField": true,
41+
"nullableBooleanField": true,
42+
"sByteField": 123,
43+
"nullableSByteField": 123,
44+
"byteField": 253,
45+
"nullableByteField": 253,
46+
"int16Field": 32000,
47+
"nullableInt16Field": 32000,
48+
"uInt16Field": 64000,
49+
"nullableUInt16Field": 64000,
50+
"int32Field": 2000000000,
51+
"nullableInt32Field": 2000000000,
52+
"uInt32Field": 3000000000,
53+
"nullableUInt32Field": 3000000000,
54+
"int64Field": 9223372036854775807,
55+
"nullableInt64Field": 9223372036854775807,
56+
"uInt64Field": 9223372036854775808,
57+
"nullableUInt64Field": 9223372036854775808,
58+
"doubleField": 1056789.123,
59+
"nullableDoubleField": 1056789.123,
60+
"singleField": 1056789.13,
61+
"nullableSingleField": 1056789.13,
62+
"decimalField": "1056789.123",
63+
"nullableDecimalField": "1056789.123",
64+
"dateTimeField": "1776-07-04T00:00:00",
65+
"nullableDateTimeField": "1776-07-04T00:00:00",
66+
"dateTimeOffsetField": "1776-07-04T00:00:00-05:00",
67+
"nullableDateTimeOffsetField": "1776-07-04T00:00:00-05:00",
68+
"guidField": "6566f9b4-5245-40de-890d-98b40a4ad656",
69+
"nullableGuidField": "3d1fb81e-43ee-4d04-af91-c8a326341293",
70+
"stringField": "Some string 156",
71+
"enumField": 1,
72+
"nullableEnumField": 2
73+
}
74+
]
75+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using JSONAPI.Extensions;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
5+
namespace JSONAPI.Tests.Extensions
6+
{
7+
[TestClass]
8+
public class TypeExtensionsTests
9+
{
10+
private enum TestEnum
11+
{
12+
13+
}
14+
15+
[TestMethod]
16+
public void CanWriteAsJsonApiAttributeTest()
17+
{
18+
Assert.IsTrue(typeof(Byte).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for Byte!");
19+
Assert.IsTrue(typeof(Byte?).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for nullable Byte!");
20+
Assert.IsTrue(typeof(SByte).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for SByte!");
21+
Assert.IsTrue(typeof(SByte?).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for nullable SByte!");
22+
Assert.IsTrue(typeof(UInt16).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for UInt16!");
23+
Assert.IsTrue(typeof(UInt16?).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for nullable UInt16!");
24+
Assert.IsTrue(typeof(Int16).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for Int16!");
25+
Assert.IsTrue(typeof(Int16?).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for nullable Int16!");
26+
Assert.IsTrue(typeof(UInt32).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for UInt32!");
27+
Assert.IsTrue(typeof(UInt32?).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for nullable UInt32!");
28+
Assert.IsTrue(typeof(Int32).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for Int32!");
29+
Assert.IsTrue(typeof(Int32?).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for nullable Int32!");
30+
Assert.IsTrue(typeof(UInt64).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for UInt64!");
31+
Assert.IsTrue(typeof(UInt64?).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for nullable UInt64!");
32+
Assert.IsTrue(typeof(Int64).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for Int64!");
33+
Assert.IsTrue(typeof(Int64?).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for nullable Int64!");
34+
Assert.IsTrue(typeof(Double).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for Double!");
35+
Assert.IsTrue(typeof(Double?).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for nullable Double!");
36+
Assert.IsTrue(typeof(Single).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for Single!");
37+
Assert.IsTrue(typeof(Single?).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for nullable Single!");
38+
Assert.IsTrue(typeof(Decimal).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for Decimal!");
39+
Assert.IsTrue(typeof(Decimal?).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for nullable Decimal!");
40+
Assert.IsTrue(typeof(DateTime).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for DateTime!");
41+
Assert.IsTrue(typeof(DateTime?).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for nullable DateTime!");
42+
Assert.IsTrue(typeof(DateTimeOffset).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for DateTimeOffset!");
43+
Assert.IsTrue(typeof(DateTimeOffset?).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for nullable DateTimeOffset!");
44+
Assert.IsTrue(typeof(Guid).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for Guid!");
45+
Assert.IsTrue(typeof(Guid?).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for nullable Guid!");
46+
Assert.IsTrue(typeof(String).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for String!");
47+
Assert.IsTrue(typeof(TestEnum).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for enum!");
48+
Assert.IsTrue(typeof(TestEnum?).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for nullable enum!");
49+
Assert.IsFalse(typeof(Object).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for Object!");
50+
}
51+
52+
}
53+
}

JSONAPI.Tests/JSONAPI.Tests.csproj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,13 @@
7676
</ItemGroup>
7777
<ItemGroup>
7878
<Compile Include="Core\MetadataManagerTests.cs" />
79+
<Compile Include="Extensions\TypeExtensionsTests.cs" />
7980
<Compile Include="Json\ErrorSerializerTests.cs" />
8081
<Compile Include="Json\JsonApiMediaFormaterTests.cs" />
8182
<Compile Include="Json\JsonHelpers.cs" />
8283
<Compile Include="Models\Author.cs" />
8384
<Compile Include="Models\Comment.cs" />
85+
<Compile Include="Models\Sample.cs" />
8486
<Compile Include="Models\Post.cs" />
8587
<Compile Include="Properties\AssemblyInfo.cs" />
8688
</ItemGroup>
@@ -92,6 +94,9 @@
9294
</ItemGroup>
9395
<ItemGroup>
9496
<None Include="app.config" />
97+
<None Include="Data\AttributeSerializationTest.json">
98+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
99+
</None>
95100
<None Include="Data\FormatterErrorSerializationTest.json">
96101
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
97102
</None>
@@ -105,7 +110,7 @@
105110
</ItemGroup>
106111
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
107112
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
108-
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
113+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
109114
Other similar extension points exist, see Microsoft.Common.targets.
110115
<Target Name="BeforeBuild">
111116
</Target>

JSONAPI.Tests/Json/JsonApiMediaFormaterTests.cs

Lines changed: 94 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class JsonApiMediaFormaterTests
1919
{
2020
Author a;
2121
Post p, p2, p3, p4;
22+
Sample s1, s2;
2223

2324
private class MockErrorSerializer : IErrorSerializer
2425
{
@@ -96,32 +97,80 @@ public void SetupModels()
9697
}
9798
};
9899

99-
}
100-
101-
private enum TestEnum
102-
{
103-
104-
}
105-
106-
[TestMethod]
107-
public void CanWritePrimitiveTest()
108-
{
109-
// Arrange
110-
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter();
111-
// Act
112-
// Assert
113-
Assert.IsTrue(formatter.CanWriteTypeAsPrimitive(typeof(Int32)), "CanWriteTypeAsPrimitive returned wrong answer for Integer!");
114-
Assert.IsTrue(formatter.CanWriteTypeAsPrimitive(typeof(Double)), "CanWriteTypeAsPrimitive returned wrong answer for Double!");
115-
Assert.IsTrue(formatter.CanWriteTypeAsPrimitive(typeof(DateTime)), "CanWriteTypeAsPrimitive returned wrong answer for DateTime!");
116-
Assert.IsTrue(formatter.CanWriteTypeAsPrimitive(typeof(DateTimeOffset)), "CanWriteTypeAsPrimitive returned wrong answer for DateTimeOffset!");
117-
Assert.IsTrue(formatter.CanWriteTypeAsPrimitive(typeof(Guid)), "CanWriteTypeAsPrimitive returned wrong answer for Guid!");
118-
Assert.IsTrue(formatter.CanWriteTypeAsPrimitive(typeof(String)), "CanWriteTypeAsPrimitive returned wrong answer for String!");
119-
Assert.IsTrue(formatter.CanWriteTypeAsPrimitive(typeof(DateTime?)), "CanWriteTypeAsPrimitive returned wrong answer for nullable DateTime!");
120-
Assert.IsTrue(formatter.CanWriteTypeAsPrimitive(typeof(DateTimeOffset?)), "CanWriteTypeAsPrimitive returned wrong answer for nullable DateTimeOffset!");
121-
Assert.IsTrue(formatter.CanWriteTypeAsPrimitive(typeof(Guid?)), "CanWriteTypeAsPrimitive returned wrong answer for nullable Guid!");
122-
Assert.IsTrue(formatter.CanWriteTypeAsPrimitive(typeof(TestEnum)), "CanWriteTypeAsPrimitive returned wrong answer for enum!");
123-
Assert.IsTrue(formatter.CanWriteTypeAsPrimitive(typeof(TestEnum?)), "CanWriteTypeAsPrimitive returned wrong answer for nullable enum!");
124-
Assert.IsFalse(formatter.CanWriteTypeAsPrimitive(typeof(Object)), "CanWriteTypeAsPrimitive returned wrong answer for Object!");
100+
s1 = new Sample
101+
{
102+
Id = "1",
103+
BooleanField = false,
104+
NullableBooleanField = false,
105+
SByteField = default(SByte),
106+
NullableSByteField = null,
107+
ByteField = default(Byte),
108+
NullableByteField = null,
109+
Int16Field = default(Int16),
110+
NullableInt16Field = null,
111+
UInt16Field = default(UInt16),
112+
NullableUInt16Field = null,
113+
Int32Field = default(Int32),
114+
NullableInt32Field = null,
115+
UInt32Field = default(Int32),
116+
NullableUInt32Field = null,
117+
Int64Field = default(Int64),
118+
NullableInt64Field = null,
119+
UInt64Field = default(UInt64),
120+
NullableUInt64Field = null,
121+
DoubleField = default(Double),
122+
NullableDoubleField = null,
123+
SingleField = default(Single),
124+
NullableSingleField = null,
125+
DecimalField = default(Decimal),
126+
NullableDecimalField = null,
127+
DateTimeField = default(DateTime),
128+
NullableDateTimeField = null,
129+
DateTimeOffsetField = default(DateTimeOffset),
130+
NullableDateTimeOffsetField = null,
131+
GuidField = default(Guid),
132+
NullableGuidField = null,
133+
StringField = default(String),
134+
EnumField = default(SampleEnum),
135+
NullableEnumField = null,
136+
};
137+
s2 = new Sample
138+
{
139+
Id = "2",
140+
BooleanField = true,
141+
NullableBooleanField = true,
142+
SByteField = 123,
143+
NullableSByteField = 123,
144+
ByteField = 253,
145+
NullableByteField = 253,
146+
Int16Field = 32000,
147+
NullableInt16Field = 32000,
148+
UInt16Field = 64000,
149+
NullableUInt16Field = 64000,
150+
Int32Field = 2000000000,
151+
NullableInt32Field = 2000000000,
152+
UInt32Field = 3000000000,
153+
NullableUInt32Field = 3000000000,
154+
Int64Field = 9223372036854775807,
155+
NullableInt64Field = 9223372036854775807,
156+
UInt64Field = 9223372036854775808,
157+
NullableUInt64Field = 9223372036854775808,
158+
DoubleField = 1056789.123,
159+
NullableDoubleField = 1056789.123,
160+
SingleField = 1056789.123f,
161+
NullableSingleField = 1056789.123f,
162+
DecimalField = 1056789.123m,
163+
NullableDecimalField = 1056789.123m,
164+
DateTimeField = new DateTime(1776, 07, 04),
165+
NullableDateTimeField = new DateTime(1776, 07, 04),
166+
DateTimeOffsetField = new DateTimeOffset(new DateTime(1776, 07, 04), new TimeSpan(-5, 0, 0)),
167+
NullableDateTimeOffsetField = new DateTimeOffset(new DateTime(1776, 07, 04), new TimeSpan(-5, 0, 0)),
168+
GuidField = new Guid("6566F9B4-5245-40DE-890D-98B40A4AD656"),
169+
NullableGuidField = new Guid("3D1FB81E-43EE-4D04-AF91-C8A326341293"),
170+
StringField = "Some string 156",
171+
EnumField = SampleEnum.Value1,
172+
NullableEnumField = SampleEnum.Value2,
173+
};
125174
}
126175

127176
[TestMethod]
@@ -174,6 +223,25 @@ public void SerializeArrayIntegrationTest()
174223
//Assert.AreEqual("[2,3,4]", sw.ToString());
175224
}
176225

226+
[TestMethod]
227+
[DeploymentItem(@"Data\AttributeSerializationTest.json")]
228+
public void Serializes_attributes_properly()
229+
{
230+
// Arrang
231+
JsonApiFormatter formatter = new JsonApiFormatter();
232+
formatter.PluralizationService = new JSONAPI.Core.PluralizationService();
233+
MemoryStream stream = new MemoryStream();
234+
235+
// Act
236+
formatter.WriteToStreamAsync(typeof(Sample), new[] { s1, s2 }, stream, null, null);
237+
238+
// Assert
239+
string output = System.Text.Encoding.ASCII.GetString(stream.ToArray());
240+
Trace.WriteLine(output);
241+
var expected = JsonHelpers.MinifyJson(File.ReadAllText("AttributeSerializationTest.json"));
242+
Assert.AreEqual(expected, output.Trim());
243+
}
244+
177245
[TestMethod]
178246
[DeploymentItem(@"Data\FormatterErrorSerializationTest.json")]
179247
public void Should_serialize_error()
@@ -238,7 +306,6 @@ public void DeserializeCollectionIntegrationTest()
238306
// Assert
239307
Assert.AreEqual(2, posts.Count);
240308
Assert.AreEqual(p.Id, posts[0].Id); // Order matters, right?
241-
242309

243310
}
244311

JSONAPI.Tests/Models/Sample.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
3+
namespace JSONAPI.Tests.Models
4+
{
5+
enum SampleEnum
6+
{
7+
Value1 = 1,
8+
Value2 = 2
9+
}
10+
11+
class Sample
12+
{
13+
public string Id { get; set; }
14+
public Boolean BooleanField { get; set; }
15+
public Boolean? NullableBooleanField { get; set; }
16+
public SByte SByteField { get; set; }
17+
public SByte? NullableSByteField { get; set; }
18+
public Byte ByteField { get; set; }
19+
public Byte? NullableByteField { get; set; }
20+
public Int16 Int16Field { get; set; }
21+
public Int16? NullableInt16Field { get; set; }
22+
public UInt16 UInt16Field { get; set; }
23+
public UInt16? NullableUInt16Field { get; set; }
24+
public Int32 Int32Field { get; set; }
25+
public Int32? NullableInt32Field { get; set; }
26+
public UInt32 UInt32Field { get; set; }
27+
public UInt32? NullableUInt32Field { get; set; }
28+
public Int64 Int64Field { get; set; }
29+
public Int64? NullableInt64Field { get; set; }
30+
public UInt64 UInt64Field { get; set; }
31+
public UInt64? NullableUInt64Field { get; set; }
32+
public Double DoubleField { get; set; }
33+
public Double? NullableDoubleField { get; set; }
34+
public Single SingleField { get; set; }
35+
public Single? NullableSingleField { get; set; }
36+
public Decimal DecimalField { get; set; }
37+
public Decimal? NullableDecimalField { get; set; }
38+
public DateTime DateTimeField { get; set; }
39+
public DateTime? NullableDateTimeField { get; set; }
40+
public DateTimeOffset DateTimeOffsetField { get; set; }
41+
public DateTimeOffset? NullableDateTimeOffsetField { get; set; }
42+
public Guid GuidField { get; set; }
43+
public Guid? NullableGuidField { get; set; }
44+
public string StringField { get; set; }
45+
public SampleEnum EnumField { get; set; }
46+
public SampleEnum? NullableEnumField { get; set; }
47+
}
48+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
3+
namespace JSONAPI.Extensions
4+
{
5+
internal static class TypeExtensions
6+
{
7+
internal static bool CanWriteAsJsonApiAttribute(this Type objectType)
8+
{
9+
if (objectType.IsGenericType && objectType.GetGenericTypeDefinition() == typeof (Nullable<>))
10+
objectType = objectType.GetGenericArguments()[0];
11+
12+
return objectType.IsPrimitive
13+
|| typeof (Decimal).IsAssignableFrom(objectType)
14+
|| typeof (Guid).IsAssignableFrom(objectType)
15+
|| typeof (DateTime).IsAssignableFrom(objectType)
16+
|| typeof (DateTimeOffset).IsAssignableFrom(objectType)
17+
|| typeof (String).IsAssignableFrom(objectType)
18+
|| objectType.IsEnum;
19+
}
20+
21+
}
22+
}

JSONAPI/JSONAPI.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
<Compile Include="Core\IPluralizationService.cs" />
7171
<Compile Include="Core\IMaterializer.cs" />
7272
<Compile Include="Core\MetadataManager.cs" />
73+
<Compile Include="Extensions\TypeExtensions.cs" />
7374
<Compile Include="Http\ApiController.cs" />
7475
<Compile Include="Json\ErrorSerializer.cs" />
7576
<Compile Include="Json\GuidErrorIdProvider.cs" />

0 commit comments

Comments
 (0)