Mercurial > pub > ImplabNet
comparison Implab/JSON/JSONWriter.cs @ 142:2100965eb97f v2
fixed JSONWriter handling Infinity, NaN and locale aware number formatting
author | cin |
---|---|
date | Wed, 04 Mar 2015 03:10:38 +0300 |
parents | 0fa293bb1351 |
children | 3258399cba83 |
comparison
equal
deleted
inserted
replaced
141:0fa293bb1351 | 142:2100965eb97f |
---|---|
1 using System; | 1 using System; |
2 using System.Collections.Generic; | 2 using System.Collections.Generic; |
3 using System.IO; | 3 using System.IO; |
4 using System.Globalization; | |
4 | 5 |
5 namespace Implab.JSON { | 6 namespace Implab.JSON { |
6 public class JSONWriter { | 7 public class JSONWriter { |
7 struct Context { | 8 struct Context { |
8 public bool needComma; | 9 public bool needComma; |
33 _escapeQ = "\\\"".ToCharArray(); | 34 _escapeQ = "\\\"".ToCharArray(); |
34 } | 35 } |
35 | 36 |
36 public JSONWriter(TextWriter writer) { | 37 public JSONWriter(TextWriter writer) { |
37 Safe.ArgumentNotNull(writer, "writer"); | 38 Safe.ArgumentNotNull(writer, "writer"); |
38 | |
39 m_writer = writer; | 39 m_writer = writer; |
40 } | 40 } |
41 | 41 |
42 public JSONWriter(TextWriter writer, bool indent) { | 42 public JSONWriter(TextWriter writer, bool indent) { |
43 Safe.ArgumentNotNull(writer, "writer"); | 43 Safe.ArgumentNotNull(writer, "writer"); |
260 | 260 |
261 m_writer.Write('"'); | 261 m_writer.Write('"'); |
262 } | 262 } |
263 | 263 |
264 void Write(double value) { | 264 void Write(double value) { |
265 m_writer.Write(value); | 265 if (double.IsNaN(value)) |
266 Write("NaN"); | |
267 else if (double.IsNegativeInfinity(value)) | |
268 Write("-Infinity"); | |
269 else if (double.IsPositiveInfinity(value)) | |
270 Write("Infinity"); | |
271 else | |
272 m_writer.Write(value.ToString(CultureInfo.InvariantCulture)); | |
266 } | 273 } |
267 | 274 |
268 void OperationNotApplicable(string opName) { | 275 void OperationNotApplicable(string opName) { |
269 throw new InvalidOperationException(String.Format("The operation '{0}' isn't applicable in the context of '{1}'", opName, m_context.element )); | 276 throw new InvalidOperationException(String.Format("The operation '{0}' isn't applicable in the context of '{1}'", opName, m_context.element )); |
270 } | 277 } |