Skip to content

Fix: Accept numeric version field in AnkiConnect API requests#90

Open
mallomar wants to merge 2 commits into
KamWithK:masterfrom
mallomar:master
Open

Fix: Accept numeric version field in AnkiConnect API requests#90
mallomar wants to merge 2 commits into
KamWithK:masterfrom
mallomar:master

Conversation

@mallomar
Copy link
Copy Markdown

@mallomar mallomar commented Oct 7, 2025

Fixes KOReader Anki plugin compatibility by accepting numeric version fields in API requests.

Problem

KOReader sends {"version":6,"action":"requestPermission"} with version as a number (per the official AnkiConnect API spec), but AnkiConnect Android was rejecting this with a MalformedJsonException at column 14 (immediately after the numeric 6).

Solution

  1. Made the JSON parser lenient to accept both numeric and string version fields
  2. Updated get_version() to explicitly handle both number and string types for the version field

Testing

  • The official AnkiConnect API spec uses version as a number
  • This maintains backward compatibility with clients that send version as a string (like Yomitan)
  • Fixes #[89]

Related issue: #89

KOReader sends version as a number per the AnkiConnect API spec. Made JSON parser lenient to accept both numeric and string version fields for compatibility.
@mallomar
Copy link
Copy Markdown
Author

Hi @KamWithK, any progress on the merge? Thanks!

@chaecramb
Copy link
Copy Markdown

Facing the same issue. Would be great if this could be merged, if the maintainer is happy with the changes.

@KamWithK
Copy link
Copy Markdown
Owner

KamWithK commented May 1, 2026

Facing the same issue. Would be great if this could be merged, if the maintainer is happy with the changes.

Have you tested the changes?

@mallomar
Copy link
Copy Markdown
Author

mallomar commented May 5, 2026

Facing the same issue. Would be great if this could be merged, if the maintainer is happy with the changes.

Have you tested the changes?

Hi @KamWithK,
I'm the PR author. I haven't been able to test the built APK myself - I'm not set up for Android development and the repo doesn't appear to have CI that produces build artifacts.
That said, I verified the underlying bug pretty thoroughly before writing the fix. I patched KOReader to log the exact HTTP traffic to AnkiConnect Android and confirmed:

  • KOReader sends {"version":6,"action":"requestPermission"} (version as a number, per the official AnkiConnect API spec)
  • AnkiConnect Android responds with MalformedJsonException at line 1 column 14 — column 14 is immediately after the numeric 6
  • Yomitan works because it sends version as a string

The fix has two parts:

  • Parser.parse() uses a lenient JsonReader so the strict Gson parser stops rejecting valid numeric values
  • get_version() explicitly handles both number and string primitives, with a try/catch around Integer.parseInt() that falls back to the default if the string isn't a valid integer (so something like "4.11" wouldn't crash, it would just return the fallback)

Happy to add a unit test for the parser if that would help with review, or to test a built APK if you can produce one. Let me know what would be most useful.

@KamWithK
Copy link
Copy Markdown
Owner

KamWithK commented May 5, 2026

Facing the same issue. Would be great if this could be merged, if the maintainer is happy with the changes.

Have you tested the changes?

Hi @KamWithK,
I'm the PR author. I haven't been able to test the built APK myself - I'm not set up for Android development and the repo doesn't appear to have CI that produces build artifacts.
That said, I verified the underlying bug pretty thoroughly before writing the fix. I patched KOReader to log the exact HTTP traffic to AnkiConnect Android and confirmed:

  • KOReader sends {"version":6,"action":"requestPermission"} (version as a number, per the official AnkiConnect API spec)
  • AnkiConnect Android responds with MalformedJsonException at line 1 column 14 — column 14 is immediately after the numeric 6
  • Yomitan works because it sends version as a string

The fix has two parts:

  • Parser.parse() uses a lenient JsonReader so the strict Gson parser stops rejecting valid numeric values
  • get_version() explicitly handles both number and string primitives, with a try/catch around Integer.parseInt() that falls back to the default if the string isn't a valid integer (so something like "4.11" wouldn't crash, it would just return the fallback)

Happy to add a unit test for the parser if that would help with review, or to test a built APK if you can produce one. Let me know what would be most useful.

Sorry what? I have limited time, set up your machine properly and test your changes before you submit a PR
You are asking for a change to make KOReader work and so you should test both with Yomitan and KOReader

@mallomar
Copy link
Copy Markdown
Author

mallomar commented May 6, 2026

Facing the same issue. Would be great if this could be merged, if the maintainer is happy with the changes.

Have you tested the changes?

Hi @KamWithK,
I'm the PR author. I haven't been able to test the built APK myself - I'm not set up for Android development and the repo doesn't appear to have CI that produces build artifacts.
That said, I verified the underlying bug pretty thoroughly before writing the fix. I patched KOReader to log the exact HTTP traffic to AnkiConnect Android and confirmed:

  • KOReader sends {"version":6,"action":"requestPermission"} (version as a number, per the official AnkiConnect API spec)
  • AnkiConnect Android responds with MalformedJsonException at line 1 column 14 — column 14 is immediately after the numeric 6
  • Yomitan works because it sends version as a string

The fix has two parts:

  • Parser.parse() uses a lenient JsonReader so the strict Gson parser stops rejecting valid numeric values
  • get_version() explicitly handles both number and string primitives, with a try/catch around Integer.parseInt() that falls back to the default if the string isn't a valid integer (so something like "4.11" wouldn't crash, it would just return the fallback)

Happy to add a unit test for the parser if that would help with review, or to test a built APK if you can produce one. Let me know what would be most useful.

Sorry what? I have limited time, set up your machine properly and test your changes before you submit a PR
You are asking for a change to make KOReader work and so you should test both with Yomitan and KOReader

Fair enough on testing expectations. The blocker on my end is that I don’t have an Android dev environment set up, and the repo doesn’t produce build artifacts on PRs, so there’s no APK to sideload. Setting up Android Studio from scratch to test a parser change isn’t something I’m able to take on right now.

Leaving the PR open in case it’s useful to someone with the build setup, or if you’d like to produce a debug APK from this branch I’m happy to test it on my end with both Yomitan and KOReader and report back.

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.

3 participants