Десериализация JSON в объект – весь объект не десериализуетсяC#

Место общения программистов C#
Ответить Пред. темаСлед. тема
Anonymous
 Десериализация JSON в объект – весь объект не десериализуется

Сообщение Anonymous »


I am receiving and object from a JS calendar via JSInterop in Blazor. I have a C# interface and class that represents the JS object. A few properties are not being deserialized. In Particular the End Property.

Deserialize like so:

var currentEvent = JsonSerializer.Deserialize(eventBeingModified.ToString(), new JsonSerializerOptions() { PropertyNameCaseInsensitive=true}); The incoming JSON:

{ "id": "c7d1fa58-af2d-4b7b-8040-e9c92ad61574", "calendarId": "1", "__cid": 43, "title": "Voluptas molestiae consectetur enim exercitationem reprehenderit error.", "body": "Aliquid iste dignissimos provident sit occaecati. Distinctio itaque maxime cupiditate quae nihil dolorem deleniti aliquid. Qui voluptas et qui.", "isAllday": false, "start": { "tzOffset": -240, "d": { "d": "2024-03-22T01:30:00.000Z" } }, "end": { "tzOffset": -240, "d": { "d": "2024-03-22T04:10:00.000Z" } }, "goingDuration": null, "comingDuration": null, "location": null, "attendees": null, "category": "time", "dueDateClass": "", "recurrenceRule": null, "state": 0, "isVisible": true, "isPending": false, "isFocused": false, "isReadOnly": false, "isPrivate": false, "color": null, "backgroundColor": null, "dragBackgroundColor": null, "borderColor": null, "customStyle": null, "raw": null } The C# Class:

[JsonConverter(typeof(JsonStringEnumMemberConverter))] public enum EventState { [JsonPropertyName("busy")] Busy, [JsonPropertyName("free")] Free } [JsonConverter(typeof(JsonStringEnumMemberConverter))] public enum EventCategory { [JsonPropertyName("milestone")] Milestone, [JsonPropertyName("task")] Task, [JsonPropertyName("allday")] Allday, /// /// /// [JsonPropertyName("time")] Time } /// /// https://nhn.github.io/tui.calendar/latest/EventObject /// public class TUIEvent : IEventObject { public string Id { get; set; } public string CalendarId { get; set; } public string Title { get; set; } public string Body { get; set; } public bool IsAllDay { get; set; } [JsonConverter(typeof(TZDateJsonConverter))] public DateTimeOffset? Start { get; set; } [JsonConverter(typeof(TZDateJsonConverter))] public DateTimeOffset? End { get; set; } public int? GoingDuration { get; set; } public int? ComingDuration { get; set; } public string Location { get; set; } public string[] Attendees { get; set; } public EventCategory Category { get; set; } public string RecurrenceRule { get; set; } public EventState State { get; set; } public bool IsVisible { get; set; } public bool IsPending { get; set; } public bool IsFocused { get; set; } public bool IsReadOnly { get; set; } public bool IsPrivate { get; set; } public string Color { get; set; } public string BackgroundColor { get; set; } public string DragBackgroundColor { get; set; } public string BorderColor { get; set; } public string CustomStyle { get; set; } public string Raw { get; set; } } The TZDateConvertor Read Method:

private const string TZDateFormat = @"yyyy-MM-ddTHH:mm:ss.fffZ"; static byte[] _date = Encoding.UTF8.GetBytes("d"); //"_date" public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { switch (reader.TokenType) { case JsonTokenType.String: // A simple DateTimeOffset string "value" return DateTimeOffset.ParseExact(reader.GetString(), TZDateFormat, CultureInfo.InvariantCulture); case JsonTokenType.StartObject: { // A DateTimeOffset string embedded in an object { "_date" : "value" } DateTimeOffset? value = null; while (reader.Read()) { switch (reader.TokenType) { case JsonTokenType.EndObject: return value.GetValueOrDefault(); case JsonTokenType.PropertyName: var match = reader.ValueTextEquals(_date); if (match) { reader.Read(); while (reader.TokenType != JsonTokenType.PropertyName) { reader.Read(); } reader.Read(); var propValue = reader.GetString(); value = DateTimeOffset.ParseExact(propValue, TZDateFormat, CultureInfo.InvariantCulture); } else reader.Skip(); break; default: throw new JsonException(); } } } break; } throw new JsonException(); } And example of serialized TUIEvent object(turned back to JSON for read-ability:

{ "Id": "7ef281a6-3086-479f-a630-9967e7449db6", "CalendarId": "1", "Title": "Magnam consequatur enim consequatur maiores non dolor.", "Body": "Eaque fuga tempore ab inventore porro iusto. Similique vitae ducimus voluptatem neque adipisci occaecati accusamus. Adipisci ipsum dolore laudantium. Et et quas quisquam velit adipisci et est.", "IsAllDay": false, "Start": "2024-03-10T03:00:00+00:00", "End": null, "GoingDuration": null, "ComingDuration": null, "Location": null, "Attendees": null, "Category": "milestone", "RecurrenceRule": null, "State": "busy", "IsVisible": false, "IsPending": false, "IsFocused": false, "IsReadOnly": false, "IsPrivate": false, "Color": null, "BackgroundColor": null, "DragBackgroundColor": null, "BorderColor": null, "CustomStyle": null, "Raw": null } Why is End,IsVisible, and likely others not being parsed/deserialized into the object??


Источник: https://stackoverflow.com/questions/780 ... serialized
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение
  • Почему System.Version в строке JSON не десериализуется правильно?
    Anonymous » » в форуме C#
    0 Ответы
    25 Просмотры
    Последнее сообщение Anonymous
  • Json не десериализуется
    Anonymous » » в форуме C#
    0 Ответы
    30 Просмотры
    Последнее сообщение Anonymous
  • Json не десериализуется
    Anonymous » » в форуме C#
    0 Ответы
    19 Просмотры
    Последнее сообщение Anonymous
  • Как кортеж сериализуется и десериализуется от JSON?
    Anonymous » » в форуме C#
    0 Ответы
    8 Просмотры
    Последнее сообщение Anonymous
  • Десериализация Json анализирует недействительный объект Json
    Anonymous » » в форуме C#
    0 Ответы
    34 Просмотры
    Последнее сообщение Anonymous

Вернуться в «C#»