-
Notifications
You must be signed in to change notification settings - Fork 68
fix: keep the precision of high-precision decimals #125
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
Open
ttu
wants to merge
2
commits into
master
Choose a base branch
from
fix-decimal-precision
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| using System; | ||
| using System.Dynamic; | ||
| using System.IO; | ||
| using Newtonsoft.Json; | ||
| using Newtonsoft.Json.Linq; | ||
|
|
||
| namespace JsonFlatFileDataStore; | ||
|
|
||
| /// <summary> | ||
| /// JSON parsing that keeps the precision of non-integral numbers. | ||
| /// | ||
| /// Newtonsoft materializes them as double by default, which silently corrupts any value with more | ||
| /// significant digits than a double can hold — and a value such as decimal.MaxValue is then written | ||
| /// back in exponent notation, after which it no longer deserializes into a decimal property at all. | ||
| /// | ||
| /// Reading numbers as decimal avoids both, but decimal covers a much narrower range than double. | ||
| /// A document holding a value outside that range is parsed the old way instead: too large throws | ||
| /// and is caught, too small would round to zero without any error, so those documents are | ||
| /// recognized from the text before parsing. | ||
| /// </summary> | ||
| internal static class JsonParser | ||
| { | ||
| // Decimal keeps at most 28 digits after the point | ||
| private const int MaxDecimalScale = 28; | ||
|
|
||
| private static readonly string _unsafeSmallLiteral = "0." + new string('0', MaxDecimalScale); | ||
|
|
||
| private static readonly JsonSerializerSettings _decimalSettings = new JsonSerializerSettings | ||
| { FloatParseHandling = FloatParseHandling.Decimal }; | ||
|
|
||
| internal static JObject Parse(string jsonText) | ||
| { | ||
| if (!MayLoseDecimalPrecision(jsonText)) | ||
| { | ||
| try | ||
| { | ||
| return LoadWithDecimalNumbers(jsonText); | ||
| } | ||
| catch (JsonReaderException) | ||
| { | ||
| // Either a number too large for decimal or genuinely invalid JSON. Parsing again | ||
| // with the default handling either succeeds or throws the error the caller expects. | ||
| } | ||
| } | ||
|
|
||
| return JObject.Parse(jsonText); | ||
| } | ||
|
|
||
| internal static ExpandoObject ToExpandoObject(string jsonText) | ||
| { | ||
| if (!MayLoseDecimalPrecision(jsonText)) | ||
| { | ||
| try | ||
| { | ||
| return JsonConvert.DeserializeObject<ExpandoObject>(jsonText, _decimalSettings); | ||
| } | ||
| catch (JsonReaderException) | ||
| { | ||
| } | ||
| } | ||
|
|
||
| return JsonConvert.DeserializeObject<ExpandoObject>(jsonText); | ||
| } | ||
|
|
||
| private static JObject LoadWithDecimalNumbers(string jsonText) | ||
| { | ||
| using (var stringReader = new StringReader(jsonText)) | ||
| using (var jsonReader = new JsonTextReader(stringReader) { FloatParseHandling = FloatParseHandling.Decimal }) | ||
| { | ||
| var jObject = JObject.Load(jsonReader); | ||
|
|
||
| // JObject.Parse rejects trailing content after the object, so do the same | ||
| while (jsonReader.Read()) | ||
| { | ||
| if (jsonReader.TokenType != JsonToken.Comment) | ||
| throw new JsonReaderException("Additional text found in JSON string after parsing content."); | ||
| } | ||
|
|
||
| return jObject; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// True when the text contains a number so small that reading it as a decimal would round it | ||
| /// towards zero. Text inside strings is not skipped: a false positive only means the document | ||
| /// is read the way it always was, whereas a missed number would be silently destroyed. | ||
| /// </summary> | ||
| private static bool MayLoseDecimalPrecision(string jsonText) | ||
| { | ||
| for (var i = 0; i < jsonText.Length - 2; i++) | ||
| { | ||
| var current = jsonText[i]; | ||
|
|
||
| if ((current != 'e' && current != 'E') || jsonText[i + 1] != '-') | ||
| continue; | ||
|
|
||
| var exponent = 0; | ||
| var digitIndex = i + 2; | ||
|
|
||
| for (; digitIndex < jsonText.Length && char.IsDigit(jsonText[digitIndex]); digitIndex++) | ||
| { | ||
| exponent = exponent * 10 + (jsonText[digitIndex] - '0'); | ||
|
|
||
| if (exponent > MaxDecimalScale) | ||
| return true; | ||
| } | ||
|
|
||
| // The exponent alone does not decide it: the digits the mantissa already has after the | ||
| // point shift the value further down. 1e-28 is exact, 1.5e-28 is not. | ||
| if (digitIndex > i + 2 && exponent + CountFractionDigits(jsonText, i) > MaxDecimalScale) | ||
| return true; | ||
| } | ||
|
|
||
| // Same magnitude written without an exponent, which only a hand-authored file contains | ||
| return jsonText.IndexOf(_unsafeSmallLiteral, StringComparison.Ordinal) >= 0; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Number of digits the mantissa ending at <paramref name="exponentIndex"/> has after the point. | ||
| /// </summary> | ||
| private static int CountFractionDigits(string jsonText, int exponentIndex) | ||
| { | ||
| var index = exponentIndex - 1; | ||
|
|
||
| while (index >= 0 && char.IsDigit(jsonText[index])) | ||
| index--; | ||
|
|
||
| return index >= 0 && jsonText[index] == '.' ? exponentIndex - 1 - index : 0; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.