I have this piece of code:
Codec<Bean> CODEC = BeanType.REGISTRY.getCodec().dispatch(Bean::type, BeanType::codec);
Where BeanType is defined as
record BeanType<T extends Bean>(Text name, Text desc, MapCodec<T> codec) {
static final public Registry<BeanType<?>> REGISTRY = FabricRegistryBuilder
.<BeanType<?>>createSimple(RegistryKey.ofRegistry(Identifier.of("beanmod", "beans")))
.buildAndRegister();
}
As I understand it, and please correct me if I'm mistaken, upon serialization a Codec<Bean> will call Bean::type to get an associated entry from the REGISTRY using the BeanType<?> and call its codec to get the actual codec of the Bean, and upon deserialization will use stored Identifier to get the BeanType<?> and call its codec and use it to deserialize further.
Now the questions.
- How does
BeanType.REGISTRY.getCodec()work by itself? What does it serialize a givenBeanType<?>to? Straight up to itsIdentifier? - I have a need to store both a list of
BeanTypeIdentifiers contained in theBeanType.REGISTRYas well as an "full" form of anyBeanas associated by theBeanType<?>. How would I achieve that? I envision the resulting json to look like:
{
"beans": [
"beanmod:bean1",
"beanmod:bean2"
],
"theOneStoredBean": {
"type": "beanmod:bean1",
"bean1field": "field1value"
}
}
- How do I easily test codecs both ways?
Thank you very much for the answers in advance!