abpframework / abp

Open-source web application framework for ASP.NET Core! Offers an opinionated architecture to build enterprise software solutions with best practices on top of the .NET. Provides the fundamental infrastructure, cross-cutting-concern implementations, startup templates, application modules, UI themes, tooling and documentation.
https://abp.io
GNU Lesser General Public License v3.0
12.78k stars 3.41k forks source link

Don't replace the System.Text.Json with Newtonsoft.Json #10549

Closed diennttlu closed 2 years ago

diennttlu commented 2 years ago

When I am trying to deserealize data from body json can't convert DateTime image

image

I've added services.AddControllers().AddNewtonsoftJson(); to Startup.cs and it doesn't change anything

Abp Framework v4.4.3

maliming commented 2 years ago

See https://docs.abp.io/en/abp/latest/JSON

diennttlu commented 2 years ago

I am creating an Api for Telegram bot to call WebhookController: image

Data form body: { "date": 1636083705, }

The "date" property has a unixtimestamp data type. When sending request it cannot convert to DateTime data type. I've added services.AddControllers().AddNewtonsoftJson(); to replace the System.Text.Json with Newtonsoft.Json. I tried it on a simple .net-core web app and it worked fine. On Abp Framework it throws an exception "The JSON value could not be counverted to System.DateTime".

qwinmen commented 2 years ago

Возможно кому-либо поможет, столкнулся с этой же проблемой, подружить abp и telegram бота удалось так: (Maybe i can help someone, get this trouble. My decision: )

Configure<AbpSystemTextJsonSerializerOptions>(options =>
{
    //AbpHybridJsonInputFormatter.SystemTextJsonInputFormatter не может обработать атрибуты модели Telegram.Bot.Types.Update, используем NewtonsoftJsonInputFormatter:
    options.UnsupportedTypes.Add(typeof(Update));
});

Configure<MvcNewtonsoftJsonOptions>(options =>
{
    //Используем дефолтную Newtonsoft реализацию вместо AbpMvcJsonContractResolver:
    options.SerializerSettings.ContractResolver = new DefaultContractResolver();
});
Y2zz commented 2 years ago

If using .NET6 JToken should be changed to JsonNode https://zhuanlan.zhihu.com/p/388545891