Skip to content

Commit

Permalink
Minor fixes to serialization
Browse files Browse the repository at this point in the history
* Use array component type when serializing array items
* Rename variables and methods
* Use Iterable directly in simple JSON, as size is not needed
  • Loading branch information
jodastephen committed Dec 24, 2024
1 parent a9a22d6 commit 0859b19
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -536,11 +536,13 @@ private static void writePrimitiveArray(
writeArrayTypeDescription(writer, arrayType);
}
// write content
var handler = JodaBeanPackedBinWriter.LOOKUP.get(arrayType.getComponentType());
var arrayComponentType = arrayType.getComponentType();
var componentType = ResolvedType.of(arrayComponentType);
var handler = JodaBeanPackedBinWriter.LOOKUP.get(arrayComponentType);
var arrayLength = Array.getLength(array);
writer.output.writeArrayHeader(arrayLength);
for (int i = 0; i < arrayLength; i++) {
handler.handle(writer, declaredType, propertyName, Array.get(array, i));
handler.handle(writer, componentType, propertyName, Array.get(array, i));
}
}

Expand Down
50 changes: 25 additions & 25 deletions src/main/java/org/joda/beans/ser/bin/JodaBeanStandardBinWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ void write(Bean bean, boolean includeRootType) throws IOException {
output.writeArrayHeader(2);
output.writeInt(1); // version 1
// root always outputs the bean, not Joda-Convert form
writeBean(rootType, "", bean, true);
writeBean(rootType, "", bean, includeRootType);
}

//-----------------------------------------------------------------------
Expand All @@ -148,7 +148,7 @@ private void writeBeanMaybeSimple(ResolvedType declaredType, String propertyName
}

// writes a bean, with meta type information if necessary
private void writeBean(ResolvedType declaredType, String propertyName, Bean bean, boolean isRoot) throws IOException {
private void writeBean(ResolvedType declaredType, String propertyName, Bean bean, boolean isRootAndInclRootType) throws IOException {
// have to determine the number of properties being output before starting to write
var count = bean.metaBean().metaPropertyCount();
var propHandlers = new PropertyHandler[count];
Expand All @@ -169,12 +169,12 @@ private void writeBean(ResolvedType declaredType, String propertyName, Bean bean
}
// write out the header, potentially including the type
if (bean.getClass() != declaredType.getRawType()) {
var type = SerTypeMapper.encodeType(bean.getClass(), settings, basePackage, knownTypes);
if (basePackage == null && isRoot) {
var typeStr = SerTypeMapper.encodeType(bean.getClass(), settings, basePackage, knownTypes);
if (isRootAndInclRootType) {
basePackage = bean.getClass().getPackage().getName() + ".";
}
output.writeMapHeader(size + 1);
output.writeExtensionString(MsgPack.JODA_TYPE_BEAN, type);
output.writeExtensionString(MsgPack.JODA_TYPE_BEAN, typeStr);
output.writeNil();
} else {
output.writeMapHeader(size);
Expand Down Expand Up @@ -258,7 +258,7 @@ private IllegalArgumentException invalidConversionMsg(String propertyName, Objec
}

// writes the meta type header
private static final void writeMetaPropertyReference(JodaBeanStandardBinWriter writer, String metaTypeName) throws IOException {
private static final void writeMetaType(JodaBeanStandardBinWriter writer, String metaTypeName) throws IOException {
writer.output.writeMapHeader(1);
writer.output.writeExtensionString(MsgPack.JODA_TYPE_META, metaTypeName);
}
Expand Down Expand Up @@ -373,9 +373,9 @@ private static void writeArray(
// write actual type
var valueType = array.getClass().getComponentType();
if (valueType == Object.class && !Object[].class.isAssignableFrom(declaredType.getRawType())) {
writeMetaPropertyReference(writer, metaTypeArrayName(valueType));
writeMetaType(writer, metaTypeArrayName(valueType));
} else if (valueType == String.class && !String[].class.isAssignableFrom(declaredType.getRawType())) {
writeMetaPropertyReference(writer, metaTypeArrayName(valueType));
writeMetaType(writer, metaTypeArrayName(valueType));
}
// write content
var componentType = toWeakenedType(declaredType.toComponentType());
Expand All @@ -395,15 +395,15 @@ private static void writePrimitiveArray(
// write actual type
var valueType = array.getClass().getComponentType();
if (!declaredType.isArray()) {
writeMetaPropertyReference(writer, metaTypeArrayName(valueType));
writeMetaType(writer, metaTypeArrayName(valueType));
}
// write content
var componentType = toWeakenedType(declaredType.toComponentType());
var componentType = declaredType.toComponentType();
var handler = JodaBeanStandardBinWriter.LOOKUP.get(componentType.getRawType());
var arrayLength = Array.getLength(array);
writer.output.writeArrayHeader(arrayLength);
for (int i = 0; i < arrayLength; i++) {
handler.handle(writer, declaredType, propertyName, Array.get(array, i));
handler.handle(writer, componentType, propertyName, Array.get(array, i));
}
}

Expand Down Expand Up @@ -444,11 +444,11 @@ private static void writeCollection(

// write actual type
if (coll instanceof Set && !Set.class.isAssignableFrom(declaredType.getRawType())) {
writeMetaPropertyReference(writer, "Set");
writeMetaType(writer, "Set");
} else if (coll instanceof List && !List.class.isAssignableFrom(declaredType.getRawType())) {
writeMetaPropertyReference(writer, "List");
writeMetaType(writer, "List");
} else if (!Collection.class.isAssignableFrom(declaredType.getRawType())) {
writeMetaPropertyReference(writer, "Collection");
writeMetaType(writer, "Collection");
}
// write content
var itemType = toWeakenedType(declaredType.getArgumentOrDefault(0));
Expand All @@ -467,7 +467,7 @@ private static void writeMap(

// write actual type
if (!Map.class.isAssignableFrom(declaredType.getRawType())) {
writeMetaPropertyReference(writer, "Map");
writeMetaType(writer, "Map");
}
// write content
writeMapEntries(writer, declaredType, propertyName, map.entrySet());
Expand Down Expand Up @@ -522,11 +522,11 @@ private static void writeMultimap(

// write actual type
if (mmap instanceof SetMultimap && !SetMultimap.class.isAssignableFrom(declaredType.getRawType())) {
writeMetaPropertyReference(writer, "SetMultimap");
writeMetaType(writer, "SetMultimap");
} else if (mmap instanceof ListMultimap && !ListMultimap.class.isAssignableFrom(declaredType.getRawType())) {
writeMetaPropertyReference(writer, "ListMultimap");
writeMetaType(writer, "ListMultimap");
} else if (!Multimap.class.isAssignableFrom(declaredType.getRawType())) {
writeMetaPropertyReference(writer, "Multimap");
writeMetaType(writer, "Multimap");
}
// write content, using a map with repeated keys
writeMapEntries(writer, declaredType, propertyName, mmap.entries());
Expand All @@ -541,7 +541,7 @@ private static void writeMultiset(

// write actual type
if (!Multiset.class.isAssignableFrom(declaredType.getRawType())) {
writeMetaPropertyReference(writer, "Multiset");
writeMetaType(writer, "Multiset");
}
// write content, using a map of value to count
var valueType = toWeakenedType(declaredType.getArgumentOrDefault(0));
Expand All @@ -562,7 +562,7 @@ private static void writeTable(

// write actual type
if (!Table.class.isAssignableFrom(declaredType.getRawType())) {
writeMetaPropertyReference(writer, "Table");
writeMetaType(writer, "Table");
}
// write content, using an array of cells
var rowType = toWeakenedType(declaredType.getArgumentOrDefault(0));
Expand All @@ -589,9 +589,9 @@ private static void writeBiMap(
// hack around Guava annoyance by assuming that size 0 and 1 ImmutableBiMap
// was actually meant to be an ImmutableMap
if ((declaredType.getRawType() != Map.class && declaredType.getRawType() != ImmutableMap.class) || biMap.size() >= 2) {
writeMetaPropertyReference(writer, "BiMap");
writeMetaType(writer, "BiMap");
} else if (!Map.class.isAssignableFrom(declaredType.getRawType())) {
writeMetaPropertyReference(writer, "Map");
writeMetaType(writer, "Map");
}
}
// write content
Expand Down Expand Up @@ -619,7 +619,7 @@ private static void writeGrid(

// write actual type
if (!Grid.class.isAssignableFrom(declaredType.getRawType())) {
writeMetaPropertyReference(writer, "Grid");
writeMetaType(writer, "Grid");
}
// write content using sparse or dense approach
var valueType = toWeakenedType(declaredType.getArgumentOrDefault(0));
Expand Down Expand Up @@ -665,7 +665,7 @@ public void handle(
Optional<?> opt) throws IOException {

if (!Optional.class.isAssignableFrom(declaredType.getRawType())) {
writeMetaPropertyReference(writer, "Optional");
writeMetaType(writer, "Optional");
}
var valueType = declaredType.getArgumentOrDefault(0).toRawType();
writer.writeObject(valueType, "", opt.orElse(null));
Expand Down Expand Up @@ -702,7 +702,7 @@ public void handle(
com.google.common.base.Optional<?> opt) throws IOException {

if (!com.google.common.base.Optional.class.isAssignableFrom(declaredType.getRawType())) {
writeMetaPropertyReference(writer, "GuavaOptional");
writeMetaType(writer, "GuavaOptional");
}
// write content
var valueType = declaredType.getArgumentOrDefault(0).toRawType();
Expand Down
Loading

0 comments on commit 0859b19

Please sign in to comment.