Skip to content

Support decoding to Collection and Map subclasses #1019

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

Merged
merged 7 commits into from
Oct 27, 2022
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
6 changes: 4 additions & 2 deletions bson/src/main/org/bson/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@

import org.bson.codecs.BsonValueCodecProvider;
import org.bson.codecs.Codec;
import org.bson.codecs.CollectionCodecProvider;
import org.bson.codecs.Decoder;
import org.bson.codecs.DecoderContext;
import org.bson.codecs.DocumentCodec;
import org.bson.codecs.DocumentCodecProvider;
import org.bson.codecs.Encoder;
import org.bson.codecs.EncoderContext;
import org.bson.codecs.IterableCodecProvider;
import org.bson.codecs.MapCodecProvider;
import org.bson.codecs.ValueCodecProvider;
import org.bson.codecs.configuration.CodecRegistry;
Expand Down Expand Up @@ -60,8 +60,10 @@
* @since 3.0.0
*/
public class Document implements Map<String, Object>, Serializable, Bson {
@SuppressWarnings("deprecation")
private static final Codec<Document> DEFAULT_CODEC =
withUuidRepresentation(fromProviders(asList(new ValueCodecProvider(), new IterableCodecProvider(),
withUuidRepresentation(fromProviders(asList(new ValueCodecProvider(),
new CollectionCodecProvider(), new org.bson.codecs.IterableCodecProvider(),
new BsonValueCodecProvider(), new DocumentCodecProvider(), new MapCodecProvider())), UuidRepresentation.STANDARD)
.get(Document.class);

Expand Down
121 changes: 121 additions & 0 deletions bson/src/main/org/bson/codecs/AbstractCollectionCodec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.bson.codecs;

import org.bson.BsonReader;
import org.bson.BsonType;
import org.bson.BsonWriter;
import org.bson.codecs.configuration.CodecConfigurationException;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.AbstractCollection;
import java.util.AbstractList;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.NavigableSet;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.function.Supplier;

import static java.lang.String.format;
import static org.bson.assertions.Assertions.notNull;

abstract class AbstractCollectionCodec<T, C extends Collection<T>> implements Codec<C> {

private final Class<C> clazz;
private final Supplier<C> supplier;

@SuppressWarnings({"unchecked", "UnnecessaryLocalVariable", "rawtypes"})
AbstractCollectionCodec(final Class<C> clazz) {
this.clazz = notNull("clazz", clazz);
Class rawClass = clazz;
if (rawClass == Collection.class || rawClass == List.class || rawClass == AbstractCollection.class || rawClass == AbstractList.class
|| rawClass == ArrayList.class) {
supplier = () -> (C) new ArrayList<T>();
} else if (rawClass == Set.class || rawClass == AbstractSet.class || rawClass == HashSet.class) {
supplier = () -> (C) new HashSet<T>();
} else if (rawClass == NavigableSet.class || rawClass == SortedSet.class || rawClass == TreeSet.class) {
//noinspection SortedCollectionWithNonComparableKeys
supplier = () -> (C) new TreeSet<T>();
} else {
Constructor<? extends Collection<?>> constructor;
Supplier<C> supplier;
try {
constructor = clazz.getDeclaredConstructor();
supplier = () -> {
try {
return (C) constructor.newInstance();
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new CodecConfigurationException(format("Can not invoke no-args constructor for Collection class %s", clazz),
e);
}
};
} catch (NoSuchMethodException e) {
supplier = () -> {
throw new CodecConfigurationException(format("No no-args constructor for Collection class %s", clazz), e);
};
}
this.supplier = supplier;
}
}

abstract T readValue(BsonReader reader, DecoderContext decoderContext);

abstract void writeValue(BsonWriter writer, T cur, EncoderContext encoderContext);

@Override
public C decode(final BsonReader reader, final DecoderContext decoderContext) {
reader.readStartArray();

C collection = supplier.get();
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
if (reader.getCurrentBsonType() == BsonType.NULL) {
reader.readNull();
collection.add(null);
} else {
collection.add(readValue(reader, decoderContext));
}
}

reader.readEndArray();

return collection;
}

@Override
public void encode(final BsonWriter writer, final C value, final EncoderContext encoderContext) {
writer.writeStartArray();
for (final T cur : value) {
if (cur == null) {
writer.writeNull();
} else {
writeValue(writer, cur, encoderContext);
}
}
writer.writeEndArray();
}

@Override
public Class<C> getEncoderClass() {
return clazz;
}
}
69 changes: 0 additions & 69 deletions bson/src/main/org/bson/codecs/AbstractIterableCodec.java

This file was deleted.

56 changes: 49 additions & 7 deletions bson/src/main/org/bson/codecs/AbstractMapCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,61 @@
import org.bson.BsonReader;
import org.bson.BsonType;
import org.bson.BsonWriter;
import org.bson.codecs.configuration.CodecConfigurationException;

import javax.annotation.Nullable;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.AbstractMap;
import java.util.HashMap;
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;
import java.util.function.Supplier;

abstract class AbstractMapCodec<T> implements Codec<Map<String, T>> {
import static java.lang.String.format;
import static org.bson.assertions.Assertions.notNull;

abstract class AbstractMapCodec<T, M extends Map<String, T>> implements Codec<M> {

private final Supplier<M> supplier;
private final Class<M> clazz;

@SuppressWarnings({"unchecked", "UnnecessaryLocalVariable", "rawtypes"})
AbstractMapCodec(@Nullable final Class<M> clazz) {
this.clazz = notNull("clazz", clazz);
Class rawClass = clazz;
if (rawClass == Map.class || rawClass == AbstractMap.class || rawClass == HashMap.class) {
supplier = () -> (M) new HashMap<String, T>();
} else if (rawClass == NavigableMap.class || rawClass == TreeMap.class) {
supplier = () -> (M) new TreeMap<String, T>();
} else {
Constructor<? extends Map<?, ?>> constructor;
Supplier<M> supplier;
try {
constructor = clazz.getDeclaredConstructor();
supplier = () -> {
try {
return (M) constructor.newInstance();
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new CodecConfigurationException("Can not invoke no-args constructor for Map class %s", e);
}
};
} catch (NoSuchMethodException e) {
supplier = () -> {
throw new CodecConfigurationException(format("Map class %s has no public no-args constructor", clazz), e);
};
}
this.supplier = supplier;
}
}

abstract T readValue(BsonReader reader, DecoderContext decoderContext);

abstract void writeValue(BsonWriter writer, T value, EncoderContext encoderContext);

@Override
public void encode(final BsonWriter writer, final Map<String, T> map, final EncoderContext encoderContext) {
public void encode(final BsonWriter writer, final M map, final EncoderContext encoderContext) {
writer.writeStartDocument();
for (final Map.Entry<String, T> entry : map.entrySet()) {
writer.writeName(entry.getKey());
Expand All @@ -46,8 +89,8 @@ public void encode(final BsonWriter writer, final Map<String, T> map, final Enco


@Override
public Map<String, T> decode(final BsonReader reader, final DecoderContext decoderContext) {
Map<String, T> map = new HashMap<>();
public M decode(final BsonReader reader, final DecoderContext decoderContext) {
M map = supplier.get();

reader.readStartDocument();
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
Expand All @@ -64,9 +107,8 @@ public Map<String, T> decode(final BsonReader reader, final DecoderContext decod
return map;
}

@SuppressWarnings({"unchecked", "rawtypes"})
@Override
public Class<Map<String, T>> getEncoderClass() {
return (Class<Map<String, T>>) ((Class) Map.class);
public Class<M> getEncoderClass() {
return clazz;
}
}
Loading