Jackson JSON Processor
  1. Jackson JSON Processor
  2. JACKSON-616

Deserialize Empty Strings As Default Primitive Values

    Details

    • Type: Improvement Improvement
    • Status: Resolved Resolved
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None
    • Number of attachments :
      0

      Description

      When a JSON null value is deserialized to a primitive int field, the int field is populated with its default value of 0. (I did not check, but I wouldn't be surprised if similar handling of JSON null values is provided when deserializing to other primitive types.)

      However, currently when a JSON empty string is deserialized to a primitive int, an exception is thrown, though StdDeserializer._parseIntPrimitive(JsonParser, DeserializationContext) is already capable of returning 0 from a JSON empty string.

      As it's unlikely that throwing an exception for this situation is benefiting any current users, Jackson should be enhanced to deserialize JSON empty strings to the appropriate primitive default value when so bound.

        Activity

        Hide
        Programmer Bruce added a comment -

        See http://stackoverflow.com/questions/6661717/ for related issue and workaround using the current StdDeserializer._parseIntPrimitive(JsonParser, DeserializationContext) implementation.

        Show
        Programmer Bruce added a comment - See http://stackoverflow.com/questions/6661717/ for related issue and workaround using the current StdDeserializer._parseIntPrimitive(JsonParser, DeserializationContext) implementation.
        Hide
        Tatu Saloranta added a comment -

        Ok. Odd, I thought this had been changed to work the way described earlier. But apparently not.
        Should obviously also work for wrappers as well.

        Show
        Tatu Saloranta added a comment - Ok. Odd, I thought this had been changed to work the way described earlier. But apparently not. Should obviously also work for wrappers as well.
        Hide
        Tatu Saloranta added a comment -

        Hmmh. I am not able to reproduce the issue...

        Show
        Tatu Saloranta added a comment - Hmmh. I am not able to reproduce the issue...
        Hide
        Tatu Saloranta added a comment -

        Actually I think I know what could cause it, even thought I can't seem to be able to easily reproduce this – probably one must use auto-boxing or something.
        But I can improve deserialization to make better use of JsonDeserializer.getNullValue() (which should always be called to produce appropriate null replacement: a relatively little known feature that's been there since 1.0). And it already returns non-null default value for primitive deserializers.

        And I will also add JsonDeserializer.getEmptyValue(), which will be used to figure out presentation for "", mostly (although could be used for other non-null, non-valid things); and make it just call 'getNullValue()' as default implementation.

        Show
        Tatu Saloranta added a comment - Actually I think I know what could cause it, even thought I can't seem to be able to easily reproduce this – probably one must use auto-boxing or something. But I can improve deserialization to make better use of JsonDeserializer.getNullValue() (which should always be called to produce appropriate null replacement: a relatively little known feature that's been there since 1.0). And it already returns non-null default value for primitive deserializers. And I will also add JsonDeserializer.getEmptyValue(), which will be used to figure out presentation for "", mostly (although could be used for other non-null, non-valid things); and make it just call 'getNullValue()' as default implementation.
        Hide
        Programmer Bruce added a comment -

        The problem occurs with no auto-boxing involved.

        package fubar;
        
        import org.codehaus.jackson.map.ObjectMapper;
        
        public class Foo
        {
         public static void main(String[] args) throws Exception
         {
          ObjectMapper mapper = new ObjectMapper();
          
          String json = "{\"n\":null}"; // {"n":null}
          Bar bar = mapper.readValue(json, Bar.class);
          System.out.println(bar.n); // 0
          
          String json2 = "{\"n\":\"\"}"; // {"n":""}
          Bar bar2 = mapper.readValue(json2, Bar.class);
          // throws JsonMappingException: 
          // Problem deserializing property 'n' 
          // (expected type: [simple type, class int]; actual type: [NULL]), 
          // problem: Can not set int field fubar.Bar.n to null value 
          // (through reference chain: fubar.Bar["n"])
         }
        }
        
        class Bar
        {
         public int n = 42;
        }
        Show
        Programmer Bruce added a comment - The problem occurs with no auto-boxing involved. package fubar; import org.codehaus.jackson.map.ObjectMapper; public class Foo { public static void main( String [] args) throws Exception { ObjectMapper mapper = new ObjectMapper(); String json = "{\" n\ ": null }" ; // { "n" : null } Bar bar = mapper.readValue(json, Bar.class); System .out.println(bar.n); // 0 String json2 = "{\" n\ ":\" \ "}" ; // { "n" :""} Bar bar2 = mapper.readValue(json2, Bar.class); // throws JsonMappingException: // Problem deserializing property 'n' // (expected type: [simple type, class int ]; actual type: [NULL]), // problem: Can not set int field fubar.Bar.n to null value // (through reference chain: fubar.Bar[ "n" ]) } } class Bar { public int n = 42; }
        Hide
        Tatu Saloranta added a comment -

        Interesting. Thanks you for the test case, should be easier to resolve now.

        Show
        Tatu Saloranta added a comment - Interesting. Thanks you for the test case, should be easier to resolve now.
        Hide
        Tatu Saloranta added a comment -

        I fixed this issue, to the best of my knowledge (for all primitive types). Due to number of internal changes, it will be 1.9 only.

        Show
        Tatu Saloranta added a comment - I fixed this issue, to the best of my knowledge (for all primitive types). Due to number of internal changes, it will be 1.9 only.

          People

          • Assignee:
            Tatu Saloranta
            Reporter:
            Programmer Bruce
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: