{"record":{"id":"122a4d02b497ab59","repo":"apache/pulsar","slug":"failed-to-encode-to-json","errorCode":null,"errorMessage":"Failed to encode to json","messagePattern":"Failed to encode to json","errorType":"exception","errorClass":"ParseEnsemblePlacementPolicyConfigException","httpStatus":null,"severity":"error","filePath":"pulsar-common/src/main/java/org/apache/pulsar/common/policies/data/EnsemblePlacementPolicyConfig.java","lineNumber":78,"sourceCode":"    @Override\n    public boolean equals(Object obj) {\n        if (obj instanceof EnsemblePlacementPolicyConfig) {\n            EnsemblePlacementPolicyConfig other = (EnsemblePlacementPolicyConfig) obj;\n            return Objects.equals(this.policyClass == null ? null : this.policyClass.getName(),\n                other.policyClass == null ? null : other.policyClass.getName())\n                && Objects.equals(this.properties, other.properties);\n        }\n        return false;\n    }\n\n    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) {","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/apache/pulsar/blob/820761864ed8e2a7d2e52dd9763ad2ae117c1395/pulsar-common/src/main/java/org/apache/pulsar/common/policies/data/EnsemblePlacementPolicyConfig.java#L60-L96","documentation":"EnsemblePlacementPolicyConfig.encode serializes the policy config object to pretty-printed JSON with Jackson. If Jackson raises a JsonProcessingException (unserializable property, invalid accessor, recursion), the original exception is wrapped in ParseEnsemblePlacementPolicyConfigException('Failed to encode to json'). For this simple POJO it is rare and usually indicates a bad getter or an incompatible Jackson version on the classpath.","triggerScenarios":"Calling encode() (directly or via buildMetadataForPlacementPolicyConfig) when the config object contains a property Jackson cannot serialize — e.g. a custom policy-class field with a getter throwing, an exotic type without a serializer, or a shaded/duplicate Jackson version conflict.","commonSituations":"Custom BookKeeper ensemble placement policy classes bundled with dependencies that shade an incompatible Jackson; adding non-POJO fields to a subclassed config; OSGi/classloader conflicts in function workers.","solutions":["Read the wrapped cause (e.getCause()) of ParseEnsemblePlacementPolicyConfigException — it names the exact property/serialization problem.","Fix or annotate the offending field (e.g. @JsonIgnore on transient/getter-throwing properties).","Check the classpath for duplicated or shaded Jackson databind versions and align them to the Pulsar-provided version.","If a custom placement policy object is embedded, ensure it is JSON-serializable or store only its class name + serializable metadata map."],"exampleFix":"// before\nclass MyPolicyConfig { InputStream stream; } // unserializable\n// after\nclass MyPolicyConfig {\n    @com.fasterxml.jackson.annotation.JsonIgnore\n    private InputStream stream;\n}","handlingStrategy":"try-catch","validationCode":"// sanity-check serializability before encode\nnew ObjectMapper().writeValueAsString(config); // throws JsonProcessingException early if unserializable","typeGuard":null,"tryCatchPattern":"try {\n    byte[] json = config.encode();\n} catch (EnsemblePlacementPolicyConfig.ParseEnsemblePlacementPolicyConfigException e) {\n    log.error(\"JSON encode failed for placement policy config\", e.getCause());\n    throw e;\n}","preventionTips":["Keep EnsemblePlacementPolicyConfig fields JSON-POJO friendly; @JsonIgnore any transient handles.","Always inspect e.getCause() — the Jackson message names the failing property.","Audit the classpath for duplicated/shaded Jackson databind versions.","When embedding custom placement policy metadata, store only serializable maps/strings."],"tags":["jackson","json-serialization","config"],"backgroundTag":"json-serialization-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"}