{"record":{"id":"804cfb2d14545428","repo":"apache/dubbo","slug":"generic-serialization-s-json-syntax-exception-t","errorCode":null,"errorMessage":"Generic serialization [%s] Json syntax exception thrown when parsing (message:%s type:%s) error:%s","messagePattern":"Generic serialization \\[(.+?)\\] Json syntax exception thrown when parsing \\(message:(.+?) type:(.+?)\\) error:(.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"dubbo-common/src/main/java/org/apache/dubbo/common/json/GsonUtils.java","lineNumber":59,"sourceCode":"                        supportGson = aClass != null;\n                    } catch (Throwable t) {\n                        supportGson = false;\n                    }\n                }\n            }\n        }\n        return supportGson;\n    }\n\n    public static Object fromJson(String json, Type originType) throws RuntimeException {\n        if (!isSupportGson()) {\n            throw new RuntimeException(\"Gson is not supported. Please import Gson in JVM env.\");\n        }\n        Type type = TypeToken.get(originType).getType();\n        try {\n            return getGson().fromJson(json, type);\n        } catch (JsonSyntaxException ex) {\n            throw new RuntimeException(String.format(\n                    \"Generic serialization [%s] Json syntax exception thrown when parsing (message:%s type:%s) error:%s\",\n                    GENERIC_SERIALIZATION_GSON, json, type.toString(), ex.getMessage()));\n        }\n    }\n\n    public static String toJson(Object obj) throws RuntimeException {\n        if (!isSupportGson()) {\n            throw new RuntimeException(\"Gson is not supported. Please import Gson in JVM env.\");\n        }\n        try {\n            return getGson().toJson(obj);\n        } catch (JsonSyntaxException ex) {\n            throw new RuntimeException(String.format(\n                    \"Generic serialization [%s] Json syntax exception thrown when parsing (object:%s ) error:%s\",\n                    GENERIC_SERIALIZATION_GSON, obj, ex.getMessage()));\n        }\n    }\n","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/apache/dubbo/blob/3a3043227f5571d25eb2889de5bca22f2914843b/dubbo-common/src/main/java/org/apache/dubbo/common/json/GsonUtils.java#L41-L77","documentation":"Thrown by GsonUtils.fromJson when Gson raises a JsonSyntaxException during deserialization. Dubbo catches it and rethrows as a RuntimeException with a formatted diagnostic including the generic-serialization tag (gson), the offending json payload, the target Type, and Gson's underlying message. The wrapping is unchecked (RuntimeException) so it propagates through generic-invocation call stacks that do not declare checked exceptions. Note: the message says 'parsing' but for fromJson this is deserialization.","triggerScenarios":"GsonUtils.fromJson(json, type) where json is malformed JSON (unbalanced braces, trailing commas, bad escapes) or is valid JSON that Gson cannot bind to the target Type (e.g. a JSON object where the Type expects an array, unknown fields under a strict policy, or a number where a string is required).","commonSituations":"Downstream service returns a changed/error response shape; a proxy or filter corrupts the payload (truncation, HTML error page instead of JSON); mismatch between the declared Type and the actual payload (version skew between provider and consumer DTOs); non-UTF-8 bytes decoded incorrectly before reaching Gson; numeric overflow when binding into a narrower field.","solutions":["Log the full json and type from the exception message, then validate the payload against the expected schema (it usually reveals truncation or a shape change).","Align provider and consumer on the DTO/Type — check for version drift in the generic interface signature.","If the payload may be unreliable, pre-validate with a lenient JSON parser or guard before calling fromJson.","Catch the RuntimeException at the generic-invocation boundary and map it to a domain error rather than letting it bubble as a 500."],"exampleFix":"// before\nObject result = GsonUtils.fromJson(payload, MyType.class);\n// after - validate + handle\nObject result;\ntry {\n    result = GsonUtils.fromJson(payload, MyType.class);\n} catch (RuntimeException e) {\n    log.error(\"gson parse failed for payload={}, type={}\", payload, MyType.class, e);\n    throw new RpcException(\"Bad JSON payload\", e);\n}","handlingStrategy":"try-catch","validationCode":"// Pre-validate JSON structure with a lenient parser or schema before binding.\n// At minimum, confirm the payload is non-null and non-empty.\nif (json == null || json.isEmpty()) {\n    throw new IllegalArgumentException(\"empty json payload\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    return GsonUtils.fromJson(json, type);\n} catch (RuntimeException e) {\n    // message format: \"Generic serialization [gson] Json syntax exception ... (message:%s type:%s) error:%s\"\n    log.error(\"gson fromJson failed type={} payload={}\", type, json, e);\n    throw new RpcException(\"Invalid JSON payload for type \" + type, e);\n}","preventionTips":["Align provider and consumer DTO/Type versions to avoid binding failures.","Log the full payload and target Type from the exception message for triage.","Validate payloads against the expected schema before deserialization when they cross trust boundaries.","Map the RuntimeException to a domain error at the generic-invocation boundary rather than surfacing a raw 500."],"tags":["json","gson","deserialization","generic-invocation","dubbo-common"],"backgroundTag":null,"analyzedSha":"3a3043227f5571d25eb2889de5bca22f2914843b","analyzedAt":"2026-08-14T00:43:19.853Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}