hibernate/hibernate-orm · error · IllegalArgumentException

Could not deserialize string to java type: {}

Error message

Could not deserialize string to java type: {}

What it means

Jackson3JsonFormatMapper is Hibernate's Jackson 3 (tools.jackson) based JSON FormatMapper, used for @JdbcTypeCode(SqlTypes.JSON) attributes. fromString() wraps any JacksonException thrown by jsonMapper.readValue() into IllegalArgumentException('Could not deserialize string to java type: <type>') with the original exception as cause — the JSON column content could not be bound to the mapped Java type.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/type/format/jackson/Jackson3JsonFormatMapper.java:79

	}

	@Override
	public boolean supportsSourceType(Class<?> sourceType) {
		return JsonParser.class.isAssignableFrom( sourceType );
	}

	@Override
	public boolean supportsTargetType(Class<?> targetType) {
		return JsonGenerator.class.isAssignableFrom( targetType );
	}

	@Override
	public <T> T fromString(CharSequence charSequence, Type type) {
		try {
			return jsonMapper.readValue( charSequence.toString(), jsonMapper.constructType( type ) );
		}
		catch (JacksonException e) {
			throw new IllegalArgumentException( "Could not deserialize string to java type: " + type, e );
		}
	}

	@Override
	public <T> String toString(T value, Type type) {
		try {
			return jsonMapper.writerFor( jsonMapper.constructType( type ) ).writeValueAsString( value );
		}
		catch (JacksonException e) {
			throw new IllegalArgumentException( "Could not serialize object of java type: " + type, e );
		}
	}
}

View on GitHub (pinned to fad1729dce)

Solutions

  1. Unwrap and read the cause (IllegalArgumentException.getCause()) — the JacksonException states the exact problem and input location
  2. Align the mapped Java type with the stored JSON: adjust fields, or tolerate drift with @JsonIgnoreProperties(ignoreUnknown = true) / @JsonIgnore on the DTO
  3. Build a JsonMapper with the modules/features you need and inject it: new Jackson3JsonFormatMapper(mapper) via the hibernate.type.json_format_mapper setting
  4. Repair malformed column data identified by the exception

Example fix

// before: default mapper, load fails on JSON/entity mismatch
Map<String, Object> props = session.find(MyEntity.class, id).getProps();
// after: supply a tolerant, preconfigured mapper
JsonMapper mapper = JsonMapper.builder()
        .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
        .build();
properties.put(AvailableSettings.JSON_FORMAT_MAPPER, () -> new Jackson3JsonFormatMapper(mapper));
Defensive patterns

Strategy: try-catch

Validate before calling

// sanity-check the column shape against the mapped type before/while loading
static <T> void validateJsonMatches(String json, Class<T> type) {
    try {
        tools.jackson.databind.json.JsonMapper mapper = tools.jackson.databind.json.JsonMapper.builder().build();
        mapper.readValue(json, mapper.constructType(type));
    } catch (tools.jackson.core.JacksonException e) {
        throw new IllegalArgumentException("Column JSON does not match " + type + ": " + e.getMessage(), e);
    }
}

Try / catch

try {
    MyEntity e = session.find(MyEntity.class, id);
} catch (IllegalArgumentException ex) {
    if (ex.getMessage() != null && ex.getMessage().startsWith("Could not deserialize string to java type")) {
        Throwable cause = ex.getCause(); // the real JacksonException with location details
        throw new DataQualityException("JSON column incompatible with mapping: " + cause.getMessage(), ex);
    }
    throw ex;
}

Prevention

When it happens

Trigger: Loading or querying an entity with a JSON-mapped attribute where: the column holds malformed JSON; the JSON shape does not match the type (unknown fields under strict settings, number-vs-string mismatches); the target type lacks a usable constructor/creator; or a required module/serializer for a field type is not registered on the mapper Hibernate uses.

Common situations: Schema drift between the JSON producer and the Hibernate mapping (renamed/added fields); another application writing the column; migrating an app from Jackson 2 to Jackson 3 where module registration and defaults differ; switching the JSON column representation between dialects.

Related errors


AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22). Data as JSON: /api/errors/5d7b017e6b937c69. Report an issue: GitHub.