Skip to content

Commit

Permalink
Refactor simple JSON serialization
Browse files Browse the repository at this point in the history
* Use a ClassValue map to speed up serialization
* Remove "clever but not faster" int handling in `JsonOutput`
  • Loading branch information
jodastephen committed Nov 13, 2024
1 parent aa33abf commit 83f8e58
Show file tree
Hide file tree
Showing 9 changed files with 566 additions and 277 deletions.
5 changes: 5 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
It is expected that most uses of Joda-Beans already have the equals, hashCode and toString methods
before the autogenerated block and therefore will be unaffected.
</action>
<action dev="jodastephen" type="update">
Incompatible change:
The simple JSON serialization format has changed slightly.
Two-dimensional arrays are now written as would be expected - a list of list of doubles.
</action>
</release>
<release version="2.11.1" date="2024-08-01" description="v2.11.1">
<action dev="jodastephen" type="add">
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/joda/beans/ResolvedType.java
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,19 @@ public ResolvedType getArgument(int index) {
return arguments.get(index);
}

/**
* Gets the matching type argument or the default value of {@code Object}
*
* @param index the index of the generic parameter
* @return the type, defaulted to Object
*/
public ResolvedType getArgumentOrDefault(int index) {
if (index < 0 || index >= arguments.size()) {
return OBJECT;
}
return arguments.get(index);
}

private IllegalArgumentException invalidTypeArgumentIndex(int index) {
return new IllegalArgumentException("Unexpected generic type access for " + this + ", index " + index + " is invalid");
}
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/org/joda/beans/ser/SerIteratorFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,15 @@ public SerIterator create(Object value, MetaProperty<?> prop, Class<?> beanClass
* @return the iterator, null if not a collection-like type
*/
public SerIterator create(Object value, MetaProperty<?> prop, Class<?> beanClass) {
Class<?> declaredType = prop.propertyType();
var declaredType = prop.propertyType();
if (value instanceof Collection<?> collection) {
Class<?> valueType = defaultToObjectClass(JodaBeanUtils.collectionType(prop, beanClass));
var valueType = defaultToObjectClass(JodaBeanUtils.collectionType(prop, beanClass));
var valueTypeTypes = JodaBeanUtils.collectionTypeTypes(prop, beanClass);
return collection(collection, declaredType, valueType, valueTypeTypes);
}
if (value instanceof Map<?, ?> map) {
Class<?> keyType = defaultToObjectClass(JodaBeanUtils.mapKeyType(prop, beanClass));
Class<?> valueType = defaultToObjectClass(JodaBeanUtils.mapValueType(prop, beanClass));
var keyType = defaultToObjectClass(JodaBeanUtils.mapKeyType(prop, beanClass));
var valueType = defaultToObjectClass(JodaBeanUtils.mapValueType(prop, beanClass));
var valueTypeTypes = JodaBeanUtils.mapValueTypeTypes(prop, beanClass);
return map(map, declaredType, keyType, valueType, valueTypeTypes);
}
Expand All @@ -150,7 +150,7 @@ public SerIterator create(Object value, MetaProperty<?> prop, Class<?> beanClass
* @return the iterator, null if not a collection-like type
*/
public SerIterator createChild(Object value, SerIterator parent) {
Class<?> declaredType = parent.valueType();
var declaredType = parent.valueType();
var childGenericTypes = parent.valueTypeTypes();
if (value instanceof Collection<?> collection) {
if (childGenericTypes.size() == 1) {
Expand Down Expand Up @@ -204,21 +204,21 @@ public SerIterable createIterable(String metaTypeDescription, JodaBeanSer settin
throw new IllegalArgumentException("Three-dimensional arrays cannot be parsed");
}
if (metaTypeDescription.endsWith("[][]")) {
Class<?> type = META_TYPE_MAP.get(metaTypeDescription);
var type = META_TYPE_MAP.get(metaTypeDescription);
if (type != null) {
return array(type);
}
var clsStr = metaTypeDescription.substring(0, metaTypeDescription.length() - 4);
try {
Class<?> cls = SerTypeMapper.decodeType(clsStr, settings, null, knownTypes);
var cls = SerTypeMapper.decodeType(clsStr, settings, null, knownTypes);
var compound = "[L" + cls.getName() + ";";
return array(Class.forName(compound)); // needs to be Class.forName
} catch (ClassNotFoundException ex) {
throw new RuntimeException(ex);
}
}
if (metaTypeDescription.endsWith("[]")) {
Class<?> type = META_TYPE_MAP.get(metaTypeDescription);
var type = META_TYPE_MAP.get(metaTypeDescription);
if (type == null) {
var clsStr = metaTypeDescription.substring(0, metaTypeDescription.length() - 2);
try {
Expand All @@ -241,7 +241,7 @@ public SerIterable createIterable(String metaTypeDescription, JodaBeanSer settin
public SerIterable createIterable(SerIterable iterable) {
var valueTypeTypes = iterable.valueTypeTypes();
if (!valueTypeTypes.isEmpty()) {
Class<?> valueType = iterable.valueType();
var valueType = iterable.valueType();
if (NavigableSet.class.isAssignableFrom(valueType)) {
return navigableSet(valueTypeTypes.get(0), EMPTY_VALUE_TYPES);
}
Expand Down
Loading

0 comments on commit 83f8e58

Please sign in to comment.