|
2 | 2 | using System.Collections.Generic; |
3 | 3 | using System.Collections.Specialized; |
4 | 4 | using System.Diagnostics; |
| 5 | +using System.Globalization; |
5 | 6 | using NUnit.Framework; |
6 | 7 | using ServiceStack.Text.Tests.DynamicModels.DataModel; |
7 | 8 |
|
@@ -279,19 +280,57 @@ public NumericType(decimal min,decimal max,Type type) |
279 | 280 | public Type Type { get; private set; } |
280 | 281 | } |
281 | 282 |
|
282 | | - [Test] |
283 | | - public void deserizes_signed_bytes_into_to_best_fit_numeric() |
284 | | - { |
285 | | - JsConfig.TryToParsePrimitiveTypeValues = true; |
286 | | - JsConfig.TryToParseNumericType = true; |
| 283 | + [Test] |
| 284 | + public void deserizes_signed_bytes_into_to_best_fit_numeric() |
| 285 | + { |
| 286 | + JsConfig.TryToParsePrimitiveTypeValues = true; |
| 287 | + JsConfig.TryToParseNumericType = true; |
| 288 | + |
| 289 | + var deserializedDict = JsonSerializer.DeserializeFromString<IDictionary<string, object>>("{\"min\":-128,\"max\":127}"); |
| 290 | + Assert.That(deserializedDict["min"], Is.TypeOf<sbyte>()); |
| 291 | + Assert.That(deserializedDict["min"], Is.EqualTo(sbyte.MinValue)); |
| 292 | + //it seemed strange having zero return as a signed byte |
| 293 | + Assert.That(deserializedDict["max"], Is.TypeOf<byte>()); |
| 294 | + Assert.That(deserializedDict["max"], Is.EqualTo(sbyte.MaxValue)); |
| 295 | + } |
287 | 296 |
|
288 | | - var deserializedDict = JsonSerializer.DeserializeFromString<IDictionary<string, object>>("{\"min\":-128,\"max\":127}"); |
289 | | - Assert.That(deserializedDict["min"], Is.TypeOf<sbyte>()); |
290 | | - Assert.That(deserializedDict["min"], Is.EqualTo(sbyte.MinValue)); |
291 | | - //it seemed strange having zero return as a signed byte |
292 | | - Assert.That(deserializedDict["max"], Is.TypeOf<byte>()); |
293 | | - Assert.That(deserializedDict["max"], Is.EqualTo(sbyte.MaxValue)); |
294 | | - } |
| 297 | + [Test] |
| 298 | + public void deserizes_floats_into_to_best_fit_floating_point() |
| 299 | + { |
| 300 | + JsConfig.TryToParsePrimitiveTypeValues = true; |
| 301 | + JsConfig.TryToParseNumericType = true; |
| 302 | + var decimalFmt = "n50"; |
| 303 | + |
| 304 | + float floatValue = 1.1f; |
| 305 | + //TODO find a number that doesn't suck which throws in float.Parse() but not double.Parse() |
| 306 | + double doubleValue = double.MaxValue - Math.Pow(2, 1000); |
| 307 | + var intValue = int.MaxValue; |
| 308 | + var longValue = long.MaxValue; |
| 309 | + |
| 310 | + float notFloat; |
| 311 | + Assert.That(!float.TryParse(doubleValue.ToString(), out notFloat)); |
| 312 | + |
| 313 | + var toFloatValue = float.Parse(floatValue.ToString()); |
| 314 | + Assert.AreEqual(toFloatValue, floatValue, 1); |
| 315 | + var toDoubleValue = double.Parse(doubleValue.ToString()); |
| 316 | + Assert.AreEqual(toDoubleValue, doubleValue, Math.Pow(2, 1000)); |
| 317 | + |
| 318 | + var json = "{{\"float\":{0},\"double\":{1},\"int\":{2},\"long\":{3}}}" |
| 319 | + .Fmt(floatValue, doubleValue, intValue, longValue); |
| 320 | + var map = JsonSerializer.DeserializeFromString<IDictionary<string, object>>(json); |
| 321 | + |
| 322 | + Assert.That(map["float"], Is.TypeOf<float>()); |
| 323 | + Assert.That(map["float"], Is.EqualTo(floatValue)); |
| 324 | + |
| 325 | + Assert.That(map["double"], Is.TypeOf<double>()); |
| 326 | + Assert.AreEqual((double)map["double"], doubleValue, Math.Pow(2, 1000)); |
| 327 | + |
| 328 | + Assert.That(map["int"], Is.TypeOf<int>()); |
| 329 | + Assert.That(map["int"], Is.EqualTo(intValue)); |
| 330 | + |
| 331 | + Assert.That(map["long"], Is.TypeOf<long>()); |
| 332 | + Assert.That(map["long"], Is.EqualTo(longValue)); |
| 333 | + } |
295 | 334 |
|
296 | 335 | [Test] |
297 | 336 | public void deserizes_signed_types_into_to_best_fit_numeric() |
|
0 commit comments