binarywang/WxJava · error · JsonParseException

Expected NUMBER but was {peek}

Error message

Expected NUMBER but was {peek}

What it means

Thrown by WxDateTypeAdapter during deserialization when the JSON token is neither NULL nor NUMBER. The adapter only understands epoch seconds (multiplied by 1000 to form a Date); a string date, object, or array triggers this JsonParseException.

Source

Thrown at weixin-java-common/src/main/java/me/chanjar/weixin/common/util/json/WxDateTypeAdapter.java:40

  public void write(JsonWriter out, Date value) throws IOException {
    if (value == null) {
      out.nullValue();
    } else {
      out.value(value.getTime() / 1000);
    }
  }

  @Override
  public Date read(JsonReader in) throws IOException {
    JsonToken peek = in.peek();
    switch (peek) {
      case NULL:
        in.nextNull();
        return null;
      case NUMBER:
        return new Date(in.nextInt() * 1000);
      default:
        throw new JsonParseException("Expected NUMBER but was " + peek);
    }
  }
}

View on GitHub (pinned to 1c43293a3c)

Solutions

  1. Log the raw response to confirm the token type of the date field.
  2. Use a string-typed field and parse it with the appropriate format, or register an adapter that also handles STRING.
  3. Verify the WxJava version matches the API contract for that field.

Example fix

// before - epoch expected but server sends string
@SerializedName("create_time") private Date createTime; // WxDateTypeAdapter
// after
@SerializedName("create_time") private String createTime; // parse manually
Defensive patterns

Strategy: try-catch

Validate before calling

null

Type guard

null

Try / catch

try {
  MyBean b = gson.fromJson(json, MyBean.class);
} catch (JsonParseException e) {
  log.error("date parse failed; raw={}", json);
  throw e;
}

Prevention

When it happens

Trigger: A Date field receives a string timestamp (e.g. "2024-01-01" or a millisecond string) instead of a numeric epoch; the server format changed; the field is bound to this adapter but carries unrelated data.

Common situations: WeChat returns a formatted date string for a field previously sent as epoch; mixing this adapter with fields that actually carry ISO dates; version skew between server format and adapter.

Related errors


AI-assisted analysis of binarywang/WxJava@1c43293a3c (2026-08-14). Data as JSON: /api/errors/1c42a48b3c9dd172. Report an issue: GitHub.