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/DeserializeRawJsonTest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"comments": [
{
"id": "2",
"customData": null
},
{
"id": "4",
"customData": {
"foo": "bar"
}
}
]
}
84 changes: 81 additions & 3 deletions JSONAPI.Tests/Data/SerializerIntegrationTest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,81 @@
{"posts":[{"id":"1","title":"Linkbait!","links":{"comments":["2","3","4"],"author":"1"}},{"id":"2","title":"Rant #1023","links":{"comments":["5"],"author":"1"}},{"id":"3","title":"Polemic in E-flat minor #824","links":{"comments":null,"author":"1"}},{"id":"4","title":"This post has no author.","links":{"comments":null,"author":null}}],"linked":{"comments":[{"id":"2","body":"Nuh uh!","links":{"post":"1"}},{"id":"3","body":"Yeah huh!","links":{"post":"1"}},{"id":"4","body":"Third Reich.","links":{"post":"1"}},{"id":"5","body":"I laughed, I cried!","links":{"post":"2"}}],"authors":[{"id":"1","name":"Jason Hater","links":{"posts":["1","2","3"]}}]}}


{
"posts": [
{
"id": "1",
"title": "Linkbait!",
"links": {
"comments": [ "2", "3", "4" ],
"author": "1"
}
},
{
"id": "2",
"title": "Rant #1023",
"links": {
"comments": [ "5" ],
"author": "1"
}
},
{
"id": "3",
"title": "Polemic in E-flat minor #824",
"links": {
"comments": null,
"author": "1"
}
},
{
"id": "4",
"title": "This post has no author.",
"links": {
"comments": null,
"author": null
}
}
],
"linked": {
"comments": [
{
"id": "2",
"body": "Nuh uh!",
"customData": null,
"links": { "post": "1" }
},
{
"id": "3",
"body": "Yeah huh!",
"customData": null,
"links": {
"post": "1"
}
},
{
"id": "4",
"body": "Third Reich.",
"customData": {
"foo": "bar"
},
"links": {
"post": "1"
}
},
{
"id": "5",
"body": "I laughed, I cried!",
"customData": null,
"links": {
"post": "2"
}
}
],
"authors": [
{
"id": "1",
"name": "Jason Hater",
"links": {
"posts": [ "1", "2", "3" ]
}
}
]
}
}
4 changes: 3 additions & 1 deletion JSONAPI.Tests/JSONAPI.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
<Compile Include="Core\ModelManagerTests.cs" />
<Compile Include="Json\ErrorSerializerTests.cs" />
<Compile Include="Json\JsonApiMediaFormaterTests.cs" />
<Compile Include="Json\JsonHelpers.cs" />
<Compile Include="Json\LinkTemplateTests.cs" />
<Compile Include="Models\Author.cs" />
<Compile Include="Models\Comment.cs" />
Expand All @@ -104,6 +103,9 @@
<None Include="Data\LinkTemplateTest.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Data\DeserializeRawJsonTest.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Data\SerializerIntegrationTest.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
29 changes: 25 additions & 4 deletions JSONAPI.Tests/Json/JsonApiMediaFormaterTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web.Http;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand Down Expand Up @@ -85,7 +86,8 @@ public void SetupModels()
new Comment() {
Id = 4,
Body = "Third Reich.",
Post = p
Post = p,
CustomData = "{ \"foo\": \"bar\" }"
}
};
p2.Comments = new List<Comment> {
Expand Down Expand Up @@ -144,7 +146,8 @@ public void SerializerIntegrationTest()
// Assert
string output = System.Text.Encoding.ASCII.GetString(stream.ToArray());
Trace.WriteLine(output);
Assert.AreEqual(output.Trim(), File.ReadAllText("SerializerIntegrationTest.json").Trim());
var expected = JsonHelpers.MinifyJson(File.ReadAllText("SerializerIntegrationTest.json"));
Assert.AreEqual(expected, output.Trim());
//Assert.AreEqual("[2,3,4]", sw.ToString());
}

Expand All @@ -168,7 +171,8 @@ public void SerializeArrayIntegrationTest()
// Assert
string output = System.Text.Encoding.ASCII.GetString(stream.ToArray());
Trace.WriteLine(output);
Assert.AreEqual(output.Trim(), File.ReadAllText("SerializerIntegrationTest.json").Trim());
var expected = JsonHelpers.MinifyJson(File.ReadAllText("SerializerIntegrationTest.json"));
Assert.AreEqual(expected, output.Trim());
//Assert.AreEqual("[2,3,4]", sw.ToString());
}

Expand Down Expand Up @@ -233,8 +237,25 @@ public void DeserializeCollectionIntegrationTest()
// Assert
Assert.AreEqual(2, posts.Count);
Assert.AreEqual(p.Id, posts[0].Id); // Order matters, right?
}

[TestMethod]
[DeploymentItem(@"Data\DeserializeRawJsonTest.json")]
public async Task DeserializeRawJsonTest()
{
using (var inputStream = File.OpenRead("DeserializeRawJsonTest.json"))
{
// Arrange
var formatter = new JsonApiFormatter(new PluralizationService());

// Act
var comments = ((IEnumerable<Comment>)await formatter.ReadFromStreamAsync(typeof (Comment), inputStream, null, null)).ToArray();

// Assert
Assert.AreEqual(2, comments.Count());
Assert.AreEqual(null, comments[0].CustomData);
Assert.AreEqual("{\"foo\":\"bar\"}", comments[1].CustomData);
}
}

// Issue #1
Expand Down
2 changes: 2 additions & 0 deletions JSONAPI.Tests/Models/Comment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JSONAPI.Attributes;
using JSONAPI.Core;

namespace JSONAPI.Tests.Models
Expand All @@ -11,5 +12,6 @@ class Comment
public int Id { get; set; }
public string Body { get; set; }
public Post Post { get; set; }
[SerializeStringAsRawJson]public string CustomData { get; set; }
}
}
9 changes: 9 additions & 0 deletions JSONAPI/Attributes/SerializeStringAsRawJsonAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace JSONAPI.Attributes
{
[AttributeUsage(AttributeTargets.Property)]
public class SerializeStringAsRawJsonAttribute : Attribute
{
}
}
2 changes: 2 additions & 0 deletions JSONAPI/JSONAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<Compile Include="Attributes\IncludeInPayload.cs" />
<Compile Include="Attributes\LinkTemplate.cs" />
<Compile Include="Attributes\SerializeAs.cs" />
<Compile Include="Attributes\SerializeStringAsRawJsonAttribute.cs" />
<Compile Include="Core\IModelManager.cs" />
<Compile Include="Core\IPluralizationService.cs" />
<Compile Include="Core\IMaterializer.cs" />
Expand All @@ -79,6 +80,7 @@
<Compile Include="Json\IErrorIdProvider.cs" />
<Compile Include="Json\IErrorSerializer.cs" />
<Compile Include="Json\JsonApiFormatter.cs" />
<Compile Include="Json\JsonHelpers.cs" />
<Compile Include="Json\RelationAggregator.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
43 changes: 41 additions & 2 deletions JSONAPI/Json/JsonApiFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,26 @@ protected void Serialize(object value, Stream writeStream, JsonWriter writer, Js

// numbers, strings, dates...
writer.WritePropertyName(_modelManager.GetJsonKeyForProperty(prop));
serializer.Serialize(writer, prop.GetValue(value, null));

var propertyValue = prop.GetValue(value, null);

if (prop.PropertyType == typeof (string) &&
prop.GetCustomAttributes().Any(attr => attr is SerializeStringAsRawJsonAttribute))
{
if (propertyValue == null)
{
writer.WriteNull();
}
else
{
var minifiedValue = JsonHelpers.MinifyJson((string) propertyValue);
writer.WriteRawValue(minifiedValue);
}
}
else
{
serializer.Serialize(writer, propertyValue);
}
}
else
{
Expand Down Expand Up @@ -603,7 +622,27 @@ public object Deserialize(Type objectType, Stream readStream, JsonReader reader,
//TODO: Embedded would be dropped here!
if (!CanWriteTypeAsPrimitive(prop.PropertyType)) continue; // These aren't supposed to be here, they're supposed to be in "links"!

prop.SetValue(retval, DeserializePrimitive(prop.PropertyType, reader), null);
object propVal;
if (prop.PropertyType == typeof (string) &&
prop.GetCustomAttributes().Any(attr => attr is SerializeStringAsRawJsonAttribute))
{
if (reader.TokenType == JsonToken.Null)
{
propVal = null;
}
else
{
var token = JToken.Load(reader);
var rawPropVal = token.ToString();
propVal = JsonHelpers.MinifyJson(rawPropVal);
}
}
else
{
propVal = DeserializePrimitive(prop.PropertyType, reader);
}

prop.SetValue(retval, propVal, null);

// Tell the MetadataManager that we deserialized this property
MetadataManager.Instance.SetMetaForProperty(retval, prop, true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text.RegularExpressions;

namespace JSONAPI.Tests.Json
namespace JSONAPI.Json
{
static class JsonHelpers
{
Expand Down