11using System ;
22using System . Collections . Generic ;
3+ using System . IO ;
34using System . Linq ;
45using System . Text ;
56using 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