Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop using references to collections #458

Merged
merged 6 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
Potentially incompatible change:
The standard and referencing binary formats now support null as a map key.
</action>
<action dev="jodastephen" type="add" issue="230">
Potentially incompatible change:
The referencing binary format no longer deduplicates collections.
The old format can still be parsed successfully.
</action>
<action dev="jodastephen" type="add" issue="446">
Potentially incompatible change:
The standard binary and simple JSON formats now handle `Iterable` as a collection type.
Expand All @@ -44,15 +49,15 @@
Instead of a Joda-Convert formatted string, the array is output as a list of primitives.
This also applies to multi-dimensional arrays.
This is a much more natural JSON format.
The old format cam still be parsed successfully.
The old format can still be parsed successfully.
</action>
<action dev="jodastephen" type="update">
Incompatible change:
The JSON serialization formats have changed requiring fewer @meta and @type annotations.
For example, where the type of the collection is Object,
previously a String value was explicitly typed as a String, now the type is implicit.
This is a much more natural JSON format.
The old format cam still be parsed successfully.
The old format can still be parsed successfully.
</action>
<action dev="jodastephen" type="update" issue="192">
Incompatible change:
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/org/joda/beans/ser/bin/BeanReferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,16 @@ private void findReferencesBean(
}

// has this object been seen before, if so no need to check it again
var result = objects.compute(base, BeanReferences::incrementOrOne);
if (result > 1) {
if (objects.compute(base, BeanReferences::incrementOrOne) > 1) {
// shouldn't try and reuse references to collections
if (!(base instanceof Bean) && parentIterator != null) {
var childIterator = settings.getIteratorFactory().createChild(base, parentIterator);
if (childIterator != null) {
findReferencesIterable(childIterator, objects);
}
}
return;
}

if (base instanceof Bean bean) {
addClassInfo(base, declaredClass);
if (settings.getConverter().isConvertible(bean.getClass())) {
Expand Down Expand Up @@ -165,6 +170,7 @@ private void findReferencesBean(
} else if (parentIterator != null) {
var childIterator = settings.getIteratorFactory().createChild(base, parentIterator);
if (childIterator != null) {
// shouldn't try and reuse references to collections
findReferencesIterable(childIterator, objects);
} else {
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(true)))
.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
Loading