{"id":3342,"date":"2025-03-11T13:38:45","date_gmt":"2025-03-11T20:38:45","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/azure-sdk\/?p=3342"},"modified":"2025-03-11T13:38:45","modified_gmt":"2025-03-11T20:38:45","slug":"replacing-jackson-databind-with-azure-json-and-azure-xml","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/azure-sdk\/replacing-jackson-databind-with-azure-json-and-azure-xml\/","title":{"rendered":"Replacing jackson-databind with azure-json and azure-xml"},"content":{"rendered":"<p><a href=\"https:\/\/github.com\/FasterXML\/jackson-databind\"><code>jackson-databind<\/code><\/a> (Jackson) is performant, reliable, and universally known in\nthe Java ecosystem. Unfortunately, our dependency on it results in pain points when consuming the Azure SDK client libraries for\nJava. Over the last few years, the Azure SDK for Java team worked to limit our reliance on <code>jackson-databind<\/code>.<\/p>\n<h2>Why was this change made?<\/h2>\n<p>Jackson is widely used in the Java ecosystem, but the versions being used varies library to library. This arrangement leads to dependency\nconflicts when multiple libraries bring in differing versions of Jackson, which can cause runtime issues. The Azure SDK for\nJava aims to minimize these conflicts and improve compatibility across different environments. Additionally, Jackson&#8217;s reliance\non reflection can cause issues with the Java module system and security policies.<\/p>\n<h2>Introducing azure-json and azure-xml<\/h2>\n<p>To reduce and replace Jackson usage, the Azure SDK for Java now offers two new serialization libraries: <a href=\"https:\/\/github.com\/Azure\/azure-sdk-for-java\/tree\/main\/sdk\/serialization\/azure-json\"><code>azure-json<\/code><\/a>\nand <a href=\"https:\/\/github.com\/Azure\/azure-sdk-for-java\/tree\/main\/sdk\/serialization\/azure-xml\"><code>azure-xml<\/code><\/a>. These libraries support\nJSON and XML serialization respectively, providing streaming readers and writers, and interfaces for models to implement serialization\nbehaviors. Neither library includes runtime dependencies, reducing the chances of dependency conflicts.<\/p>\n<p><code>azure-json<\/code> includes <code>JsonReader<\/code>, <code>JsonWriter<\/code>, and <code>JsonSerializable<\/code>, inspired by the <a href=\"https:\/\/www.json.org\/\">JSON specification<\/a>\nand streaming APIs from <code>jackson-core<\/code> and <code>gson<\/code> (Gson). It uses the <code>JsonProvider<\/code> service provider interface (SPI) for creating instances,\nwith a default implementation using shaded <code>jackson-core<\/code>. <code>azure-json<\/code> also includes a model-based JSON structure in <code>com.azure.json.models<\/code>.<\/p>\n<p><code>azure-xml<\/code> includes <code>XmlReader<\/code>, <code>XmlWriter<\/code>, and <code>XmlSerializable<\/code>, inspired by the <a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/javax\/xml\/stream\/package-summary.html\">javax.xml.stream<\/a>\nAPIs. These classes wrap Java&#8217;s <code>XMLStreamReader<\/code> and <code>XMLStreamWriter<\/code> interfaces. For performance reasons, <code>azure-xml<\/code> uses shaded\n<code>aalto-xml<\/code> if the built-in implementations are resolved.<\/p>\n<p>Using <code>azure-json<\/code> and <code>azure-xml<\/code> reduces dependency conflicts and the need for reflection, improving compatibility with the\nJava module system.<\/p>\n<h2>Changes to models in Azure SDK for Java client libraries<\/h2>\n<p>Previously, client library models used <code>jackson-annotations<\/code> for serialization. Now, with <code>azure-json<\/code> and <code>azure-xml<\/code>, these\nannotations are replaced with <code>JsonSerializable<\/code> and <code>XmlSerializable<\/code> implementations. This change reduces the need for\nreflection and simplifies support for both JSON and XML serialization.<\/p>\n<h2>Integration points with Jackson and Gson<\/h2>\n<p>To integrate with existing applications, we provide support for serializing and deserializing <code>JsonSerializable<\/code> types using\nJackson and Gson. We also offer full implementations of <code>JsonReader<\/code> and <code>JsonWriter<\/code> in these libraries.<\/p>\n<p>In <code>azure-core-serializer-json-jackson<\/code>, use <code>JacksonJsonProvider.getJsonSerializableDatabindModule()<\/code> to get a Jackson <code>Module<\/code>\nthat handles <code>JsonSerializable<\/code> types:<\/p>\n<pre><code class=\"language-java\">ObjectMapper objectMapper = JsonMapper.builder()\r\n    .addModule(JacksonJsonProvider.getJsonSerializableDatabindModule())\r\n    .build();<\/code><\/pre>\n<p>In <code>azure-core-serializer-json-gson<\/code>, use <code>GsonJsonProvider.getJsonSerializableTypeAdapterFactory()<\/code> to get a Gson <code>TypeAdapter<\/code>\nthat handles <code>JsonSerializable<\/code> types:<\/p>\n<pre><code class=\"language-java\">Gson gson = new GsonBuilder()\r\n    .registerTypeAdapterFactory(GsonJsonProvider.getJsonSerializableTypeAdapterFactory())\r\n    .create();<\/code><\/pre>\n<h2>What happened to Jackson dependencies?<\/h2>\n<p>The Azure SDK for Java client libraries continue to depend on <code>jackson-annotations<\/code>, <code>jackson-core<\/code>, and <code>jackson-databind<\/code> in\n<code>azure-core<\/code> for functionality that can&#8217;t be achieved by the <code>azure-json<\/code> and <code>azure-xml<\/code> streaming APIs. <code>jackson-dataformat-xml<\/code>\nwas removed as a dependency. Most non-<code>azure-core*<\/code> libraries eliminated direct dependencies on Jackson, relying instead on\n<code>azure-json<\/code> and <code>azure-xml<\/code>.<\/p>\n<p>The Azure SDK for Java team continues to explore the possibility of completely removing Jackson dependencies in <code>azure-core<\/code>.<\/p>\n<p>If you have questions or concerns, <a href=\"https:\/\/github.com\/Azure\/azure-sdk-for-java\/issues\/new?template=bug_report.md\">contact us on GitHub<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This blog post explains how azure-json and azure-xml replaced jackson-databind in the Azure SDK for Java.<\/p>\n","protected":false},"author":32199,"featured_media":3354,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[750,920,160,938],"class_list":["post-3342","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-azure-sdk","tag-azure-sdk","tag-core","tag-java","tag-serialization"],"acf":[],"blog_post_summary":"<p>This blog post explains how azure-json and azure-xml replaced jackson-databind in the Azure SDK for Java.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/posts\/3342","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/users\/32199"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/comments?post=3342"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/posts\/3342\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/media\/3354"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/media?parent=3342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/categories?post=3342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/tags?post=3342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}