Skip to content

Fix preferences listing crash with non-string types on Android#40

Merged
Redth merged 1 commit intomainfrom
fix/preferences-typed-read
Mar 11, 2026
Merged

Fix preferences listing crash with non-string types on Android#40
Redth merged 1 commit intomainfrom
fix/preferences-typed-read

Conversation

@Redth
Copy link
Copy Markdown
Owner

@Redth Redth commented Mar 11, 2026

Fixes #39

Android's SharedPreferences stores typed values natively and throws ClassCastException when you try to read an int as a string.

Changes

  • Added ReadPreferenceValue(key, sharedName) helper that tries each supported type in order (string → int → bool → double → long → float), catching cast failures
  • List handler uses the helper for each key, so one non-string value no longer crashes the entire listing
  • Get handler uses the helper when no type query param is specified (auto-detect)
  • List response now includes a type field for each entry

Testing

Tested by storing an int preference on Android via POST /api/preferences/myInt with {"value": 42, "type": "int"}, then verifying GET /api/preferences returns all preferences including the int with type: "int".

Android SharedPreferences stores typed values (int, bool, double, etc.)
and throws ClassCastException when reading with the wrong type. The List
handler always called Preferences.Get<string> which crashes when int or
other non-string values exist.

Added ReadPreferenceValue helper that tries each supported type in order
(string, int, bool, double, long, float) catching cast failures. Used
by both List and Get (when no type param specified). List response now
includes a 'type' field for each entry.

Fixes: Storing an int preference then listing would return 'java.lang.
Integer cannot be cast to java.lang.String' and hide all preferences.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@Redth Redth merged commit 0b832c3 into main Mar 11, 2026
2 checks passed
@Redth Redth deleted the fix/preferences-typed-read branch March 11, 2026 19:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Preferences listing crashes with non-string types on Android

1 participant