serialize enum fields with gson

By default, Gson just serialize the ‘name’ of the Enum which might not be enough since we might need also want to carry all the fields during the serialization. To achieve this we need to has our own gson adaptor and make use of reflection. usage: Enum class: Test Code: Result:

enum vs public static final

Technically one could indeed view enums as a class with a bunch of typed constants, and this is in fact how enum constants are implemented internally. Using an enum however gives you useful methods (Enum javadoc) that you would otherwise have to implement yourself, such as Enum.valueOf. There is also .values() to iterate over the…

hibernate enum mapping example

User Domain: Hibernate Mapping (HBM): By default Hibernate persists the enumeration value as integer in the database when type param is not specified, In order to store the enum display name, we use a param┬átype, which should be pointed to value in java.sql.Types. Since we want to store the enum display name in database we…