Skip to content

MapCodec to fix mongoDB driver issue `Maps MUST have string keys, found class X instead` base on Yuna Braska's solution (https://stackoverflow.com/a/67849755) with additional handling of codecs that do not encode/decode BsonString by using PropertyEditors

License

Notifications You must be signed in to change notification settings

benjamonnguyen/mongodb-bson-codec

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MapCodec to fix mongoDB driver issue Maps MUST have string keys, found class X instead

Based on Yuna Braska's solution with additional handling of codecs that do not encode/decode BsonString by using PropertyEditors

Usage:

import me.benjinguyen.mongodb.bson.codec.MapCodec;

public class MapCodecProvider implements PropertyCodecProvider {
    @Override
    @SuppressWarnings({"rawtypes", "unchecked"})
    public <T> Codec<T> get(final TypeWithTypeParameters<T> type, final PropertyCodecRegistry registry) {
        if (Map.class.isAssignableFrom(type.getType()) && type.getTypeParameters().size() == 2) {
            return new MapCodec(
                    type.getType(),
                    registry.get(type.getTypeParameters().get(0)),
                    registry.get(type.getTypeParameters().get(1)));
        }
        return null;
    }
}

A 4th constructor argument can be provided to register additional PropertyEditors

import me.benjinguyen.mongodb.bson.codec.MapCodec;

import java.util.Collections;

public class MapCodecProvider implements PropertyCodecProvider {
    @Override
    @SuppressWarnings({"rawtypes", "unchecked"})
    public <T> Codec<T> get(final TypeWithTypeParameters<T> type, final PropertyCodecRegistry registry) {
        if (Map.class.isAssignableFrom(type.getType()) && type.getTypeParameters().size() == 2) {
            return new MapCodec(
                    type.getType(),
                    registry.get(type.getTypeParameters().get(0)),
                    registry.get(type.getTypeParameters().get(1)),
                    Collections.singletonMap(Foo.class, FooEditor.class));
        }
        return null;
    }
}

About

MapCodec to fix mongoDB driver issue `Maps MUST have string keys, found class X instead` base on Yuna Braska's solution (https://stackoverflow.com/a/67849755) with additional handling of codecs that do not encode/decode BsonString by using PropertyEditors

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages