Repository navigation
Cache targetingData as a JSON string - #58
Open
jrosendahl-opt wants to merge 1 commit into
Open
jrosendahl-opt wants to merge 1 commit into
jrosendahl-opt wants to merge 1 commit into
Conversation
setTargeting() cached the whole OptableTargeting with Gson, which can't
serialize org.json.JSONObject: it wrote the object's internal fields
(nameValuePairs, values). targetingFromCache() then returned a
targetingData whose nested objects and arrays were strings such as
"{values=[...]}" and whose integers were doubles. gamTargetingKeywords
and openRtbJson were unaffected.
Cache a private CachedTargeting with targetingData as its JSON string and
rebuild the JSONObject on read. Entries in the old format no longer parse,
so getTargeting() clears them and returns null, as it already does for
unreadable entries; the next targeting() call refills the cache.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
mosherBT
approved these changes
Nov 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
targetingFromCache()returns a brokentargetingData.LocalStorage.setTargeting()cached the wholeOptableTargetingwithGson().toJson(...), and Gson can't serializeorg.json.JSONObject: it writes the object's internal fields (nameValuePairs,values). On read, nested objects and arrays come back as strings such as"{values=[{nameValuePairs={...}}]}", and integers become doubles (5001→5001.0). CallinggetJSONArray("audience")on cached data throwsJSONException.gamTargetingKeywordsandopenRtbJsonwere unaffected, so GAM and Prebid targeting from the cache still worked.This has been the case since #35, so v1.0.0–v1.2.0 are affected. It was found while writing a round-trip test for the cache (real SharedPreferences and
org.jsonunder Robolectric), which fails on master.What Changed
LocalStoragecaches a privateCachedTargetingwhosetargetingDatais the JSON string (targetingData.toString()), and rebuilds theJSONObjecton read. Its fields carry@SerializedName, so the stored keys stay stable under R8 (Gson's bundled rules keep annotated fields).getTargeting()clears them and returnsnull, as it already does for unreadable entries.LocalStorageTestbuilds its cached entries withsetTargeting()instead ofGson().toJson(targeting), which is the old format.JSONObjectconstructor, a stub in local unit tests.