google/gson · error · JsonSyntaxException
Failed parsing '" + s + "' as SQL Time; at path " + in.getPr
Error message
Failed parsing '" + s + "' as SQL Time; at path " + in.getPreviousPath()
What it means
SqlTimeTypeAdapter parses the JSON string with SimpleDateFormat("hh:mm:ss a") (12-hour clock with AM/PM marker, e.g. '01:30:00 PM'). A ParseException is wrapped as JsonSyntaxException. The format is locale-sensitive and synchronized.
Source
Thrown at gson/src/main/java/com/google/gson/internal/sql/SqlTimeTypeAdapter.java:70
private final DateFormat format = new SimpleDateFormat("hh:mm:ss a");
private SqlTimeTypeAdapter() {}
@Override
public Time read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
return null;
}
String s = in.nextString();
synchronized (this) {
TimeZone originalTimeZone = format.getTimeZone(); // Save the original time zone
try {
Date date = format.parse(s);
return new Time(date.getTime());
} catch (ParseException e) {
throw new JsonSyntaxException(
"Failed parsing '" + s + "' as SQL Time; at path " + in.getPreviousPath(), e);
} finally {
format.setTimeZone(originalTimeZone); // Restore the original time zone
}
}
}
@Override
public void write(JsonWriter out, Time value) throws IOException {
if (value == null) {
out.nullValue();
return;
}
String timeString;
synchronized (this) {
timeString = format.format(value);
}
out.value(timeString);View on GitHub (pinned to 8b8628c656)
Solutions
- Provide the time string in 'hh:mm:ss a' form (e.g. '02:30:00 PM').
- Register a custom TypeAdapter<java.sql.Time> using a 24-hour format like 'HH:mm:ss'.
- Deserialize as java.time.LocalTime and convert if the SQL type is not essential.
Example fix
// before
java.sql.Time t = gson.fromJson("\"14:30:00\"", java.sql.Time.class);
// after
Gson gson = new GsonBuilder().registerTypeAdapter(java.sql.Time.class, new TypeAdapter<java.sql.Time>() {
@Override public java.sql.Time read(JsonReader in) throws IOException { return java.sql.Time.valueOf(in.nextString()); }
@Override public void write(JsonWriter out, java.sql.Time v) throws IOException { out.value(v.toString()); }
}).create(); Defensive patterns
Strategy: validation
Validate before calling
boolean isSqlTimeParsable(String s) {
if (s == null) return false;
try { java.text.DateFormat f = new java.text.SimpleDateFormat("hh:mm:ss a", Locale.ENGLISH); f.parse(s); return true; }
catch (java.text.ParseException e) { return false; }
} Type guard
static boolean matchesDefaultSqlTimeFormat(String s) {
return s != null && s.matches("\\d{2}:\\d{2}:\\d{2} (AM|PM)");
} Try / catch
try {
java.sql.Time t = gson.fromJson(json, java.sql.Time.class);
} catch (JsonSyntaxException e) {
// register a 24-hour HH:mm:ss adapter and retry
} Prevention
- Register a java.sql.Time adapter with the producer's 24-hour format.
- Pin Locale.ENGLISH for AM/PM symbols if you keep the default format.
- Prefer java.time.LocalTime for new code.
When it happens
Trigger: Deserializing a JSON value into a java.sql.Time field where the string is not in 'hh:mm:ss a' 12-hour form (e.g. '14:30:00' 24-hour, ISO format, or missing AM/PM).
Common situations: Producer emits 24-hour ISO times, missing AM/PM marker, or a locale whose DateTimeSymbols lack the expected symbols; mismatched conventions across system layers.
Related errors
- Failed parsing '" + s + "' as SQL Date; at path " + in.getPr
- duplicate key: {key}
- Expecting number, got: " + jsonToken + "; at path " + in.get
- Unexpected token: " + peeked
- Failed parsing '" + s + "' as BigDecimal; at path " + in.get
AI-assisted analysis of google/gson@8b8628c656 (2026-08-04).
Data as JSON: /data/errors/81be8f7cafd7491b.json.
Report an issue: GitHub.