Skip to content

Commit

Permalink
Set encoding to UTF8 without BOM (#245)
Browse files Browse the repository at this point in the history
`Encoding.UTF8` will include a UTF8 BOM character, which is invalid according to the [JSON spec RFC7159, Section 8.1](https://tools.ietf.org/html/rfc7159#section-8.1).
This was causing issues with (in particular) JavaScript clients that call `JSON.parse` on the response.
`UTF8Encoding` allows us to specify whether to include the BOM character or not.
  • Loading branch information
developius authored Oct 29, 2020
1 parent ff2ec5f commit 32d34a5
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Saule/Http/JsonApiMediaTypeFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ public override MediaTypeFormatter GetPerRequestFormatterInstance(

private async Task WriteJsonToStream(JToken json, Stream stream)
{
using (var writer = new StreamWriter(stream, Encoding.UTF8, 2048, true))
using (var writer = new StreamWriter(stream, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false), 2048, true))
{
await writer.WriteAsync(json.ToString(Formatting.None, _config.JsonConverters.ToArray()));
}
}
}
}
}

0 comments on commit 32d34a5

Please sign in to comment.