Skip to content

Commit

Permalink
Merge pull request #113 from FasterXML/tatu/2.17/78-boolean-wrapper-n…
Browse files Browse the repository at this point in the history
…ulls

Fix part of #78: handle `java.lang.Boolean` separate from primitive `boolean`
  • Loading branch information
cowtowncoder authored Feb 16, 2024
2 parents 72724b7 + 4d8df8c commit 1781de7
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ public void writeField(String fieldName, Object value, int type) throws IOExcept
// Scalar types:

case SER_BOOLEAN:
case SER_BOOLEAN_WRAPPER:
writeBooleanField(fieldName, ((Boolean) value).booleanValue());
return;
case SER_CHAR:
Expand Down Expand Up @@ -348,6 +349,7 @@ protected void _writeValue(Object value, int type) throws IOException
// Other scalar types:

case SER_BOOLEAN:
case SER_BOOLEAN_WRAPPER:
writeBooleanValue(((Boolean) value).booleanValue());
return;
case SER_CHAR:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public Object readNext(JSONReader reader, JsonParser p) throws IOException
// Other scalar types:

case SER_BOOLEAN:
case SER_BOOLEAN_WRAPPER:
{
Boolean b = p.nextBooleanValue();
if (b != null) {
Expand Down Expand Up @@ -139,14 +140,16 @@ public Object read(JSONReader reader, JsonParser p) throws IOException
// Other scalar types:

case SER_BOOLEAN:
case SER_BOOLEAN_WRAPPER:
switch (p.currentTokenId()) {
case JsonTokenId.ID_TRUE:
return Boolean.TRUE;
case JsonTokenId.ID_FALSE:
return Boolean.FALSE;
case JsonTokenId.ID_NULL:
// 07-Jul-2020, tatu: since `boolean` and `java.lang.Boolean` both handled
// here, can not (alas!) separate yet
if (_typeId == SER_BOOLEAN_WRAPPER) {
return null;
}
return Boolean.FALSE;

case JsonTokenId.ID_STRING:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,19 @@ public abstract class ValueLocatorBase
// // // Other specific scalar types

public final static int SER_BOOLEAN = 21;
public final static int SER_CHAR = 22;
public final static int SER_BOOLEAN_WRAPPER = 22;
public final static int SER_CHAR = 23;

public final static int SER_ENUM = 23;
public final static int SER_ENUM = 24;

public final static int SER_DATE = 24;
public final static int SER_CALENDAR = 25;
public final static int SER_DATE = 25;
public final static int SER_CALENDAR = 26;

public final static int SER_CLASS = 26;
public final static int SER_FILE = 27;
public final static int SER_UUID = 28;
public final static int SER_URL = 29;
public final static int SER_URI = 30;
public final static int SER_CLASS = 27;
public final static int SER_FILE = 28;
public final static int SER_UUID = 29;
public final static int SER_URL = 30;
public final static int SER_URI = 31;


// // // Iterate-able types
Expand All @@ -114,7 +115,7 @@ public abstract class ValueLocatorBase
* Anything that implements {@link java.lang.Iterable}, but not
* {@link java.util.Collection}.
*/
public final static int SER_ITERABLE = 31;
public final static int SER_ITERABLE = 32;

/*
/**********************************************************************
Expand Down Expand Up @@ -170,7 +171,7 @@ protected int _findSimpleType(Class<?> raw, boolean forSer)
throw new IllegalArgumentException("Unrecognized primitive type: "+raw.getName());
}
if (raw == Boolean.class) {
return SER_BOOLEAN;
return SER_BOOLEAN_WRAPPER;
}
if (Number.class.isAssignableFrom(raw)) {
if (raw == Integer.class) return SER_NUMBER_INTEGER;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.fasterxml.jackson.jr.failing;

import java.util.LinkedHashMap;
import java.util.Map;

import com.fasterxml.jackson.jr.ob.JSON;
import com.fasterxml.jackson.jr.ob.TestBase;

Expand All @@ -21,32 +18,13 @@ static class LongWrapper {
static class LongPrimitiveWrapper {
public long value;
}
static class BooleanWrapper {
public Boolean value;
}
static class BooleanPrimitiveWrapper {
public boolean value;
}
static class DoubleWrapper {
public Double value;
}
static class DoublePrimitiveWrapper {
public double value;
}

// Test to verify that outputting of nulls is configurable
public void testMapNullEntries() throws Exception
{
Map<String,Object> map = new LinkedHashMap<String,Object>();
map.put("a", 1);
map.put("b", null);
// By default we do NOT write null-valued entries:
assertEquals("{\"a\":1}", JSON.std.asString(map));
// but we can disable it easily
assertEquals("{\"a\":1,\"b\":null}",
JSON.std.with(JSON.Feature.WRITE_NULL_PROPERTIES).asString(map));
}

// [jackson-jr#78], int/Integer

public void testIntPrimitive() throws Exception
Expand Down Expand Up @@ -97,30 +75,6 @@ public void testLongWrapper() throws Exception

// [jackson-jr#78], boolean/Boolean

public void testBooleanPrimitive() throws Exception
{
BooleanPrimitiveWrapper w = JSON.std.beanFrom(BooleanPrimitiveWrapper.class,
a2q("{'value':true}"));
assertTrue(w.value);

w = JSON.std.beanFrom(BooleanPrimitiveWrapper.class,
a2q("{'value':null}"));
assertFalse(w.value);
}

public void testBooleanWrapper() throws Exception
{
BooleanWrapper w = JSON.std.beanFrom(BooleanWrapper.class,
a2q("{'value':true}"));
assertEquals(Boolean.TRUE, w.value);

w = JSON.std.beanFrom(BooleanWrapper.class,
a2q("{'value':null}"));
assertNull(w.value);
}

// [jackson-jr#78], boolean/Boolean

public void testDoublePrimitive() throws Exception
{
DoublePrimitiveWrapper w = JSON.std.beanFrom(DoublePrimitiveWrapper.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ static class StringBean {
public String str = "a";
}

static class BooleanWrapper {
public Boolean value;
}
static class BooleanPrimitiveWrapper {
public boolean value;
}

// Test to verify that outputting of nulls is configurable
public void testMapNullEntries() throws Exception
{
Expand Down Expand Up @@ -42,4 +49,29 @@ public void testNullForByteArray() throws Exception
Bean107 bean = JSON.std.beanFrom(Bean107.class, a2q("{'b':null}"));
assertNull(bean.b);
}

// [jackson-jr#78], boolean/Boolean

public void testBooleanPrimitive() throws Exception
{
BooleanPrimitiveWrapper w = JSON.std.beanFrom(BooleanPrimitiveWrapper.class,
a2q("{'value':true}"));
assertTrue(w.value);

w = JSON.std.beanFrom(BooleanPrimitiveWrapper.class,
a2q("{'value':null}"));
assertFalse(w.value);
}

public void testBooleanWrapper() throws Exception
{
BooleanWrapper w = JSON.std.beanFrom(BooleanWrapper.class,
a2q("{'value':true}"));
assertEquals(Boolean.TRUE, w.value);

w = JSON.std.beanFrom(BooleanWrapper.class,
a2q("{'value':null}"));
assertNull(w.value);
}

}

0 comments on commit 1781de7

Please sign in to comment.