Skip to content

NeonEnumNames

Paolo Rossi edited this page Feb 7, 2024 · 1 revision

NeonEnumNames

The NeonEnumNames attribute tells Neon what names to use when serializing/deserializing an enum field (property). Of course you don’t have to specify the attribute because by default Neon uses the string representation for the enum value (by using EnumToString() ) so if you have:

TSpeed = (spHigh, spAverage, spLow);

// defining a field of TSpeed type
Speed: TSpeed;

Serializing a field of type TSpeed will result in:

{
  ...
  "Speed": "spHigh"
}

If you annotate the TSpeed type with NeonEnumNames Neon uses the provided values:

[NeonEnumNames('High Speed,Average Speed,Low Speed')]
TSpeed = (spHigh, spAverage, spLow);

// defining a field of TSpeed type
Speed: TSpeed;

Now if you serialize the field, you will obtain:

{
  ...
  "Speed": "High Speed"
}

There some caveat though:

  1. Because of some limitation in the way Delphi treats attributes, the parameter must be a comma separated string containing the values. The values, for now, are considered strings by Neon
  2. You have to be careful providing the same number of values as the underlying enum type

In the future I plan to expand the management of enum names with custom values separators and supporting types other than strings.

Clone this wiki locally