{"record":{"id":"8e9f002b6503ddb1","repo":"apache/pulsar","slug":"failed-to-decode-from-json","errorCode":null,"errorMessage":"Failed to decode from json","messagePattern":"Failed to decode from json","errorType":"exception","errorClass":"ParseEnsemblePlacementPolicyConfigException","httpStatus":null,"severity":"error","filePath":"pulsar-common/src/main/java/org/apache/pulsar/common/policies/data/EnsemblePlacementPolicyConfig.java","lineNumber":89,"sourceCode":"    public byte[] encode() throws ParseEnsemblePlacementPolicyConfigException {\n        try {\n            return ObjectMapperFactory.getMapper()\n                .writer().withDefaultPrettyPrinter()\n                .writeValueAsString(this)\n                .getBytes(StandardCharsets.UTF_8);\n        } catch (JsonProcessingException e) {\n            throw new ParseEnsemblePlacementPolicyConfigException(\"Failed to encode to json\", e);\n        }\n    }\n\n    private static final ObjectReader ENSEMBLE_PLACEMENT_CONFIG_READER = ObjectMapperFactory.getMapper()\n            .reader().forType(EnsemblePlacementPolicyConfig.class);\n\n    public static EnsemblePlacementPolicyConfig decode(byte[] data) throws ParseEnsemblePlacementPolicyConfigException {\n        try {\n            return ENSEMBLE_PLACEMENT_CONFIG_READER.readValue(data);\n        } catch (IOException e) {\n            throw new ParseEnsemblePlacementPolicyConfigException(\"Failed to decode from json\", e);\n        }\n    }\n\n    public static class ParseEnsemblePlacementPolicyConfigException extends Exception {\n        private static final long serialVersionUID = 1L;\n\n        ParseEnsemblePlacementPolicyConfigException(String message, Throwable throwable) {\n            super(message, throwable);\n        }\n    }\n}\n","sourceCodeStart":71,"sourceCodeEnd":101,"githubUrl":"https://github.com/apache/pulsar/blob/820761864ed8e2a7d2e52dd9763ad2ae117c1395/pulsar-common/src/main/java/org/apache/pulsar/common/policies/data/EnsemblePlacementPolicyConfig.java#L71-L101","documentation":"EnsemblePlacementPolicyConfig.decode parses a JSON byte array back into an EnsemblePlacementPolicyConfig via a static Jackson ObjectReader. Any IOException from Jackson — malformed JSON, wrong structure, unknown-type mismatch, or truncated data — is wrapped as ParseEnsemblePlacementPolicyConfigException('Failed to decode from json').","triggerScenarios":"Calling decode(bytes) with bytes that are not valid JSON for this type: corrupt znode/metadata content, JSON produced by a different schema/version, empty or truncated byte arrays, or hand-written JSON with wrong types (e.g. string where a map is expected).","commonSituations":"Reading policy metadata stored by an older/newer Pulsar version with an incompatible layout; manual edits to namespace-isolation policy data in ZooKeeper/metadata store; passing the wrong byte payload (e.g. another config's JSON) into decode.","solutions":["Inspect the wrapped cause — Jackson messages name the offending field/line in the JSON.","Validate/pretty-print the source JSON and correct syntax or field types to match EnsemblePlacementPolicyConfig.","If produced by an older Pulsar version, upgrade the client/cluster or re-create the namespace isolation policy via the admin API (pulsar-admin namespaces set-namespace-isolation-policy).","Delete and re-create the corrupt policy metadata rather than hand-patching it."],"exampleFix":"// before\nbyte[] bad = \"{policy: ...\".getBytes(); // malformed\ncfg = EnsemblePlacementPolicyConfig.decode(bad);\n// after\nbyte[] good = \"{\\\"policyClass\\\":\\\"org.apache.pulsar...\\\",\\\"autoFailoverPolicy\\\":{...}}\".getBytes();\ncfg = EnsemblePlacementPolicyConfig.decode(good); // valid JSON matching the type","handlingStrategy":"try-catch","validationCode":"// validate JSON parses before decode\nstatic boolean isJson(byte[] data) {\n    try { new ObjectMapper().readTree(data); return true; }\n    catch (IOException e) { return false; }\n}","typeGuard":null,"tryCatchPattern":"try {\n    EnsemblePlacementPolicyConfig cfg = EnsemblePlacementPolicyConfig.decode(data);\n} catch (EnsemblePlacementPolicyConfig.ParseEnsemblePlacementPolicyConfigException e) {\n    log.error(\"Failed to decode placement policy config\", e.getCause());\n    // fall back to defaults or re-create the policy via the admin API\n}","preventionTips":["Never hand-edit policy metadata in the metadata store; use pulsar-admin.","Check e.getCause() — Jackson reports the exact bad field/offset.","Beware cross-version upgrades: confirm the stored policy format matches your Pulsar version.","Validate JSON payload shape (fields/types) before calling decode."],"tags":["jackson","json-deserialization","config"],"backgroundTag":"json-deserialization-failed","analyzedSha":"820761864ed8e2a7d2e52dd9763ad2ae117c1395","analyzedAt":"2026-09-06T00:14:20.138Z","contentChangedAt":"2026-09-06T00:14:20.138Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}