Skip to content
This repository was archived by the owner on Nov 18, 2017. It is now read-only.

Commit 360cf99

Browse files
Merge pull request #34 from NLog/extra-json-options
extra render options for json.net
2 parents d71a992 + fdcf53d commit 360cf99

1 file changed

Lines changed: 24 additions & 2 deletions

File tree

test/Parser.Tests/JsonNetSerializer.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
34
using System.Linq;
45
using System.Text;
56
using System.Threading.Tasks;
@@ -12,14 +13,35 @@ namespace Parser.Tests
1213
/// </summary>
1314
public class JsonNetSerializer : ISerializer
1415
{
16+
private readonly bool _singleQuote;
17+
private readonly bool _alwaysQuote;
18+
private JsonSerializer _serializer;
1519

16-
public static ISerializer Instance => new JsonNetSerializer();
20+
/// <summary>Initializes a new instance of the <see cref="T:System.Object" /> class.</summary>
21+
public JsonNetSerializer(bool singleQuote, bool alwaysQuote)
22+
{
23+
_singleQuote = singleQuote;
24+
_alwaysQuote = alwaysQuote;
25+
_serializer = new JsonSerializer();
26+
}
27+
28+
29+
public static ISerializer Instance => new JsonNetSerializer(false, false);
1730

1831
#region Implementation of ISerialization
1932

2033
public void SerializeObject(StringBuilder sb, object value, IFormatProvider formatProvider)
2134
{
22-
sb.AppendFormat(formatProvider, "{0}", JsonConvert.SerializeObject(value));
35+
var stringWriter = new StringWriter(sb, formatProvider);
36+
using (var writer = new JsonTextWriter(stringWriter))
37+
{
38+
writer.QuoteName = !_alwaysQuote;
39+
if (_singleQuote)
40+
{
41+
writer.QuoteChar = '\'';
42+
}
43+
_serializer.Serialize(writer, value);
44+
}
2345
}
2446

2547
#endregion

0 commit comments

Comments
 (0)