Skip to content

Commit cb0f340

Browse files
committed
Element type for map keys. Allow reading non-object JSON for JsonSerializable.
1 parent 7306654 commit cb0f340

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/com/esotericsoftware/jsonbeans/Json.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ public void setUsePrototypes (boolean usePrototypes) {
136136
this.usePrototypes = usePrototypes;
137137
}
138138

139-
/** Sets the type of elements in a collection. When the element type is known, the class for each element in the collection does
140-
* not need to be written unless different from the element type. */
139+
/** Sets the type of elements in a collection. When the element type is known, the class for each element in the collection
140+
* does not need to be written unless different from the element type. */
141141
public void setElementType (Class type, String fieldName, Class elementType) {
142142
ObjectMap<String, FieldMetadata> fields = getFields(type);
143143
FieldMetadata metadata = fields.get(fieldName);
@@ -434,8 +434,8 @@ public void writeValue (Object value, Class knownType) {
434434
writeValue(value, knownType, null);
435435
}
436436

437-
/** Writes the value, writing the class of the object if it differs from the specified known type. The specified element type is
438-
* used as the default type for collections.
437+
/** Writes the value, writing the class of the object if it differs from the specified known type. The specified element type
438+
* is used as the default type for collections.
439439
* @param value May be null.
440440
* @param knownType May be null if the type is unknown.
441441
* @param elementType May be null if the type is unknown. */
@@ -897,6 +897,13 @@ public <T> T readValue (Class<T> type, Class elementType, JsonValue jsonData) {
897897
if (type != null) {
898898
JsonSerializer serializer = classToSerializer.get(type);
899899
if (serializer != null) return (T)serializer.read(this, jsonData, type);
900+
901+
if (JsonSerializable.class.isAssignableFrom(type)) {
902+
// A Serializable may be read as an array, string, etc, even though it will be written as an object.
903+
Object object = newInstance(type);
904+
((JsonSerializable)object).read(this, jsonData);
905+
return (T)object;
906+
}
900907
}
901908

902909
if (jsonData.isArray()) {
@@ -1042,8 +1049,12 @@ public FieldMetadata (Field field) {
10421049
Type genericType = field.getGenericType();
10431050
if (genericType instanceof ParameterizedType) {
10441051
Type[] actualTypes = ((ParameterizedType)genericType).getActualTypeArguments();
1045-
if (actualTypes.length == 1) {
1046-
Type actualType = actualTypes[0];
1052+
Type actualType = null;
1053+
if (actualTypes.length == 1)
1054+
actualType = actualTypes[0];
1055+
else if (actualTypes.length == 2 && Map.class.isAssignableFrom(field.getType())) //
1056+
actualType = actualTypes[1];
1057+
if (actualType != null) {
10471058
if (actualType instanceof Class)
10481059
elementType = (Class)actualType;
10491060
else if (actualType instanceof ParameterizedType)

0 commit comments

Comments
 (0)