Skip to content

Commit

Permalink
Fix #2
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jun 3, 2020
1 parent 8c19755 commit 75864ba
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public class JSR353Module extends SimpleModule
public JSR353Module() {
super(PackageVersion.VERSION); //ModuleVersion.instance.version());

JsonProvider jp = JsonProvider.provider();
final JsonProvider jp = JsonProvider.provider();
_builderFactory = jp.createBuilderFactory(Collections.<String, Object>emptyMap());
final JsonValueDeserializer jsonValueDeser = new JsonValueDeserializer(_builderFactory);
final JsonValueDeserializer jsonValueDeser = new JsonValueDeserializer(JsonValue.class, _builderFactory);
final JsonPatchDeserializer jsonPatchDeser = new JsonPatchDeserializer(jsonValueDeser);
final JsonMergePatchDeserializer jsonMergePatchDeser = new JsonMergePatchDeserializer(jsonValueDeser);

Expand All @@ -35,8 +35,11 @@ public JsonDeserializer<?> findBeanDeserializer(
DeserializationConfig config,
BeanDescription beanDesc
) {
if (JsonValue.class.isAssignableFrom(type.getRawClass())) {
return jsonValueDeser;
if (type.isTypeOrSubTypeOf(JsonValue.class)) {
if (type.hasRawClass(JsonValue.class)) {
return jsonValueDeser;
}
return new JsonValueDeserializer(type.getRawClass(), _builderFactory);
}
if (JsonPatch.class.isAssignableFrom(type.getRawClass())) {
return jsonPatchDeser;
Expand All @@ -55,8 +58,8 @@ public JsonDeserializer<?> findCollectionDeserializer(
TypeDeserializer elementTypeDeserializer,
JsonDeserializer<?> elementDeserializer
) {
if (JsonArray.class.isAssignableFrom(type.getRawClass())) {
return jsonValueDeser;
if (type.hasRawClass(JsonArray.class)) {
return new JsonValueDeserializer(type.getRawClass(), _builderFactory);
}
return null;
}
Expand All @@ -70,8 +73,8 @@ public JsonDeserializer<?> findMapDeserializer(
TypeDeserializer elementTypeDeserializer,
JsonDeserializer<?> elementDeserializer
) {
if (JsonObject.class.isAssignableFrom(type.getRawClass())) {
return jsonValueDeser;
if (type.hasRawClass(JsonObject.class)) {
return new JsonValueDeserializer(type.getRawClass(), _builderFactory);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,68 @@
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonParser.NumberType;
import com.fasterxml.jackson.core.JsonToken;

import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
import com.fasterxml.jackson.databind.util.ClassUtil;

public class JsonValueDeserializer extends StdDeserializer<JsonValue> {
private static final long serialVersionUID = 1L;

protected final JsonBuilderFactory _builderFactory;

// @since 2.12
protected final boolean _forJsonValue;

@Deprecated // since 2.12
public JsonValueDeserializer(JsonBuilderFactory bf) {
super(JsonValue.class);
this(JsonValue.class, bf);
}

// @since 2.12
public JsonValueDeserializer(Class<?> target, JsonBuilderFactory bf) {
super(target);
_builderFactory = bf;
_forJsonValue = (target == JsonValue.class);
}

@Override
public JsonValue deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException
{
JsonValue v;
switch (p.getCurrentToken()) {
case START_OBJECT:
return _deserializeObject(p, ctxt);
v = _deserializeObject(p, ctxt);
break;
case START_ARRAY:
return _deserializeArray(p, ctxt);
v = _deserializeArray(p, ctxt);
break;
default:
return _deserializeScalar(p, ctxt);
v = _deserializeScalar(p, ctxt);
}

if (!_forJsonValue) {
if (!handledType().isAssignableFrom(v.getClass())) {
ctxt.reportInputMismatch(handledType(),
"Expected %s, but encountered %s Value",
ClassUtil.getClassDescription(handledType()),
v.getValueType().toString()
);
}
}
return v;
}

@Override
public JsonValue getNullValue(final DeserializationContext ctxt) {
return JsonValue.NULL;
// 02-Jun-2020, tatu: JsonValue.NULL only allowed if nominal type
// compatible
if (_forJsonValue) {
return JsonValue.NULL;
}
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.fasterxml.jackson.datatype.jsr353;

import javax.json.JsonObject;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;

public class DeserViaCreatorTest extends TestBase
{
static class Pojo {
String text;
JsonObject object;

@JsonCreator
public Pojo(@JsonProperty("s") String s, @JsonProperty("o") JsonObject o) {
text = s;
object = o;
}
}

public void testCreatorDeser() throws Exception
{
final ObjectMapper mapper = sharedMapper();
Pojo p = mapper.readerFor(Pojo.class)
.readValue( "{\"s\": \"String\", \"o\": { \"a\": 1, \"b\": \"2\" } }");
assertNotNull(p);
assertNotNull(p.object);

p = mapper.readerFor(Pojo.class).readValue("{\"s\": \"String\"}");
assertNotNull(p);
// has to remain `null` as JsonValue.NULL is not JsonObject
assertNull(p.object);
}
}
5 changes: 3 additions & 2 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ Modules:
=== Releases ===
------------------------------------------------------------------------

2.11.0 (not yet released)
2.11.1 (not yet released)

-
#2: (jsr-353) Input `null` being deserialized as Java null value instead
of `JsonValue.NULL`

2.11.0 (26-Apr-2020)

Expand Down

0 comments on commit 75864ba

Please sign in to comment.