JSON Deserialization behaviour regarding missing properties

MarkD 21 Reputation points
2021-02-26T16:34:22.63+00:00

Hi,

I'm using System.Text.Json.JsonSerializer.Serialize in a .NET 5 C# application to save the application state to a json file.
The application uses JsonSerializer.Deserialize<MyClass>(jsonString) to read the state back, which works fine. I'm however looking for the default behaviour of the deserialize method after adjusting MyClass by adding or removing properties. I looked in the documentation for the default deserialization behaviour but it doesn't mention what happens to missing properties or properties which are deprecated. I also found this information about Version tolerant serialization in binary serialization.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,521 questions
0 comments No comments
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,576 Reputation points
    2021-03-01T05:40:00.07+00:00

    It seems that there is no need to do additional things.

    If there is a property in the class that is not in json, then it will be the default value of that data type. If there is a property in json that is not in the class, then this property will be ignored.

    Have you encountered any errors? Or am I misunderstanding what you mean?


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful