JamesNK / Newtonsoft.Json

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

Different DateTimeOffset deserialization on Mac and Linux #2487

Closed gbreen12 closed 3 years ago

gbreen12 commented 3 years ago

I'm running a .Net 5 Web API and I'm seeing different values when deserializing date time offsets.

Source/destination JSON

{
    "Date": "03/09/2021"
}

Expected behavior

Deserialize with local offset (or ability to specify via settings)

Actual behavior

On Mac, it is deserializing to my local offset but on linux (via Docker) it is deserializing with a 0 offset.

Steps to reproduce

Here is my docker file:

FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build
WORKDIR /app

COPY . .
RUN dotnet restore Cuwcd.WebApi.sln
RUN dotnet publish Cuwcd.WebApi.sln -c Release -o out

FROM mcr.microsoft.com/dotnet/aspnet:5.0
RUN apt-get update && apt-get install -y libgdiplus
WORKDIR /app
COPY --from=build /app/out ./
ENTRYPOINT ["dotnet", "Cuwcd.WebApi.dll"]

Here is my C# setup:

var mvc = services.AddMvc();//.SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
mvc.AddMvcOptions(options => { options.EnableEndpointRouting = false; });
mvc.AddNewtonsoftJson(options =>
{
    options.SerializerSettings.ContractResolver = new DeltaContractResolver(new JsonMediaTypeFormatter());
    options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
    options.SerializerSettings.Converters.Add(new StringEnumConverter());
    options.UseMemberCasing();
});

I've tried setting the DateParseHandling and the DateTimeZoneHandling but neither fixed the issue.

gbreen12 commented 3 years ago

I created a simple web api project to show the issue:

https://github.com/gbreen12/Json.Net-Deserialization

Run locally on Mac to see the local offset, run in docker to see 0 offset

gbreen12 commented 3 years ago

Turns out I have to set the timezone on the docker container. That fixed the issue.