As I will probably need this one day: [WayBack] How does one convert a Delphi enum to a JSON value, with different specific values? – CHUA Chee Wee – Google+:
eg,
TEnum1 = (test1, test2, test3)
TSomeClass.FEnum := test1;
When converted to JSON, I’d like to see maybe
{"Enum": "Value1"}
instead oftest1
and
test2
to"Godzilla"
,test3
to"Tiburon"
The solution is in his repository: github/chuacw/EnumJson:
- solution: EnumJSON.Interceptors.pas
- example: EnumDemo.pas / CustomizedEnumJSON.dpr
After initially suggesting to look into [Archive.is] Serializing User Objects – RAD Studio, he based his solution on a set of clever tricks circumventing Delphi compiler limitations and bugs:
- derive a generic
TEnumInterceptor<T>
from [Archive.is]Data.DBXJSONReflect.TJSONInterceptor
and put the conversion logic in there - attach attributes for each enumeration value to descendants of
TEnumInterceptor<T>
because of a compiler limitation and bug - bind those descendants to the properties in the class to be marshaled using the [Archive.is]
Data.DBXJSONReflect.JSONReflectAttribute
Later he extended the solution to include sets in additions to enums: [Archive.is] Persisting enumeration and sets to JSON – Chee Wee’s blog: IT solutions for Singapore and companies worldwide (via [Archive.is] Made my JSON interceptor demo public. Now you can save your enum and sets to JSON, with customized output to boot! – CHUA Chee Wee – Google+)
Clever!
Hopefully this got Lars Fosdal some ideas to solve [WayBack] JSON in Berlin – Can I persuade TJSon to treat “params” as it was a string and not an object for the TJsonRPC class? – Lars Fosdal – Google+
Enums with Delphi
Enums with Delphi and their mapping is a repeating topic, see for instance [WayBack] What’s the easiest (ie., least coding) way to map an enum to a const string and vice versa? (Can attributes be used on enum values yet?) eg., type TMyColor… – David Schwartz – Google+
It shows how much the Delphi language is in need of language enhancements as right now there are way too many open source libraries struggling with the same issues each working around them or providing solutions in slightly different way.
Three things immediately come to mind:
- the NameOf that – analogous to TypeOf – gets the name of an identifier as a string
- expose all RTTI for all enum types (especially the ones with non-contiguous values or starting at another value than ordinal zero)
- allow for attributes on enum values
Some examples of libraries providing enumeration support:
- Spring4D in Source/Base/Spring.SystemUtils.pas has a
TEnum
class with static methods - David Heffernan has a
TEnumeration
class with static methods - Agustin Ortu has an
Enum.Wrapper
unit with various records mimicing .NET behaviour based on David’s work - Attribute mapping in [WayBack] I want to annotate some of my enumerated types with human facing names… – David Heffernan – Google+
- Some commercial 3rd party libraries also offer extra enumeration support.
Some more thoughts from a different perspective: [WayBack] What’s New in Edge Rails: Active Record enums
–jeroen