Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions JSONAPI.Tests/Data/ByteIdSerializationTest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"tags": [
{
"id": "1",
"text": "Ember"
}, {
"id": "2",
"text": "React"
}, {
"id": "3",
"text": "Angular"
}
]
}
4 changes: 4 additions & 0 deletions JSONAPI.Tests/JSONAPI.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
<Compile Include="Json\LinkTemplateTests.cs" />
<Compile Include="Models\Author.cs" />
<Compile Include="Models\Comment.cs" />
<Compile Include="Models\Tag.cs" />
<Compile Include="Models\Sample.cs" />
<Compile Include="Models\Post.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand All @@ -96,6 +97,9 @@
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="Data\ByteIdSerializationTest.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Data\ReformatsRawJsonStringWithUnquotedKeys.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
37 changes: 36 additions & 1 deletion JSONAPI.Tests/Json/JsonApiMediaFormaterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class JsonApiMediaFormaterTests
Author a;
Post p, p2, p3, p4;
Sample s1, s2;
Tag t1, t2, t3;

private class MockErrorSerializer : IErrorSerializer
{
Expand Down Expand Up @@ -54,6 +55,22 @@ public void SetupModels()
Name = "Jason Hater",
};

t1 = new Tag
{
Id = 1,
Text = "Ember"
};
t2 = new Tag
{
Id = 2,
Text = "React"
};
t3 = new Tag
{
Id = 3,
Text = "Angular"
};

p = new Post()
{
Id = 1,
Expand Down Expand Up @@ -234,7 +251,7 @@ public void SerializeArrayIntegrationTest()

[TestMethod]
[DeploymentItem(@"Data\AttributeSerializationTest.json")]
public void Serializes_attributes_properly()
public void Serializes_attributes_properly()
{
// Arrang
JsonApiFormatter formatter = new JsonApiFormatter(new PluralizationService());
Expand All @@ -250,6 +267,24 @@ public void Serializes_attributes_properly()
Assert.AreEqual(expected, output.Trim());
}

[TestMethod]
[DeploymentItem(@"Data\ByteIdSerializationTest.json")]
public void Serializes_byte_ids_properly()
{
// Arrang
JsonApiFormatter formatter = new JsonApiFormatter(new PluralizationService());
MemoryStream stream = new MemoryStream();

// Act
formatter.WriteToStreamAsync(typeof(Tag), new[] { t1, t2, t3 }, stream, null, null);

// Assert
string output = System.Text.Encoding.ASCII.GetString(stream.ToArray());
Trace.WriteLine(output);
var expected = JsonHelpers.MinifyJson(File.ReadAllText("ByteIdSerializationTest.json"));
Assert.AreEqual(expected, output.Trim());
}

[TestMethod]
[DeploymentItem(@"Data\ReformatsRawJsonStringWithUnquotedKeys.json")]
public void Reformats_raw_json_string_with_unquoted_keys()
Expand Down
14 changes: 14 additions & 0 deletions JSONAPI.Tests/Models/Tag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JSONAPI.Tests.Models
{
class Tag
{
public byte Id { get; set; }
public string Text { get; set; }
}
}
2 changes: 2 additions & 0 deletions JSONAPI/Json/JsonApiFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,8 @@ protected string GetValueForIdProperty(PropertyInfo idprop, object obj)
return idprop.GetValue(obj).ToString();
if (idprop.PropertyType == typeof(int))
return ((int)idprop.GetValue(obj, null)).ToString();
if (idprop.PropertyType == typeof(byte))
return ((byte)idprop.GetValue(obj, null)).ToString();
}
return "NOIDCOMPUTABLE!";
}
Expand Down