Skip to content

Commit

Permalink
Fix string output for TomlDateTime and TomlFloat
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Feb 13, 2019
1 parent 07662d7 commit 2a800c7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 30 deletions.
2 changes: 1 addition & 1 deletion ext/toml-test
Submodule toml-test updated 52 files
+7 −2 README.md
+46 −2 json.go
+1 −0 tests/invalid/array-7.toml
+4 −0 tests/invalid/array-of-tables-1.toml
+10 −0 tests/invalid/array-of-tables-2.toml
+1 −0 tests/invalid/bare-key-1.toml
+2 −0 tests/invalid/bare-key-2.toml
+1 −0 tests/invalid/bare-key-3.toml
+1 −0 tests/invalid/int-0-padded.toml
+1 −0 tests/invalid/key-value-pair-1.toml
+3 −0 tests/invalid/multiple-dot-key.toml
+3 −0 tests/invalid/multiple-key.toml
+1 −0 tests/invalid/no-key-name.toml
+3 −0 tests/invalid/non-dec-integers.toml
+ tests/invalid/string-basic-control-1.toml
+1 −0 tests/invalid/string-basic-control-2.toml
+1 −0 tests/invalid/string-basic-control-3.toml
+1 −0 tests/invalid/string-basic-control-4.toml
+ tests/invalid/string-basic-multiline-control-1.toml
+1 −0 tests/invalid/string-basic-multiline-control-2.toml
+1 −0 tests/invalid/string-basic-multiline-control-3.toml
+1 −0 tests/invalid/string-basic-multiline-control-4.toml
+1 −0 tests/invalid/string-basic-multiline-out-of-range-unicode-escape-1.toml
+1 −0 tests/invalid/string-basic-multiline-out-of-range-unicode-escape-2.toml
+1 −0 tests/invalid/string-basic-multiline-unknown-escape.toml
+1 −0 tests/invalid/string-basic-out-of-range-unicode-escape-1.toml
+1 −0 tests/invalid/string-basic-out-of-range-unicode-escape-2.toml
+1 −0 tests/invalid/string-basic-unknown-escape.toml
+ tests/invalid/string-literal-control-1.toml
+1 −0 tests/invalid/string-literal-control-2.toml
+1 −0 tests/invalid/string-literal-control-3.toml
+1 −0 tests/invalid/string-literal-control-4.toml
+ tests/invalid/string-literal-multiline-control-1.toml
+1 −0 tests/invalid/string-literal-multiline-control-2.toml
+1 −0 tests/invalid/string-literal-multiline-control-3.toml
+1 −0 tests/invalid/string-literal-multiline-control-4.toml
+7 −0 tests/invalid/table-1.toml
+7 −0 tests/invalid/table-2.toml
+2 −1 tests/valid/datetime.json
+1 −0 tests/valid/datetime.toml
+21 −0 tests/valid/dotted-keys.json
+5 −0 tests/valid/dotted-keys.toml
+8 −0 tests/valid/infinity-and-nan.json
+7 −0 tests/valid/infinity-and-nan.toml
+3 −0 tests/valid/local-date.json
+1 −0 tests/valid/local-date.toml
+5 −0 tests/valid/local-datetime.json
+3 −0 tests/valid/local-datetime.toml
+4 −0 tests/valid/local-time.json
+3 −0 tests/valid/local-time.toml
+8 −0 tests/valid/non-dec-integers.json
+6 −0 tests/valid/non-dec-integers.toml
47 changes: 18 additions & 29 deletions src/Tomlyn.Tests/ModelHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,32 @@ public static JToken ToJson(TomlObject obj)
{ "value", tomlBoolean.Value.ToString(CultureInfo.InvariantCulture).ToLowerInvariant()}
};
case TomlDateTime tomlDateTime:
string kindStr = "";
switch (tomlDateTime.Kind)
{
case ObjectKind.OffsetDateTime:
kindStr = "datetime";
break;
case ObjectKind.LocalDateTime:
kindStr = "datetime-local";
break;
case ObjectKind.LocalDate:
kindStr = "date";
break;
case ObjectKind.LocalTime:
kindStr = "time";
break;
}
return new JObject
{
{"type", "datetime"},
{"type", kindStr},
{ "value", tomlDateTime.ToString()}
};
case TomlFloat tomlFloat:
return new JObject
{
{"type", "float"},
{ "value", AppendDecimalPoint(tomlFloat.Value.ToString("g16", CultureInfo.InvariantCulture), true)}
{ "value", tomlFloat.ToString()}
};
case TomlInteger tomlInteger:
return new JObject
Expand Down Expand Up @@ -80,32 +96,5 @@ public static JToken ToJson(TomlObject obj)
}
throw new NotSupportedException($"The type element `{obj.GetType()}` is not supported");
}

private static string DateTimeToString(DateTime time)
{
time = time.ToUniversalTime();
if (time.Millisecond == 0) return time.ToString("yyyy-MM-dd'T'HH:mm:ssK", CultureInfo.InvariantCulture);
return time.ToString("yyyy-MM-dd'T'HH:mm:ss.fffK", CultureInfo.InvariantCulture);
}

private static string AppendDecimalPoint(string text, bool hasNaN)
{
for (var i = 0; i < text.Length; i++)
{
var c = text[i];
// Do not append a decimal point if floating point type value
// - is in exponential form, or
// - already has a decimal point
if (c == 'e' || c == 'E' || c == '.')
{
return text;
}
}
// Special cases for floating point type supporting NaN and Infinity
if (hasNaN && (string.Equals(text, "NaN") || text.Contains("Infinity")))
return text;

return text + ".0";
}
}
}
37 changes: 37 additions & 0 deletions src/Tomlyn/Model/TomlFloat.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// Licensed under the BSD-Clause 2 license.
// See license.txt file in the project root for full license information.

using System;
using System.Globalization;

namespace Tomlyn.Model
{
/// <summary>
Expand All @@ -11,5 +15,38 @@ public sealed class TomlFloat : TomlValue<double>
public TomlFloat(double value) : base(ObjectKind.Float, value)
{
}

public override string ToString()
{
if (double.IsNaN(Value))
{
return BitConverter.DoubleToInt64Bits(Value) > 0 ? "+nan" : "nan";
}
if (double.IsPositiveInfinity(Value))
{
return "+inf";
}
if (double.IsNegativeInfinity(Value))
{
return "-inf";
}
return AppendDecimalPoint(Value.ToString("g16", CultureInfo.InvariantCulture));
}

private static string AppendDecimalPoint(string text)
{
for (var i = 0; i < text.Length; i++)
{
var c = text[i];
// Do not append a decimal point if floating point type value
// - is in exponential form, or
// - already has a decimal point
if (c == 'e' || c == 'E' || c == '.')
{
return text;
}
}
return text + ".0";
}
}
}

0 comments on commit 2a800c7

Please sign in to comment.