Skip to content

A property with a private setter ({ get; private set; }) is saved but loads back as its default, and the next save erases the stored value #362

Closed

Description

@matt-edmondson

What's wrong

System.Text.Json writes a public property whose setter is non-public, but skips it when reading unless the property has [JsonInclude]. Nothing fails, so LoadOrCreate (AppDataStorage/AppData.cs:647-653) archives nothing and returns the default value. The next Save()/QueueSave() then writes that default over the user's stored value.

This is the same silent data loss as #341, but a different kind of member. The fix proposed in #341 (JsonObjectCreationHandling.Populate) only helps get-only collections, so it does not cover this case.

Repro (net10.0, HEAD d6fcd04)

var q = PrivSettings.LoadOrCreateIn(root); q.SetVolume(80); q.Save();
var q2 = PrivSettings.LoadOrCreateIn(root);
Console.WriteLine($"private set reloaded Volume={q2.Volume}");
q2.Save(); Console.WriteLine("after next save file: " + File.ReadAllText(Path.Combine(root, "priv_settings.json")));

public class PrivSettings : AppData<PrivSettings> { public int Volume { get; private set; } = 50; public void SetVolume(int v) => Volume = v; }
public class Inc : AppData<Inc> { [JsonInclude] public int Volume { get; private set; } = 50; public void SetVolume(int v) => Volume = v; }

Observed:

private set reloaded Volume=50          (expected 80)
after next save file: {   "$id": "1",   "Volume": 50 }
[JsonInclude] reloaded Volume=80        (the same class with [JsonInclude] round-trips correctly)

A user who writes the natural encapsulated shape, a public getter with a private setter changed through a method, loses the setting on every restart. Nothing is logged and the corrupt-file archive is never triggered.

Suggested fix

Either:

  • add a DefaultJsonTypeInfoResolver modifier to AppData.JsonSerializerOptions that lets deserialization use the non-public setters of public properties on AppData<T> types, or
  • fail loudly: detect serialized members that can't be deserialized and throw or warn instead of silently returning defaults. Also document [JsonInclude] as required.

Acceptance criteria

  • A round-trip test with a { get; private set; } property passes, or loading that type fails with a clear message instead of silently resetting the value.
  • The existing round-trip tests still pass.

Related: #341 (the same symptom for get-only collections).

Activity

  1. added
    bug
    .NET
    on Nov 2, 2026
  2. matt-edmondson commented on Nov 2, 2026

    @matt-edmondson
    ContributorAuthor

    Triage


    Generated by Claude Code

  3. added a commit that references this issue on Nov 4, 2026
    d937181
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

.NETbug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions