Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cache float format #270

Merged
merged 1 commit into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 2 additions & 14 deletions src/Argon/JsonConvert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,23 +143,12 @@ public static string ToString(ulong value) =>
public static string ToString(float value) =>
EnsureDecimalPlace(value, value.ToString("R", InvariantCulture));

internal static string ToString(float value, FloatFormatHandling floatFormatHandling, char quoteChar, bool nullable, byte? precision)
internal static string ToString(float value, FloatFormatHandling floatFormatHandling, char quoteChar, bool nullable, string format)
{
var format = GetFormat(precision);
var text = value.ToString(format, InvariantCulture);
return EnsureFloatFormat(value, EnsureDecimalPlace(value, text), floatFormatHandling, quoteChar, nullable);
}

static string GetFormat(byte? precision)
{
if (precision.HasValue)
{
return $"0.{new string('#', precision.Value)}";
}

return "R";
}

static string EnsureFloatFormat(double value, string text, FloatFormatHandling floatFormatHandling, char quoteChar, bool nullable)
{
if (floatFormatHandling == FloatFormatHandling.Symbol ||
Expand Down Expand Up @@ -188,9 +177,8 @@ static string EnsureFloatFormat(double value, string text, FloatFormatHandling f
public static string ToString(double value) =>
EnsureDecimalPlace(value, value.ToString("R", InvariantCulture));

internal static string ToString(double value, FloatFormatHandling floatFormatHandling, char quoteChar, bool nullable, byte? precision)
internal static string ToString(double value, FloatFormatHandling floatFormatHandling, char quoteChar, bool nullable, string format)
{
var format = GetFormat(precision);
var text = value.ToString(format, InvariantCulture);
var ensureDecimalPlace = EnsureDecimalPlace(value, text);
return EnsureFloatFormat(value, ensureDecimalPlace, floatFormatHandling, quoteChar, nullable);
Expand Down
4 changes: 2 additions & 2 deletions src/Argon/JsonTextWriter.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ public override Task WriteValueAsync(double value, Cancel cancel = default)

Task WriteValueAsync(double value, bool nullable, Cancel cancel)
{
var convertedValue = JsonConvert.ToString(value, FloatFormatHandling, QuoteChar, nullable, FloatPrecision);
var convertedValue = JsonConvert.ToString(value, FloatFormatHandling, QuoteChar, nullable, FloatFormat);
return WriteValueInternalAsync(JsonToken.Float, convertedValue, cancel);
}

Expand Down Expand Up @@ -920,7 +920,7 @@ public override Task WriteValueAsync(float value, Cancel cancel = default)
Task WriteValueAsync(float value, bool nullable, Cancel cancel) =>
WriteValueInternalAsync(
JsonToken.Float,
JsonConvert.ToString(value, FloatFormatHandling, QuoteChar, nullable, FloatPrecision),
JsonConvert.ToString(value, FloatFormatHandling, QuoteChar, nullable, FloatFormat),
cancel);

/// <summary>
Expand Down
8 changes: 4 additions & 4 deletions src/Argon/JsonTextWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ public override void WriteValue(ulong value)
public override void WriteValue(float value)
{
InternalWriteValue(JsonToken.Float);
WriteValueInternal(JsonConvert.ToString(value, FloatFormatHandling, QuoteChar, false, FloatPrecision));
WriteValueInternal(JsonConvert.ToString(value, FloatFormatHandling, QuoteChar, false, FloatFormat));
}

/// <summary>
Expand All @@ -340,7 +340,7 @@ public override void WriteValue(float? value)
else
{
InternalWriteValue(JsonToken.Float);
WriteValueInternal(JsonConvert.ToString(value.GetValueOrDefault(), FloatFormatHandling, QuoteChar, true, FloatPrecision));
WriteValueInternal(JsonConvert.ToString(value.GetValueOrDefault(), FloatFormatHandling, QuoteChar, true, FloatFormat));
}
}

Expand All @@ -350,7 +350,7 @@ public override void WriteValue(float? value)
public override void WriteValue(double value)
{
InternalWriteValue(JsonToken.Float);
WriteValueInternal(JsonConvert.ToString(value, FloatFormatHandling, QuoteChar, false, FloatPrecision));
WriteValueInternal(JsonConvert.ToString(value, FloatFormatHandling, QuoteChar, false, FloatFormat));
}

/// <summary>
Expand All @@ -365,7 +365,7 @@ public override void WriteValue(double? value)
else
{
InternalWriteValue(JsonToken.Float);
WriteValueInternal(JsonConvert.ToString(value.GetValueOrDefault(), FloatFormatHandling, QuoteChar, true, FloatPrecision));
WriteValueInternal(JsonConvert.ToString(value.GetValueOrDefault(), FloatFormatHandling, QuoteChar, true, FloatFormat));
}
}

Expand Down
21 changes: 20 additions & 1 deletion src/Argon/JsonWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,29 @@ protected virtual void OnEscapeHandlingChanged()
/// </summary>
public FloatFormatHandling FloatFormatHandling { get; set; }

byte? floatPrecision;

/// <summary>
/// Gets or sets how many decimal points to use when serializing floats and doubles.
/// </summary>
public byte? FloatPrecision { get; set; }
public byte? FloatPrecision
{
get => floatPrecision;
set
{
floatPrecision = value;
if (floatPrecision.HasValue)
{
FloatFormat = $"0.{new string('#', floatPrecision.Value)}";
}
else
{
FloatFormat = "R";
}
}
}

protected string FloatFormat { get; private set; } = "R";

/// <summary>
/// Initializes a new instance of the <see cref="JsonWriter" /> class.
Expand Down