{"record":{"id":"12da3dfa8be0db7a","repo":"dianping/cat","slug":"the-date-should-be-a-string-value","errorCode":null,"errorMessage":"The date should be a string value","messagePattern":"The date should be a string value","errorType":"validation","errorClass":"JsonParseException","httpStatus":null,"severity":"error","filePath":"cat-core/src/main/java/com/dianping/cat/helper/JsonBuilder.java","lineNumber":76,"sourceCode":"\tprivate Gson m_gson = new GsonBuilder().registerTypeAdapter(Timestamp.class, new TimestampTypeAdapter())\n\t\t\t\t\t\t\t.setDateFormat(\"yyyy-MM-dd HH:mm:ss\").setFieldNamingStrategy(m_fieldNamingStrategy).create();\n\n\t@SuppressWarnings({ \"unchecked\", \"rawtypes\" })\n\tpublic Object parse(String json, Class clz) {\n\t\treturn m_gson.fromJson(json, clz);\n\t}\n\n\tpublic String toJson(Object o) {\n\t\treturn m_gson.toJson(o);\n\t}\n\n\tpublic class TimestampTypeAdapter implements JsonSerializer<Timestamp>, JsonDeserializer<Timestamp> {\n\t\tprivate final DateFormat format = new SimpleDateFormat(\"yyyy-MM-dd HH:mm:ss\");\n\n\t\tpublic Timestamp deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)\n\t\t\t\t\t\t\t\tthrows JsonParseException {\n\t\t\tif (!(json instanceof JsonPrimitive)) {\n\t\t\t\tthrow new JsonParseException(\"The date should be a string value\");\n\t\t\t}\n\n\t\t\ttry {\n\t\t\t\tDate date = format.parse(json.getAsString());\n\t\t\t\treturn new Timestamp(date.getTime());\n\t\t\t} catch (ParseException e) {\n\t\t\t\tthrow new JsonParseException(e);\n\t\t\t}\n\t\t}\n\n\t\tpublic JsonElement serialize(Timestamp src, Type arg1, JsonSerializationContext arg2) {\n\t\t\tString dateFormatAsString = format.format(new Date(src.getTime()));\n\t\t\treturn new JsonPrimitive(dateFormatAsString);\n\t\t}\n\t}\n\n}\n","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/dianping/cat/blob/e815e74d4c2dd74edac831241f1253fcc7d25381/cat-core/src/main/java/com/dianping/cat/helper/JsonBuilder.java#L58-L94","documentation":"A Gson JsonParseException thrown by the custom TimestampTypeAdapter registered inside JsonBuilder when deserializing a JSON field into java.sql.Timestamp and the JSON element is not a JsonPrimitive — i.e. the value is an object, array, or null literal instead of a string. The adapter expects dates serialized as \"yyyy-MM-dd HH:mm:ss\" strings; only the 'not a primitive' shape produces this exact message (an unparseable string yields a wrapped ParseException instead).","triggerScenarios":"JsonBuilder.fromJson(json, type) (or any Gson parse using this adapter) where a Timestamp-typed field in the payload is {} / [] / null rather than a quoted date string. For example {\"createTime\": null} or {\"createTime\": {\"$date\": ...}} (Mongo-style extended JSON).","commonSituations":"Consuming JSON from systems that serialize dates as structured objects (BSON extended JSON, Jackson default array [y,m,d,...]) or that emit explicit nulls; changing a field type to Timestamp while old payloads still carry a different shape.","solutions":["Inspect the raw JSON for the Timestamp field; confirm it is a quoted \"yyyy-MM-dd HH:mm:ss\" string.","Fix the producer to serialize timestamps as strings in that exact format.","If nulls are legitimate, make the field nullable on the Java side so Gson skips the adapter, or preprocess the JSON replacing null dates with a sentinel string.","Write your own TypeAdapter that returns null for non-primitive input instead of throwing, and register it on your Gson instance."],"exampleFix":"// before\n{\"createTime\": {\"$date\":\"2026-01-01T00:00:00Z\"}} // object -> JsonParseException\n\n// after\n{\"createTime\": \"2026-01-01 00:00:00\"}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"JsonElement el = jsonObject.get(\"createTime\");\nif (el == null || !el.isJsonPrimitive()) { /* treat as null/missing */ }","tryCatchPattern":"try { obj = jsonBuilder.fromJson(json, type); }\ncatch (JsonParseException e) { if (e.getMessage().contains(\"date should be a string\")) { /* normalize payload or null the field */ } else throw e; }","preventionTips":["Standardize on string dates 'yyyy-MM-dd HH:mm:ss' in producers.","Pre-validate payloads with JsonObject inspection before binding.","Register a lenient custom TypeAdapter if null/object dates must be tolerated."],"tags":["json","gson","serialization","timestamp"],"backgroundTag":null,"analyzedSha":"e815e74d4c2dd74edac831241f1253fcc7d25381","analyzedAt":"2026-08-14T14:22:34.512Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}