Skip to content

RecordCodecs do not encode fields in the correct order in circumstances #101

Description

@ChrysanthCow

Unsure if this'll get noticed, but I might as well try.

When working with order specific values, any RecordCodecBuilders with 5 fields or above will flip the field locations in half when encoding.
The top half of the fields will appear at the bottom half of the structure. If the field count is an odd number, the value after the whole value will also be moved to the bottom.

While I don't think this is that important for any current ops that Mojang utilizes, I have ran into issues within custom implementation where order is important due to this issue.

In Practice

Say I have this RecordCodec.

RecordCodecBuilder.mapCodec(inst -> inst.group(
	Codec.INT.fieldOf("silly"),
		.forGetter(CommonValues::silly),
	ENCHANTMENT_OPINION_CODEC.fieldOf("enchantment_opinion")
		.forGetter(CommonValues::enchantmentOpinion),
	Codec.INT.fieldOf("red_number")
		.forGetter(CommonValues::redNumber),
	Codec.FLOAT.fieldOf("green_number")
		.forGetter(CommonValues::greenNumber),
	TextColor.CODEC.fieldOf("color")
		.forGetter(CommonValues::color),
).apply(inst, CommonValues::new))
What I expect.
{
	"silly": 1,
	"enchantment_opinion": {
		"enchantment": "minecraft:frost_walker",
		"opinion": "like"
	},
	"red_number": 4,
	"green_number": 6.66
	"color": "#FFFFFF"
}
What actually encoded.
{
	"green_number": 6.66
	"color": "#FFFFFF",
	"silly": 1,
	"enchantment_opinion": {
		"enchantment": "minecraft:frost_walker",
		"opinion": "like"
	},
	"red_number": 4
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions