Skip to content

Commit 06d55f8

Browse files
committed
Byte Id Serialization
This pull request allows to have Id of type byte. They will be serialized properly.
1 parent e78e0f7 commit 06d55f8

5 files changed

Lines changed: 64 additions & 2 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"tags": [
3+
{
4+
"id": "1",
5+
"text": "Ember"
6+
}, {
7+
"id": "2",
8+
"text": "React"
9+
}, {
10+
"id": "3",
11+
"text": "Angular"
12+
}
13+
]
14+
}

JSONAPI.Tests/JSONAPI.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
<Compile Include="Json\LinkTemplateTests.cs" />
8585
<Compile Include="Models\Author.cs" />
8686
<Compile Include="Models\Comment.cs" />
87+
<Compile Include="Models\Tag.cs" />
8788
<Compile Include="Models\Sample.cs" />
8889
<Compile Include="Models\Post.cs" />
8990
<Compile Include="Properties\AssemblyInfo.cs" />
@@ -96,6 +97,9 @@
9697
</ItemGroup>
9798
<ItemGroup>
9899
<None Include="app.config" />
100+
<None Include="Data\ByteIdSerializationTest.json">
101+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
102+
</None>
99103
<None Include="Data\ReformatsRawJsonStringWithUnquotedKeys.json">
100104
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
101105
</None>

JSONAPI.Tests/Json/JsonApiMediaFormaterTests.cs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class JsonApiMediaFormaterTests
2121
Author a;
2222
Post p, p2, p3, p4;
2323
Sample s1, s2;
24+
Tag t1, t2, t3;
2425

2526
private class MockErrorSerializer : IErrorSerializer
2627
{
@@ -54,6 +55,19 @@ public void SetupModels()
5455
Name = "Jason Hater",
5556
};
5657

58+
t1 = new Tag {
59+
Id = 1,
60+
Text = "Ember"
61+
};
62+
t2 = new Tag {
63+
Id = 2,
64+
Text = "React"
65+
};
66+
t3 = new Tag {
67+
Id = 3,
68+
Text = "Angular"
69+
};
70+
5771
p = new Post()
5872
{
5973
Id = 1,
@@ -234,8 +248,7 @@ public void SerializeArrayIntegrationTest()
234248

235249
[TestMethod]
236250
[DeploymentItem(@"Data\AttributeSerializationTest.json")]
237-
public void Serializes_attributes_properly()
238-
{
251+
public void Serializes_attributes_properly() {
239252
// Arrang
240253
JsonApiFormatter formatter = new JsonApiFormatter(new PluralizationService());
241254
MemoryStream stream = new MemoryStream();
@@ -250,6 +263,23 @@ public void Serializes_attributes_properly()
250263
Assert.AreEqual(expected, output.Trim());
251264
}
252265

266+
[TestMethod]
267+
[DeploymentItem(@"Data\ByteIdSerializationTest.json")]
268+
public void Serializes_byte_ids_properly() {
269+
// Arrang
270+
JsonApiFormatter formatter = new JsonApiFormatter(new PluralizationService());
271+
MemoryStream stream = new MemoryStream();
272+
273+
// Act
274+
formatter.WriteToStreamAsync(typeof(Tag), new[] { t1, t2, t3 }, stream, null, null);
275+
276+
// Assert
277+
string output = System.Text.Encoding.ASCII.GetString(stream.ToArray());
278+
Trace.WriteLine(output);
279+
var expected = JsonHelpers.MinifyJson(File.ReadAllText("ByteIdSerializationTest.json"));
280+
Assert.AreEqual(expected, output.Trim());
281+
}
282+
253283
[TestMethod]
254284
[DeploymentItem(@"Data\ReformatsRawJsonStringWithUnquotedKeys.json")]
255285
public void Reformats_raw_json_string_with_unquoted_keys()

JSONAPI.Tests/Models/Tag.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace JSONAPI.Tests.Models {
8+
class Tag {
9+
public byte Id { get; set; }
10+
public string Text { get; set; }
11+
}
12+
}

JSONAPI/Json/JsonApiFormatter.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,8 @@ protected string GetValueForIdProperty(PropertyInfo idprop, object obj)
890890
return idprop.GetValue(obj).ToString();
891891
if (idprop.PropertyType == typeof(int))
892892
return ((int)idprop.GetValue(obj, null)).ToString();
893+
if (idprop.PropertyType == typeof(byte))
894+
return ((byte)idprop.GetValue(