Skip to content

Fix JSON serialization of quantities with decimal values - #868

Merged
angularsen merged 1 commit into
angularsen:masterfrom
rohahn:decimal-serialization
Dec 17, 2020
Merged

Fix JSON serialization of quantities with decimal values#868
angularsen merged 1 commit into
angularsen:masterfrom
rohahn:decimal-serialization

Conversation

@rohahn

Copy link
Copy Markdown
Contributor

Add IDecimalQuantity interface to expose the decimal value
Serialize decimal values as string to keep number of decimal places

@codecov

codecovBot commented Dec 15, 2020

Copy link
Copy Markdown

Codecov Report

Merging #868 (e9a8161) into master (4993a2a) will decrease coverage by 0.35%.
The diff coverage is 100.00%.

Impacted file tree graph

@@ Coverage Diff @@## master #868 +/- ##
==========================================
- Coverage 83.09% 82.74% -0.36% 
==========================================
Files 287 287 Lines 42987 43332 +345 ==========================================
+ Hits 35720 35853 +133 - Misses 7267 7479 +212 
Impacted FilesCoverage Δ
...Serialization.JsonNet/UnitsNetBaseJsonConverter.cs100.00% <100.00%> (ø)
UnitsNet/GeneratedCode/Quantities/BitRate.g.cs85.43% <100.00%> (+0.09%)⬆️
UnitsNet/GeneratedCode/Quantities/Information.g.cs86.09% <100.00%> (+0.09%)⬆️
UnitsNet/GeneratedCode/Quantities/Power.g.cs85.20% <100.00%> (+0.10%)⬆️
UnitsNet/GeneratedCode/Quantity.g.cs52.00% <0.00%> (-29.41%)⬇️
UnitsNet/CustomCode/Quantity.cs81.08% <0.00%> (-3.13%)⬇️
UnitsNet/QuantityTypeConverter.cs98.38% <0.00%> (-0.03%)⬇️
UnitsNet/GeneratedCode/Quantities/Length.g.cs94.00% <0.00%> (+0.02%)⬆️
UnitsNet/GeneratedCode/Quantities/VolumeFlow.g.cs90.01% <0.00%> (+0.03%)⬆️
... and 102 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4993a2a...e9a8161. Read the comment docs.

@angularsen

Copy link
Copy Markdown
Owner

Solid work on this 👏 Love the tests!

I think it is clever to use string to support decimal, but I'm concerned we are breaking backwards compatibility on the JSON schema for those expecting to read Value as a number.

How about this:

  • Keep Unit and Value exactly as before, the double representation of the number matching our common public interface for quantities.
  • If value is not double, then add two more fields
    • ValueString with textual representation of number
    • ValueType with string values like "decimal", "float", "SomeLibrary.BigInt"

This makes the schema more extensible to other types of numbers with generics, as have been discussed a number of times:
#180
#285
#666
#714

@rohahn
rohahnforce-pushed the decimal-serialization branch from 75665e8 to c5af7a1CompareDecember 17, 2020 15:56
@rohahn

Copy link
Copy Markdown
ContributorAuthor

I implemented your proposal.

Add IDecimalQuantity interface to expose the decimal value
Serialize decimal values as string to keep number of decimal places
@rohahn
rohahnforce-pushed the decimal-serialization branch from c5af7a1 to e9a8161CompareDecember 17, 2020 17:14

@angularsenangularsen left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks perfect to me, great work on this!

Comment threadUnitsNet.Serialization.JsonNet/UnitsNetBaseJsonConverter.cs
@angularsen
angularsen merged commit 25b6558 into angularsen:masterDec 17, 2020
@angularsen

Copy link
Copy Markdown
Owner

Nuget is on the way out, I'd appreciate it if you gave it a spin to ensure it works as expected and comment back here! 🙇
Release JsonNet/4.3.0 · angularsen/UnitsNet

@rohahn

Copy link
Copy Markdown
ContributorAuthor

Why is the version number 4.3.0?

@angularsen

Copy link
Copy Markdown
Owner

JsonNet nuget is versioned separately from UnitsNet nuget, but they share major version.

angularsen added a commit that referenced this pull request Mar 19, 2021
Fixes parsing JSON for decimal quantities like Power on machines with cultures like Norwegian, where a comma is decimal separator is used.
Related to #847, #868
angularsen added a commit that referenced this pull request Mar 19, 2021
Fixes parsing JSON for decimal quantities like Power on machines with cultures like Norwegian, where a comma is decimal separator is used.
Related to #847, #868
angularsen added a commit that referenced this pull request Apr 4, 2021
Fixes parsing JSON for decimal quantities like Power on machines with cultures like Norwegian, where a comma is decimal separator is used.
Related to #847, #868
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@rohahn@angularsen