Created
December 22, 2020 20:55
-
-
Save Mr00Anderson/41a0c802661f5a00a4a21d475983baed to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package app.virtualhex.gdx.engine.data; | |
import com.badlogic.gdx.utils.Array; | |
import org.bson.BsonReader; | |
import org.bson.BsonType; | |
import org.bson.BsonValue; | |
import org.bson.BsonWriter; | |
import org.bson.codecs.BsonValueCodecProvider; | |
import org.bson.codecs.Codec; | |
import org.bson.codecs.DecoderContext; | |
import org.bson.codecs.EncoderContext; | |
import org.bson.codecs.configuration.CodecProvider; | |
import org.bson.codecs.configuration.CodecRegistry; | |
import static org.bson.assertions.Assertions.notNull; | |
class GdxArrayCodec implements Codec<Array<?>> { | |
private final CodecRegistry codecRegistry; | |
public GdxArrayCodec(CodecRegistry codecRegistry) { | |
this.codecRegistry = notNull("codecRegistry", codecRegistry); | |
} | |
@Override | |
public Array<BsonValue> decode(BsonReader reader, DecoderContext decoderContext) { | |
reader.readStartArray(); | |
Array<BsonValue> array = new Array<>(); | |
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) { | |
array.add(readValue(reader, decoderContext)); | |
} | |
reader.readEndArray(); | |
return array; | |
} | |
@Override | |
public void encode(BsonWriter writer, Array<?> array, EncoderContext encoderContext) { | |
writer.writeStartArray(); | |
for (Object value: array) { | |
Codec codec = codecRegistry.get(value.getClass()); | |
encoderContext.encodeWithChildContext(codec, writer, value); | |
} | |
writer.writeEndArray(); | |
} | |
@Override | |
public Class<Array<?>> getEncoderClass() { | |
return (Class<Array<?>>)(Class<?>)Array.class; | |
} | |
protected BsonValue readValue(final BsonReader reader, final DecoderContext decoderContext) { | |
return codecRegistry.get( | |
BsonValueCodecProvider.getClassForBsonType(reader.getCurrentBsonType()) | |
).decode(reader, decoderContext); | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package app.virtualhex.gdx.engine.data; | |
import com.badlogic.gdx.utils.Array; | |
import org.bson.codecs.Codec; | |
import org.bson.codecs.configuration.CodecProvider; | |
import org.bson.codecs.configuration.CodecRegistry; | |
public class GdxArrayCodecProvider implements CodecProvider { | |
@Override | |
public <T> Codec<T> get(final Class<T> clazz, final CodecRegistry registry) { | |
if (clazz == Array.class) { | |
return (Codec<T>) new GdxArrayCodec(registry); | |
} | |
// CodecProvider returns null if it's not a provider for the requresed Class | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment