google/gson · error · JsonSyntaxException

Failed parsing '{}' as SQL Time; at path {}

Error message

Failed parsing '{}' as SQL Time; at path {}

What it means

Thrown by Gson's SqlTimeTypeAdapter when its SimpleDateFormat("hh:mm:ss a") fails to parse the JSON string into java.sql.Time. The default format expects a 12-hour clock with AM/PM marker in the adapter locale (e.g. "12:00:00 PM"); ISO times ("00:00:00"), 24-hour strings, or locale-mismatched AM/PM tokens cause ParseException wrapped as JsonSyntaxException.

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 310ac341f2)

Solutions

  1. Produce JSON strings matching "hh:mm:ss a" in the JVM locale, OR switch the field type to java.time.LocalTime / String and convert explicitly.
  2. Register a custom TypeAdapter<java.sql.Time> using SimpleDateFormat("HH:mm:ss") for 24-hour ISO input.
  3. Pin the format locale to Locale.US (or the producer's locale) to avoid AM/PM token mismatches.
  4. Sanitize the input (convert 24-hour to 12-hour with AM/PM) before Gson if you cannot change the producer.

Example fix

// before
public class Schedule { public java.sql.Time opensAt; }
// JSON: {"opensAt":"13:30:00"} -> fails (expects "hh:mm:ss a")

// after: custom adapter for 24-hour ISO times
Gson g = new GsonBuilder()
  .registerTypeAdapter(java.sql.Time.class, new TypeAdapter<java.sql.Time>() {
    private final DateFormat f = new SimpleDateFormat("HH:mm:ss", Locale.US);
    public java.sql.Time read(JsonReader in) throws IOException {
      try { return new java.sql.Time(f.parse(in.nextString()).getTime()); }
      catch (ParseException e) { throw new JsonSyntaxException(e); }
    }
    public void write(JsonWriter out, java.sql.Time v) throws IOException { out.value(f.format(v)); }
  }).create();
Defensive patterns

Strategy: validation

Validate before calling

// validate 24-hour ISO time, or accept 'hh:mm:ss a'
private static final Pattern HHMMSS = Pattern.compile("^\\d{2}:\\d{2}:\\d{2}$");
String raw = jsonNode.get("opensAt").getAsString();
if (raw == null || !HHMMSS.matcher(raw).matches()) {
  throw new IllegalArgumentException("Not 'HH:mm:ss': " + raw);
}

Try / catch

try {
  return gson.fromJson(json, Schedule.class);
} catch (JsonSyntaxException e) {
  if (e.getMessage().contains("as SQL Time")) {
    // switch to a 24-hour adapter for java.sql.Time
    throw new IllegalArgumentException("Bad SQL Time format", e);
  }
  throw e;
}

Prevention

When it happens

Trigger: Deserializing a field of type java.sql.Time from a JSON string that does not match "hh:mm:ss a" in the running locale: e.g. "13:30:00" (24-hour), "13:30" (no seconds/AM-PM), "1330", or AM/PM tokens in a locale whose DateFormatSymbols differ.

Common situations: Producers using ISO 8601 time or 24-hour strings; servers in locales where AM/PM markers are localized differently (e.g. non-English locales); fields mistakenly typed as java.sql.Time when the data is a full timestamp; timezone shifts.

Related errors


AI-assisted analysis of google/gson@310ac341f2 (2026-08-10). Data as JSON: /api/errors/4a8bc317dd19aac6. Report an issue: GitHub.