diff --git a/src/main/java/xyz/srnyx/magicmongo/MagicMongo.java b/src/main/java/xyz/srnyx/magicmongo/MagicMongo.java index 4ff78b7..2a64925 100644 --- a/src/main/java/xyz/srnyx/magicmongo/MagicMongo.java +++ b/src/main/java/xyz/srnyx/magicmongo/MagicMongo.java @@ -13,6 +13,8 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import xyz.srnyx.magicmongo.codecs.UUIDCodec; + import java.util.HashMap; import java.util.Map; @@ -136,13 +138,19 @@ public MagicDatabase getMagicDatabase(@NotNull String name) { } /** - * Gets the default {@link CodecRegistry} with the {@link PojoCodecProvider} enabled + * Gets the default {@link CodecRegistry} + * * * @return the default {@link CodecRegistry} */ @NotNull public static CodecRegistry getDefaultCodecRegistry() { return CodecRegistries.fromRegistries( + CodecRegistries.fromCodecs(new UUIDCodec()), MongoClientSettings.getDefaultCodecRegistry(), CodecRegistries.fromProviders(PojoCodecProvider.builder().automatic(true).build())); } diff --git a/src/main/java/xyz/srnyx/magicmongo/codecs/UUIDCodec.java b/src/main/java/xyz/srnyx/magicmongo/codecs/UUIDCodec.java new file mode 100644 index 0000000..305fd03 --- /dev/null +++ b/src/main/java/xyz/srnyx/magicmongo/codecs/UUIDCodec.java @@ -0,0 +1,32 @@ +package xyz.srnyx.magicmongo.codecs; + +import org.bson.BsonReader; +import org.bson.BsonWriter; +import org.bson.codecs.Codec; +import org.bson.codecs.DecoderContext; +import org.bson.codecs.EncoderContext; + +import org.jetbrains.annotations.NotNull; + +import java.util.UUID; + + +/** + * A {@link Codec} for encoding and decoding {@link UUID UUIDs} to and from {@link String Strings} + */ +public class UUIDCodec implements Codec { + @Override + public void encode(@NotNull BsonWriter writer, @NotNull UUID value, EncoderContext encoderContext) { + writer.writeString(value.toString()); + } + + @Override @NotNull + public UUID decode(@NotNull BsonReader reader, DecoderContext decoderContext) { + return UUID.fromString(reader.readString()); + } + + @Override + public Class getEncoderClass() { + return UUID.class; + } +} \ No newline at end of file