Skip to content

Commit

Permalink
first part (canSerialize) of #1917
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jan 29, 2018
1 parent be45354 commit b18ecb3
Show file tree
Hide file tree
Showing 8 changed files with 0 additions and 160 deletions.
28 changes: 0 additions & 28 deletions src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2579,34 +2579,6 @@ public <T extends JsonNode> T valueToTree(Object fromValue)
/**********************************************************
*/

/**
* Method that can be called to check whether mapper thinks
* it could serialize an instance of given Class.
* Check is done
* by checking whether a serializer can be found for the type.
*<p>
* NOTE: since this method does NOT throw exceptions, but internal
* processing may, caller usually has little information as to why
* serialization would fail. If you want access to internal {@link Exception},
* call {@link #canSerialize(Class, AtomicReference)} instead.
*
* @return True if mapper can find a serializer for instances of
* given class (potentially serializable), false otherwise (not
* serializable)
*/
public boolean canSerialize(Class<?> type) {
return _serializerProvider().hasSerializerFor(type, null);
}

/**
* Method similar to {@link #canSerialize(Class)} but that can return
* actual {@link Throwable} that was thrown when trying to construct
* serializer: this may be useful in figuring out what the actual problem is.
*/
public boolean canSerialize(Class<?> type, AtomicReference<Throwable> cause) {
return _serializerProvider().hasSerializerFor(type, cause);
}

/**
* Method that can be called to check whether mapper thinks
* it could deserialize an Object of given type.
Expand Down
13 changes: 0 additions & 13 deletions src/main/java/com/fasterxml/jackson/databind/ObjectWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import java.util.concurrent.atomic.AtomicReference;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.io.CharacterEscapes;
Expand Down Expand Up @@ -1052,18 +1051,6 @@ public void acceptJsonFormatVisitor(Class<?> rawType, JsonFormatVisitorWrapper v
acceptJsonFormatVisitor(_config.constructType(rawType), visitor);
}

public boolean canSerialize(Class<?> type) {
return _serializerProvider().hasSerializerFor(type, null);
}

/**
* Method for checking whether instances of given type can be serialized,
* and optionally why (as per {@link Throwable} returned).
*/
public boolean canSerialize(Class<?> type, AtomicReference<Throwable> cause) {
return _serializerProvider().hasSerializerFor(type, cause);
}

/*
/**********************************************************
/* Overridable helper methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1338,36 +1338,6 @@ protected void _reportIncompatibleRootType(Object value, JavaType rootType) thro
rootType, ClassUtil.classNameOf(value)));
}

/**
* Method that will try to find a serializer, either from cache
* or by constructing one; but will not return an "unknown" serializer
* if this cannot be done but rather returns null.
*
* @return Serializer if one can be found, null if not.
*/
protected JsonSerializer<Object> _findExplicitUntypedSerializer(Class<?> runtimeType)
throws JsonMappingException
{
// Fast lookup from local lookup thingy works?
JsonSerializer<Object> ser = _knownSerializers.untypedValueSerializer(runtimeType);
if (ser == null) {
// If not, maybe shared map already has it?
ser = _serializerCache.untypedValueSerializer(runtimeType);
if (ser == null) {
ser = _createAndCacheUntypedSerializer(runtimeType);
}
}
/* 18-Sep-2014, tatu: This is unfortunate patch over related change
* that pushes creation of "unknown type" serializer deeper down
* in BeanSerializerFactory; as a result, we need to "undo" creation
* here.
*/
if (isUnknownTypeSerializer(ser)) {
return null;
}
return ser;
}

/*
/**********************************************************
/* Low-level methods for actually constructing and initializing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.IOException;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

import com.fasterxml.jackson.annotation.ObjectIdGenerator;
import com.fasterxml.jackson.core.JsonGenerator;
Expand Down Expand Up @@ -229,46 +228,10 @@ protected Map<Object,WritableObjectId> _createObjectIdMap()
/**********************************************************
*/

/**
* Method that can be called to see if this serializer provider
* can find a serializer for an instance of given class.
*<p>
* Note that no Exceptions are thrown, including unchecked ones:
* implementations are to swallow exceptions if necessary.
*/
public boolean hasSerializerFor(Class<?> cls, AtomicReference<Throwable> cause)
{
// 07-Nov-2015, tatu: One special case, Object.class; will work only if
// empty beans are allowed or custom serializer registered. Easiest to
// check here.
if (cls == Object.class) {
if (!_config.isEnabled(SerializationFeature.FAIL_ON_EMPTY_BEANS)) {
return true;
}
}

try {
JsonSerializer<?> ser = _findExplicitUntypedSerializer(cls);
return (ser != null);
} catch (JsonMappingException e) {
if (cause != null) {
cause.set(e);
}
} catch (RuntimeException e) {
if (cause == null) { // earlier behavior
throw e;
}
cause.set(e);
}
return false;
}

/**
* Accessor for the {@link JsonGenerator} currently in use for serializing
* content. Null for blueprint instances; non-null for actual active
* provider instances.
*
* @since 2.8
*/
@Override
public JsonGenerator getGenerator() {
Expand Down
29 changes: 0 additions & 29 deletions src/test/java/com/fasterxml/jackson/databind/ObjectMapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,35 +271,6 @@ public void testCustomDefaultPrettyPrinter() throws Exception
assertEquals("[1,2]", m.writer().without(SerializationFeature.INDENT_OUTPUT)
.writeValueAsString(input));
}

// For [databind#703], [databind#978]
public void testNonSerializabilityOfObject()
{
ObjectMapper m = new ObjectMapper();
assertFalse(m.canSerialize(Object.class));
// but this used to pass, incorrectly, second time around
assertFalse(m.canSerialize(Object.class));

// [databind#978]: Different answer if empty Beans ARE allowed
m = new ObjectMapper();
m.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
assertTrue(m.canSerialize(Object.class));
assertTrue(MAPPER.writer().without(SerializationFeature.FAIL_ON_EMPTY_BEANS)
.canSerialize(Object.class));
assertFalse(MAPPER.writer().with(SerializationFeature.FAIL_ON_EMPTY_BEANS)
.canSerialize(Object.class));
}

// for [databind#756]
public void testEmptyBeanSerializability()
{
// with default settings, error
assertFalse(MAPPER.writer().with(SerializationFeature.FAIL_ON_EMPTY_BEANS)
.canSerialize(EmptyBean.class));
// but with changes
assertTrue(MAPPER.writer().without(SerializationFeature.FAIL_ON_EMPTY_BEANS)
.canSerialize(EmptyBean.class));
}

// for [databind#898]
public void testSerializerProviderAccess() throws Exception
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,6 @@ public void testPolymorphicWithTyping() throws Exception
assertEquals(aposToQuotes("{'type':'B','b':-5}"), json);
}

public void testCanSerialize() throws Exception
{
assertTrue(MAPPER.writer().canSerialize(String.class));
assertTrue(MAPPER.writer().canSerialize(String.class, null));
}

public void testNoPrefetch() throws Exception
{
ObjectWriter w = MAPPER.writer()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.IOException;

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.fasterxml.jackson.databind.ser;

import java.util.concurrent.atomic.AtomicReference;

import com.fasterxml.jackson.core.json.JsonFactory;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.cfg.GeneratorSettings;
Expand Down Expand Up @@ -32,19 +30,5 @@ public void testFindExplicit() throws JsonMappingException
assertNotNull(prov.getDefaultNullValueSerializer());
// as well as 'unknown type' one (throws exception)
assertNotNull(prov.getUnknownTypeSerializer(getClass()));

assertTrue(prov.createInstance(config, genSettings, f).hasSerializerFor(String.class, null));
// call twice to verify it'll be cached (second code path)
assertTrue(prov.createInstance(config, genSettings, f).hasSerializerFor(String.class, null));

assertTrue(prov.createInstance(config, genSettings, f).hasSerializerFor(MyBean.class, null));
assertTrue(prov.createInstance(config, genSettings, f).hasSerializerFor(MyBean.class, null));

// And then some negative testing
AtomicReference<Throwable> cause = new AtomicReference<Throwable>();
assertFalse(prov.createInstance(config, genSettings, f).hasSerializerFor(NoPropsBean.class, cause));
Throwable t = cause.get();
// no actual exception: just fails since there are no properties
assertNull(t);
}
}

0 comments on commit b18ecb3

Please sign in to comment.