Skip to content

LoadStateAsync with JsonFilePersistenceProvider always throws InvalidOperationException, so saved navigation can never be restored #53

Closed

Description

@matt-edmondson

What's wrong

NavigationState<T> (Navigation/Models/NavigationState.cs, ~line 21) has the constructor NavigationState(IEnumerable<T> items, int currentIndex), but the matching property is IReadOnlyList<T> Items. System.Text.Json requires a constructor parameter to have the same type as the property it binds to, so JsonSerializer.Deserialize<NavigationState<T>> throws InvalidOperationException.

JsonFilePersistenceProvider.LoadStateAsync (Navigation/Services/JsonFilePersistenceProvider.cs, ~lines 68-70) only catches JsonException and IOException, so the exception reaches the caller. No test performs a JSON load.

Repro

var provider = new JsonFilePersistenceProvider<NavigationItem>(file);
var nav = new Navigation<NavigationItem>(null, provider);
nav.NavigateTo(new NavigationItem("a", "A"));
nav.NavigateTo(new NavigationItem("b", "B"));
nav.GoBack();
await nav.SaveStateAsync();

var nav2 = new Navigation<NavigationItem>(null, provider);
await nav2.LoadStateAsync();
  • Observed: InvalidOperationException: Each parameter in the deserialization constructor on type 'ktsu.Navigation.Models.NavigationState1[...]' must bind to an object property or field on deserialization...`
  • Expected: true, with the history and current item restored.

With the fix below applied temporarily, the same repro loaded correctly.

Why it matters

The JSON file provider is the library's only built-in durable persistence, and the README's persistence example crashes on the load side. Saving appears to work, so the failure only shows up on the next app start.

Suggested fix

Add a constructor System.Text.Json can bind:

[JsonConstructor]
internal NavigationState(IReadOnlyList<T> items, int currentIndex)
    : this((IEnumerable<T>)items, currentIndex) { }

Optionally, also catch NotSupportedException / InvalidOperationException in the provider if it is meant to return null on unreadable data.

Acceptance criteria

  • A test that saves via JsonFilePersistenceProvider and loads into a new Navigation restores Current and the back/forward stacks.

Activity

  1. matt-edmondson commented on Sep 26, 2026

    @matt-edmondson
    ContributorAuthor

    Triage

    • Category: Bug
    • Priority: High. JsonFilePersistenceProvider is the library's only built-in durable persistence, and its load path always throws. The README's persistence example crashes on load. Saving appears to succeed, so users only find out on the next app start.
    • Area / suggested assignee: NavigationState<T> deserialization constructor, and the exception handling in JsonFilePersistenceProvider.LoadStateAsync. Maintainer: @matt-edmondson.
    • Duplicates / in progress: No duplicate, and no open PR in this repo. It is related to Saving then loading restores the wrong position when the current page also appears earlier in history #54, which is in the same NavigationState round-trip. That bug is hidden until this one is fixed, because a JSON load never gets far enough to restore a position. Fixing both together, with one round-trip test suite, makes sense.
    • Next steps: Add the [JsonConstructor] overload that takes IReadOnlyList<T>. Decide whether the provider should return null on unreadable data by also catching NotSupportedException / InvalidOperationException. Add a save-then-load test through JsonFilePersistenceProvider, which is currently missing.

    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions