Skip to content

Commit

Permalink
Referencing Binary Format: Remove references to collections
Browse files Browse the repository at this point in the history
  • Loading branch information
jodastephen committed Dec 30, 2024
2 parents e1e9b9e + f375c0e commit 4d9df1c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/main/java/org/joda/beans/ser/bin/BeanReferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,13 @@ private void findReferencesBean(
return;
}

// has this object been seen before, if so no need to check it again
var result = objects.compute(base, BeanReferences::incrementOrOne);
if (result > 1) {
return;
}
if (base instanceof Bean) {
// has this object been seen before, if so no need to check it again
var result = objects.compute(base, BeanReferences::incrementOrOne);
if (result > 1) {
return;
}

if (base instanceof Bean bean) {
addClassInfo(base, declaredClass);
if (settings.getConverter().isConvertible(bean.getClass())) {
return;
Expand All @@ -165,11 +165,25 @@ private void findReferencesBean(
} else if (parentIterator != null) {
var childIterator = settings.getIteratorFactory().createChild(base, parentIterator);
if (childIterator != null) {
if (childIterator.metaTypeRequired()) {
objects.compute(childIterator.metaTypeName(), BeanReferences::incrementOrOne);
}
// shouldn't try and reuse references to collections
findReferencesIterable(childIterator, objects);
} else {
// has this object been seen before, if so no need to check it again
int result = objects.compute(base, BeanReferences::incrementOrOne);
if (result > 1) {
return;
}
addClassInfo(base, declaredClass);
}
} else {
// has this object been seen before, if so no need to check it again
int result = objects.compute(base, BeanReferences::incrementOrOne);
if (result > 1) {
return;
}
addClassInfo(base, declaredClass);
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/org/joda/beans/ser/SerTestHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
import java.util.Currency;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import java.util.TreeSet;

import org.joda.beans.sample.Address;
import org.joda.beans.sample.Company;
Expand Down Expand Up @@ -256,6 +258,17 @@ public static ImmGenericCollections<JodaConvertInterface> testGenericInterfaces(
.build();
}

public static ImmGenericCollections<Object> testGenericInterfacesCollections() {
return ImmGenericCollections.builder()
.map(ImmutableMap.of(
"First", Arrays.asList("A", "B"),
"First1", ImmutableList.of("A", "B"),
"Third1", new TreeSet<>(ImmutableList.of("A", "B")),
"Third", new HashSet<>(Arrays.asList("A", "B")),
"Second", testCollections()))
.build();
}

public static ImmKeyList testIntermediateInterfaces() {
// second serialized as JodaConvertInterface, non-bean
// third and fourth are serialized as an intermediate Joda-Convert interface INamedKey
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ void test_writeJodaConvertInterface() {
BeanAssert.assertBeanEquals(bean, parsed);
}

@Test
void test_writeJodaConvertInterfaceCollections() {
var bean = SerTestHelper.testGenericInterfacesCollections();

var bytes = JodaBeanSer.COMPACT.binWriterReferencing().write(bean);
// System.out.println(JodaBeanBinReader.visualize(bytes));

var parsed = JodaBeanSer.COMPACT.binReader().read(bytes);
BeanAssert.assertBeanEquals(parsed, bean);
}

@Test
void test_writeIntermediateInterface() {
var bean = SerTestHelper.testIntermediateInterfaces();
Expand Down

0 comments on commit 4d9df1c

Please sign in to comment.