{"id":646,"date":"2022-08-30T15:04:49","date_gmt":"2022-08-30T15:04:49","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/how-do-i-specialize-deserializing-null-with-jackson-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:04:49","modified_gmt":"2022-08-30T15:04:49","slug":"how-do-i-specialize-deserializing-null-with-jackson-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/how-do-i-specialize-deserializing-null-with-jackson-collection-of-common-programming-errors\/","title":{"rendered":"How do I specialize deserializing &#39;null&#39; with Jackson?-Collection of common programming errors"},"content":{"rendered":"<p>I have a class that can output any of the following:<\/p>\n<ul>\n<li>{ title: &#8220;my claim&#8221; }<\/li>\n<li>{ title: &#8220;my claim&#8221;, judgment: null }<\/li>\n<li>{ title: &#8220;my claim&#8221;, judgment: &#8220;STANDING&#8221; }<\/li>\n<\/ul>\n<p>(notice how judgment is different in each case: it can be undefined, null, or a value)<\/p>\n<p>The class looks like:<\/p>\n<pre><code>class Claim {\n   String title;\n   Nullable judgment;\n}\n<\/code><\/pre>\n<p>and Nullable is this:<\/p>\n<pre><code>class Nullable {\n   public T value;\n}\n<\/code><\/pre>\n<p>with a custom serializer:<\/p>\n<pre><code>SimpleModule module = new SimpleModule(\"NullableSerMod\", Version.unknownVersion());\nmodule.addSerializer(Nullable.class, new JsonSerializer() {\n   @Override\n   public void serialize(Nullable arg0, JsonGenerator arg1, SerializerProvider arg2) throws IOException, JsonProcessingException {\n      if (arg0 == null)\n         return;\n      arg1.writeObject(arg0.value);\n   }\n});\noutputMapper.registerModule(module);\n<\/code><\/pre>\n<p><em>Summary:<\/em> This setup allows me to output the value, or null, <em>or undefined<\/em>.<\/p>\n<p><strong>Now, my question<\/strong>: How do I write the corresponding deserializer?<\/p>\n<p>I imagine it would look something like this:<\/p>\n<pre><code>SimpleModule module = new SimpleModule(\"NullableDeserMod\", Version.unknownVersion());\nmodule.addDeserializer(Nullable.class, new JsonDeserializer deserialize(JsonParser parser, DeserializationContext context) throws IOException, JsonProcessingException {\n      if (next thing is null)\n         return new Nullable(null);\n      else\n         return new Nullable(parser.readValueAs(inner type));\n   }\n});\n<\/code><\/pre>\n<p>but I don&#8217;t know what to put for &#8220;next thing is null&#8221; or &#8220;inner type&#8221;.<\/p>\n<p>Any ideas on how I can do this?<\/p>\n<p>Thanks!<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-09 21:10:17. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have a class that can output any of the following: { title: &#8220;my claim&#8221; } { title: &#8220;my claim&#8221;, judgment: null } { title: &#8220;my claim&#8221;, judgment: &#8220;STANDING&#8221; } (notice how judgment is different in each case: it can be undefined, null, or a value) The class looks like: class Claim { String title; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-646","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/646","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=646"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/646\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=646"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=646"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=646"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}