JamesNK / Newtonsoft.Json

Json.NET is a popular high-performance JSON framework for .NET
https://www.newtonsoft.com/json
MIT License
10.81k stars 3.26k forks source link

v13.0.2 not preserving trailing zeros when deserializing decimals with 0 #2786

Open Alperen2558 opened 1 year ago

Alperen2558 commented 1 year ago

Source/destination types


public sealed class Invoice
    {
        public Guid Id { get; set; }

        public string? InvoiceNumber { get; set; }

        public List<InvoiceLine>? InvoiceLines { get; set; }
    }

public sealed class InvoiceLine
    {
        public Guid Id { get; set; }

        public decimal VatAmount { get; set; }

        public decimal NetAmount { get; set; }

        public decimal TotalAmount { get; set; }
    }

Source/destination JSON


{
  "id": "d28888e9-2ba9-473a-a40f-e38cb54f9c25",
  "invoiceLines": [
    {
      "id": "b1bc25c7-0a67-405e-b82a-b39f63d6c1ad",
      "vatAmount": 0.00,
      "netAmount": 90.00,
      "totalAmount": 100.00
    },
    {
      "d": "3cdce072-1824-4c9e-8bfd-c9fd4e739f5a",
      "vatAmount": 15.00,
      "netAmount": 85.00,
      "totalAmount": 100.00
    }
  ]
}

Expected behavior

line1-> "vatAmount": 0.00, should have been serialized as "0.00"

Actual behavior

line1-> "vatAmount": 0.00, have been serialized as "0"

Steps to reproduce

public class TrailingZerosTests
    {
        [Fact]
        public void DeserializeObject_WhenSomeZerosOnADecimalValue_ShouldPreserveTrailingZeros()
        {
            var invoiceJson = File.ReadAllText("invoice.json");

            var invoice = JsonConvert.DeserializeObject<Invoice>(invoiceJson);

            invoice.Should().NotBeNull();
            invoice?.InvoiceLines?[0].VatAmount.ToString(CultureInfo.InvariantCulture).Should().Be("0.00");
        }
    }
elgonzo commented 1 year ago

Duplicate of https://github.com/JamesNK/Newtonsoft.Json/issues/2768.

The author of Newtonsoft.Json has already committed a fix to the repository, and should likely land with the next Newtonsoft.Json version...

Alperen2558 commented 1 year ago

Hi again @elgonzo. When is the next planned release for version 13.0.3? We are a bit dependent on this fix.

elgonzo commented 1 year ago

@Alperen2558 i can't tell because i am not associated nor involved in any way with the Newtonsoft.Json project. That's a question only the author of Newtonsoft.Json can give an answer to...

Alperen2558 commented 1 year ago

Hi @JamesNK. Do you have any rough estimation of the time for your next release for version 13.0.3?

JamesNK commented 1 year ago

13.0.3-beta1 is already on NuGet. You can use it now. If you require a final version then I don't have an exact date. Maybe in a few weeks?