Skip to content

Commit 960e49e

Browse files
committed
Minor improvements to error reporting, related to work on #691
1 parent 186b89d commit 960e49e

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

src/main/java/com/fasterxml/jackson/databind/JsonSerializer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ public void serializeWithType(T value, JsonGenerator gen, SerializerProvider ser
139139
if (clz == null) {
140140
clz = value.getClass();
141141
}
142-
throw new UnsupportedOperationException("Type id handling not implemented for type "+clz.getName());
142+
throw serializers.mappingException("Type id handling not implemented for type %s (by serializer of type %s)",
143+
clz.getName(), getClass().getName());
143144
}
144145

145146
/*

src/main/java/com/fasterxml/jackson/databind/SerializerProvider.java

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,7 @@ public JsonSerializer<?> handleSecondaryContextualization(JsonSerializer<?> ser,
908908

909909
/*
910910
/********************************************************
911-
/* Convenience methods
911+
/* Convenience methods for serializing using default methods
912912
/********************************************************
913913
*/
914914

@@ -919,8 +919,7 @@ public JsonSerializer<?> handleSecondaryContextualization(JsonSerializer<?> ser,
919919
* field values are best handled calling
920920
* {@link #defaultSerializeField} instead.
921921
*/
922-
public final void defaultSerializeValue(Object value, JsonGenerator jgen)
923-
throws IOException, JsonProcessingException
922+
public final void defaultSerializeValue(Object value, JsonGenerator jgen) throws IOException
924923
{
925924
if (value == null) {
926925
if (_stdNullValueSerializer) { // minor perf optimization
@@ -940,7 +939,7 @@ public final void defaultSerializeValue(Object value, JsonGenerator jgen)
940939
* null) using standard serializer locating functionality.
941940
*/
942941
public final void defaultSerializeField(String fieldName, Object value, JsonGenerator jgen)
943-
throws IOException, JsonProcessingException
942+
throws IOException
944943
{
945944
jgen.writeFieldName(fieldName);
946945
if (value == null) {
@@ -958,12 +957,6 @@ public final void defaultSerializeField(String fieldName, Object value, JsonGene
958957
}
959958
}
960959

961-
/*
962-
/**********************************************************
963-
/* Convenience methods
964-
/**********************************************************
965-
*/
966-
967960
/**
968961
* Method that will handle serialization of Date(-like) values, using
969962
* {@link SerializationConfig} settings to determine expected serialization
@@ -972,7 +965,7 @@ public final void defaultSerializeField(String fieldName, Object value, JsonGene
972965
* Java convention (and not date-only values like in SQL)
973966
*/
974967
public final void defaultSerializeDateValue(long timestamp, JsonGenerator jgen)
975-
throws IOException, JsonProcessingException
968+
throws IOException
976969
{
977970
// [JACKSON-87]: Support both numeric timestamps and textual
978971
if (isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)) {
@@ -990,7 +983,7 @@ public final void defaultSerializeDateValue(long timestamp, JsonGenerator jgen)
990983
* Java convention (and not date-only values like in SQL)
991984
*/
992985
public final void defaultSerializeDateValue(Date date, JsonGenerator jgen)
993-
throws IOException, JsonProcessingException
986+
throws IOException
994987
{
995988
// [JACKSON-87]: Support both numeric timestamps and textual
996989
if (isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)) {
@@ -1006,7 +999,7 @@ public final void defaultSerializeDateValue(Date date, JsonGenerator jgen)
1006999
* value (and if using textual representation, configured date format)
10071000
*/
10081001
public void defaultSerializeDateKey(long timestamp, JsonGenerator jgen)
1009-
throws IOException, JsonProcessingException
1002+
throws IOException
10101003
{
10111004
if (isEnabled(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS)) {
10121005
jgen.writeFieldName(String.valueOf(timestamp));
@@ -1020,18 +1013,16 @@ public void defaultSerializeDateKey(long timestamp, JsonGenerator jgen)
10201013
* based on {@link SerializationFeature#WRITE_DATE_KEYS_AS_TIMESTAMPS}
10211014
* value (and if using textual representation, configured date format)
10221015
*/
1023-
public void defaultSerializeDateKey(Date date, JsonGenerator jgen)
1024-
throws IOException, JsonProcessingException
1016+
public void defaultSerializeDateKey(Date date, JsonGenerator jgen) throws IOException
10251017
{
10261018
if (isEnabled(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS)) {
10271019
jgen.writeFieldName(String.valueOf(date.getTime()));
10281020
} else {
10291021
jgen.writeFieldName(_dateFormat().format(date));
10301022
}
10311023
}
1032-
1033-
public final void defaultSerializeNull(JsonGenerator jgen)
1034-
throws IOException, JsonProcessingException
1024+
1025+
public final void defaultSerializeNull(JsonGenerator jgen) throws IOException
10351026
{
10361027
if (_stdNullValueSerializer) { // minor perf optimization
10371028
jgen.writeNull();
@@ -1040,12 +1031,28 @@ public final void defaultSerializeNull(JsonGenerator jgen)
10401031
}
10411032
}
10421033

1034+
/*
1035+
/********************************************************
1036+
/* Error reporting
1037+
/********************************************************
1038+
*/
1039+
1040+
/**
1041+
* @since 2.6
1042+
*/
1043+
public JsonMappingException mappingException(String message, Object... args) {
1044+
if (args != null && args.length > 0) {
1045+
message = String.format(message, args);
1046+
}
1047+
return new JsonMappingException(message);
1048+
}
1049+
10431050
/*
10441051
/********************************************************
10451052
/* Helper methods
10461053
/********************************************************
10471054
*/
1048-
1055+
10491056
protected void _reportIncompatibleRootType(Object value, JavaType rootType)
10501057
throws IOException, JsonProcessingException
10511058
{
@@ -1099,7 +1106,7 @@ protected JsonSerializer<Object> _findExplicitUntypedSerializer(Class<?> runtime
10991106
/* serializers
11001107
/**********************************************************
11011108
*/
1102-
1109+
11031110
/**
11041111
* Method that will try to construct a value serializer; and if
11051112
* one is successfully created, cache it for reuse.

0 commit comments

Comments
 (0)