Skip to content

Commit 4428859

Browse files
jar-stripeclaude
andcommitted
Serialize BigDecimal as JSON string in V2 request bodies
JsonEncoder.BODY_GSON serialized BigDecimal as a JSON number (e.g., 75.25), but the V2 API expects decimal fields as JSON strings (e.g., "75.25"). V1 doesn't hit this because FormEncoder calls .toString() which naturally produces a string. Adds a TypeAdapter<BigDecimal> that writes toPlainString(). All BigDecimal fields in stripe-java are format: decimal and expect string wire format. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Committed-By-Agent: claude
1 parent d66b816 commit 4428859

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

src/main/java/com/stripe/net/JsonEncoder.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,42 @@
33
import com.google.gson.FieldNamingPolicy;
44
import com.google.gson.Gson;
55
import com.google.gson.GsonBuilder;
6+
import com.google.gson.TypeAdapter;
7+
import com.google.gson.stream.JsonReader;
8+
import com.google.gson.stream.JsonWriter;
69
import java.io.IOException;
10+
import java.math.BigDecimal;
711
import java.util.HashMap;
812
import java.util.Map;
913

1014
final class JsonEncoder {
15+
/**
16+
* Serializes BigDecimal as a JSON string (e.g., "25.5") rather than a JSON number (25.5). All
17+
* BigDecimal fields in the Stripe API use format: decimal, and the V2 API expects them as strings
18+
* on the wire.
19+
*/
20+
private static final TypeAdapter<BigDecimal> BIG_DECIMAL_STRING_ADAPTER =
21+
new TypeAdapter<BigDecimal>() {
22+
@Override
23+
public void write(JsonWriter out, BigDecimal value) throws IOException {
24+
if (value == null) {
25+
out.nullValue();
26+
} else {
27+
out.value(value.toPlainString());
28+
}
29+
}
30+
31+
@Override
32+
public BigDecimal read(JsonReader in) throws IOException {
33+
return new BigDecimal(in.nextString());
34+
}
35+
};
36+
1137
private static final Gson BODY_GSON =
1238
new GsonBuilder()
1339
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
1440
.serializeNulls()
41+
.registerTypeAdapter(BigDecimal.class, BIG_DECIMAL_STRING_ADAPTER)
1542
.create();
1643

1744
public static HttpContent createHttpContent(Map<String, Object> params) throws IOException {

0 commit comments

Comments
 (0)