Support serializing Stripe objects with ApiResource.GSON#2168
Merged
jar-stripe merged 2 commits intomasterfrom Mar 4, 2026
Merged
Support serializing Stripe objects with ApiResource.GSON#2168jar-stripe merged 2 commits intomasterfrom
jar-stripe merged 2 commits intomasterfrom
Conversation
ApiResource.GSON.toJson() produced JSON that could not be deserialized
back via ApiResource.GSON.fromJson() because createGson() registered
custom deserializers but not corresponding serializers for several types.
Three fixes:
1. Register the existing ExpandableFieldSerializer in createGson() —
it was only used in PRETTY_PRINT_GSON, so expanded objects serialized
as {"id":"x","expandedObject":{...}} instead of the flat resource.
2. Add StripeRawJsonObjectSerializer that writes the raw JsonObject
directly, instead of the default reflection wrapping it in {"json":{}}.
3. Fix write() in the three polymorphic TypeAdapterFactories
(PaymentSource, BalanceTransactionSource, ExternalAccount) to resolve
the adapter for the runtime type via getDelegateAdapter, instead of
delegating to the interface-typed adapter which produces {}.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Committed-By-Agent: claude
xavdid-stripe
approved these changes
Mar 4, 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?
Users serializing Stripe objects with
ApiResource.GSON.toJson()get JSON output thatApiResource.GSON.fromJson()cannot deserialize back into the correct object. This happens becausecreateGson()registers custom deserializers for several types but not corresponding serializers — so GSON falls back to default reflection, which produces a different JSON shape than the deserializers expect.For example, an
Invoicewith an expandedcustomerfield serializes theExpandableFieldwrapper object ({"id":"cus_xxx","expanded_object":{...}}) instead of the flat customer object the deserializer expects. Similarly, polymorphic fields likeCharge.sourceserialize as{}because the type adapter delegates to the interface type (which has no fields) rather than the concrete runtime type.What?
ExpandableFieldSerializerincreateGson()— it was only registered inPRETTY_PRINT_GSON, soApiResource.GSONfell back to reflection for serializationStripeRawJsonObjectSerializerto write the rawJsonObjectdirectly instead of wrapping it in{"json":{...}}write()in the three polymorphicTypeAdapterFactoryclasses (PaymentSourceTypeAdapterFactory,BalanceTransactionSourceTypeAdapterFactory,ExternalAccountTypeAdapterFactory) to resolve the adapter for the runtime type instead of the interface type, which produced{}ApiResource.GSON.toJson()and verify they can be deserialized back withApiResource.GSON.fromJson()See Also
Changelog
ApiResource.GSONnow supports serializing Stripe objects back into compatible JSON