title: Enum Configuration sidebar_position: 6 id: enum_configuration license: | Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
This page explains how Java enum serialization is configured in Apache Fory.
Java enums can be serialized in two modes:
serializeEnumByName(true)Numeric tags are always used in xlang mode. In native Java mode, serializeEnumByName(true) switches enum serialization to names instead of numeric tags.
Use serializeEnumByName(true) when native Java peers should match enum constants by name instead of numeric tag.
Fory fory = Fory.builder() .withLanguage(Language.JAVA) .serializeEnumByName(true) .build();
This mode is useful when declaration order is unstable but enum names remain fixed. It only affects native Java mode. Xlang still uses numeric tags.
Without serializeEnumByName(true), Java enums are serialized by numeric tag. The default tag is the declaration ordinal. When an enum needs stable ids that do not depend on declaration order, annotate exactly one id source with @ForyEnumId, or annotate every enum constant with explicit tag values.
import org.apache.fory.annotation.ForyEnumId; enum Status { Unknown(10), Running(20), Finished(30); private final int id; Status(int id) { this.id = id; } @ForyEnumId public int getId() { return id; } }
Java also supports annotating one enum instance field with @ForyEnumId, or annotating every enum constant directly such as @ForyEnumId(10) Unknown.
@ForyEnumId Styles@ForyEnumId supports exactly three configuration styles:
getId().@ForyEnumId(10) Unknown.value() at its default -1.@ForyEnumId.int.@ForyEnumId, Fory writes the declaration ordinal.@ForyEnumId, Fory writes the configured stable numeric tag instead.@ForyEnumId when declaration order may change but the numeric wire ids must stay stable.serializeEnumByName and other runtime options@ForyField, @Ignore, and integer encoding annotations