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
12 changes: 12 additions & 0 deletions JSONAPI.Tests/Data/MalformedRawJsonString.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"comments": [
{
"id": "5",
"body": null,
"customData": { },
"links": {
"post": null
}
}
]
}
14 changes: 14 additions & 0 deletions JSONAPI.Tests/Data/ReformatsRawJsonStringWithUnquotedKeys.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"comments": [
{
"id": "5",
"body": null,
"customData": {
"unquotedKey": 5
},
"links": {
"post": null
}
}
]
}
6 changes: 6 additions & 0 deletions JSONAPI.Tests/JSONAPI.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="Data\ReformatsRawJsonStringWithUnquotedKeys.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Data\MalformedRawJsonString.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Data\FormatterErrorSerializationTest.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
38 changes: 38 additions & 0 deletions JSONAPI.Tests/Json/JsonApiMediaFormaterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,44 @@ public void Serializes_attributes_properly()
Assert.AreEqual(expected, output.Trim());
}

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

// Act
var payload = new [] { new Comment { Id = 5, CustomData = "{ unquotedKey: 5 }"}};
formatter.WriteToStreamAsync(typeof(Comment), payload, stream, null, null);

// Assert
var minifiedExpectedJson = JsonHelpers.MinifyJson(File.ReadAllText("ReformatsRawJsonStringWithUnquotedKeys.json"));
string output = System.Text.Encoding.ASCII.GetString(stream.ToArray());
Trace.WriteLine(output);
output.Should().Be(minifiedExpectedJson);
}

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

// Act
var payload = new[] { new Comment { Id = 5, CustomData = "{ x }" } };
formatter.WriteToStreamAsync(typeof(Comment), payload, stream, null, null);

// Assert
var minifiedExpectedJson = JsonHelpers.MinifyJson(File.ReadAllText("MalformedRawJsonString.json"));
string output = System.Text.Encoding.ASCII.GetString(stream.ToArray());
Trace.WriteLine(output);
output.Should().Be(minifiedExpectedJson);
}

[TestMethod]
[DeploymentItem(@"Data\FormatterErrorSerializationTest.json")]
public void Should_serialize_error()
Expand Down
20 changes: 18 additions & 2 deletions JSONAPI/Json/JsonApiFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ internal JsonApiFormatter(IModelManager modelManager, IErrorSerializer errorSeri
_modelManager = modelManager;
_errorSerializer = errorSerializer;
SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/vnd.api+json"));
ValidateRawJsonStrings = true;
}

public bool ValidateRawJsonStrings { get; set; }

[Obsolete("Use ModelManager.PluralizationService instead")]
public IPluralizationService PluralizationService //FIXME: Deprecated, will be removed shortly
{
Expand Down Expand Up @@ -212,8 +215,21 @@ protected void Serialize(object value, Stream writeStream, JsonWriter writer, Js
}
else
{
var minifiedValue = JsonHelpers.MinifyJson((string) propertyValue);
writer.WriteRawValue(minifiedValue);
var json = (string) propertyValue;
if (ValidateRawJsonStrings)
{
try
{
var token = JToken.Parse(json);
json = token.ToString();
}
catch (Exception)
{
json = "{}";
}
}
var valueToSerialize = JsonHelpers.MinifyJson(json);
writer.WriteRawValue(valueToSerialize);
}
}
else
Expand Down