localization: standardize (de)serialization error messages
diff --git a/src/main/java/org/eclipse/yasson/internal/deserializer/JsonbCreatorDeserializer.java b/src/main/java/org/eclipse/yasson/internal/deserializer/JsonbCreatorDeserializer.java index 9231c50..dbab8c8 100644 --- a/src/main/java/org/eclipse/yasson/internal/deserializer/JsonbCreatorDeserializer.java +++ b/src/main/java/org/eclipse/yasson/internal/deserializer/JsonbCreatorDeserializer.java
@@ -1,5 +1,6 @@ /* * Copyright (c) 2021, 2023 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2026 Contributors to the Eclipse Foundation * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0 which is available at @@ -86,7 +87,8 @@ paramValues.put(key, o); } } catch (JsonbException e) { - throw new JsonbException("Unable to deserialize property '" + key + "' because of: " + e.getMessage(), e); + throw new JsonbException( + Messages.getMessage(MessageKeys.DESERIALIZE_PROPERTY_ERROR, key, e.getMessage()), e); } } else if (failOnUnknownProperties && !ignoredProperties.contains(key)) { throw new JsonbException(Messages.getMessage(MessageKeys.UNKNOWN_JSON_PROPERTY, key, clazz)); @@ -110,7 +112,8 @@ context.getDeferredDeserializers().clear(); return context.getInstance(); default: - throw new JsonbException("Unexpected state: " + parser.currentEvent()); + throw new JsonbException(Messages.getMessage(MessageKeys.INTERNAL_ERROR, + "Unexpected parser state: " + parser.currentEvent())); } } return context.getInstance();
diff --git a/src/main/java/org/eclipse/yasson/internal/deserializer/ObjectDeserializer.java b/src/main/java/org/eclipse/yasson/internal/deserializer/ObjectDeserializer.java index e5aa541..8907a47 100644 --- a/src/main/java/org/eclipse/yasson/internal/deserializer/ObjectDeserializer.java +++ b/src/main/java/org/eclipse/yasson/internal/deserializer/ObjectDeserializer.java
@@ -1,5 +1,6 @@ /* * Copyright (c) 2021, 2023 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2026 Contributors to the Eclipse Foundation * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0 which is available at @@ -75,7 +76,8 @@ try { propertyDeserializerChains.get(key).deserialize(parser, context); } catch (JsonbException e) { - throw new JsonbException("Unable to deserialize property '" + key + "' because of: " + e.getMessage(), e); + throw new JsonbException( + Messages.getMessage(MessageKeys.DESERIALIZE_PROPERTY_ERROR, key, e.getMessage()), e); } } else if (failOnUnknownProperty && !ignoredProperties.contains(key)) { throw new JsonbException(Messages.getMessage(MessageKeys.UNKNOWN_JSON_PROPERTY, key, rawClass)); @@ -89,7 +91,8 @@ case END_OBJECT: return context.getInstance(); default: - throw new JsonbException("Unexpected state: " + parser.currentEvent()); + throw new JsonbException(Messages.getMessage(MessageKeys.INTERNAL_ERROR, + "Unexpected parser state: " + parser.currentEvent())); } } return context.getInstance();
diff --git a/src/main/java/org/eclipse/yasson/internal/deserializer/ValueSetterDeserializer.java b/src/main/java/org/eclipse/yasson/internal/deserializer/ValueSetterDeserializer.java index 80f50d1..76a047a 100644 --- a/src/main/java/org/eclipse/yasson/internal/deserializer/ValueSetterDeserializer.java +++ b/src/main/java/org/eclipse/yasson/internal/deserializer/ValueSetterDeserializer.java
@@ -19,6 +19,8 @@ import jakarta.json.bind.JsonbException; import org.eclipse.yasson.internal.DeserializationContextImpl; +import org.eclipse.yasson.internal.properties.MessageKeys; +import org.eclipse.yasson.internal.properties.Messages; /** * Value setter. Invokes created {@link MethodHandle} to set deserialized value to the instance. @@ -40,7 +42,8 @@ } catch (Throwable e) { // Do not call toString() of the instance, which is only partially deserialized String typeName = instance == null ? "null" : instance.getClass().getName(); - throw new JsonbException("Error setting value on: " + typeName, e); + throw new JsonbException( + Messages.getMessage(MessageKeys.SETTING_VALUE_WITH, typeName, valueSetter), e); } }
diff --git a/src/main/java/org/eclipse/yasson/internal/properties/MessageKeys.java b/src/main/java/org/eclipse/yasson/internal/properties/MessageKeys.java index 2171f8b..654e0ac 100644 --- a/src/main/java/org/eclipse/yasson/internal/properties/MessageKeys.java +++ b/src/main/java/org/eclipse/yasson/internal/properties/MessageKeys.java
@@ -1,5 +1,6 @@ /* * Copyright (c) 2015, 2026 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2026 Contributors to the Eclipse Foundation * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0 which is available at @@ -109,11 +110,15 @@ */ CREATING_HANDLES("creatingHandles"), /** - * Could not get field value with method. + * Could not get field value with method. + * {0} is the class name of the object + * {1} is the method invoked */ GETTING_VALUE_WITH("gettingValueWith"), /** - * Could not set field value with method. + * Could not set field value with method. + * {0} is the class name of the object. + * {1} is the method invoked */ SETTING_VALUE_WITH("settingValueWith"), /** @@ -230,9 +235,18 @@ INTERNAL_ERROR("internalError"), /** * There has been an error during property serialization. + * {0} is the property name + * {1} is the canonical class name of the object being serialized + * {2} is the chained error message */ SERIALIZE_PROPERTY_ERROR("serializePropertyError"), /** + * There has been an error during property deserialization. + * {0} is the property name + * {1} is the chained error message + */ + DESERIALIZE_PROPERTY_ERROR("deserializePropertyError"), + /** * There has been an error during value deserialization. */ DESERIALIZE_VALUE_ERROR("deserializeValueError"),
diff --git a/src/main/java/org/eclipse/yasson/internal/serializer/ObjectSerializer.java b/src/main/java/org/eclipse/yasson/internal/serializer/ObjectSerializer.java index f9383cf..e61e05c 100644 --- a/src/main/java/org/eclipse/yasson/internal/serializer/ObjectSerializer.java +++ b/src/main/java/org/eclipse/yasson/internal/serializer/ObjectSerializer.java
@@ -41,8 +41,9 @@ context.setKey(key); serializer.serialize(value, generator, context); } catch (Exception e) { - throw new JsonbException(Messages.getMessage(MessageKeys.SERIALIZE_PROPERTY_ERROR, key, - value.getClass().getCanonicalName()), e); + throw new JsonbException( + Messages.getMessage(MessageKeys.SERIALIZE_PROPERTY_ERROR, key, + value.getClass().getCanonicalName(), e.getMessage()), e); } finally { context.setKey(null); }
diff --git a/src/main/java/org/eclipse/yasson/internal/serializer/ValueGetterSerializer.java b/src/main/java/org/eclipse/yasson/internal/serializer/ValueGetterSerializer.java index d22c1a4..44278b7 100644 --- a/src/main/java/org/eclipse/yasson/internal/serializer/ValueGetterSerializer.java +++ b/src/main/java/org/eclipse/yasson/internal/serializer/ValueGetterSerializer.java
@@ -1,5 +1,6 @@ /* * Copyright (c) 2021, 2022 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2026 Contributors to the Eclipse Foundation * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0 which is available at @@ -18,6 +19,8 @@ import jakarta.json.stream.JsonGenerator; import org.eclipse.yasson.internal.SerializationContextImpl; +import org.eclipse.yasson.internal.properties.MessageKeys; +import org.eclipse.yasson.internal.properties.Messages; /** * Extractor of the serialized value from the instance. @@ -38,7 +41,8 @@ try { object = valueGetter.invoke(value); } catch (Throwable e) { - throw new JsonbException("Error getting value on: " + value.getClass().getName(), e); + throw new JsonbException( + Messages.getMessage(MessageKeys.GETTING_VALUE_WITH, value.getClass().getName(), valueGetter), e); } delegate.serialize(object, generator, context); }
diff --git a/src/main/resources/yasson-messages.properties b/src/main/resources/yasson-messages.properties index 59b7d23..c2cdbdc 100644 --- a/src/main/resources/yasson-messages.properties +++ b/src/main/resources/yasson-messages.properties
@@ -1,5 +1,6 @@ # # Copyright (c) 2016, 2022 Oracle and/or its affiliates. All rights reserved. +# Copyright (c) 2026 Contributors to the Eclipse Foundation # # This program and the accompanying materials are made available under the # terms of the Eclipse Public License v. 2.0 which is available at @@ -32,8 +33,8 @@ processFromJson=Process class: {0} from json using converter: {1} processToJson=Process class: {0} to json using converter: {1} creatingHandles=Error creating handles. -settingValueWith=Error setting value with: {0} -gettingValueWith=Error getting value with: {0} +settingValueWith=Error setting value on {0} with {1} +gettingValueWith=Error getting value on {0} with {1} unpairedSurrogate=String contains unpaired surrogate. adapterException=Problem adapting object of type {0} to {1} in class {2} adapterFound=Found adapter from type {0} to type {1}. @@ -67,7 +68,8 @@ incompatibleFactoryCreatorReturnType=Return type of creator {0} must be of type {1}. multipleJsonbCreators=More than one @JsonbCreator declared in class {0}. internalError=Internal error: {0} -serializePropertyError=Unable to serialize property ''{0}'' from {1} +serializePropertyError=Unable to serialize property ''{0}'' from {1} because of: {2} +deserializePropertyError=Unable to deserialize property ''{0}'' because of: {1} deserializeValueError=Error deserialize JSON value into type: {0}. parsingNumber=Error parsing number {0} with format {1}. unknownBinaryDataStrategy=Unknown binary data strategy: {0}