What is "the correct" way to serialize an enum to JSON?

The struts2plugin can't handle enums quite right (Issue 9: http://code.google.com/p/jsonplugin/issues/detail?id=9) and while trying to finalize a patch I haven't found a consistent opinion.

There's two schools of thought:
- an enum should be serialized as a value. eg. { "prop":"name()" } or { "prop":ordinal() } - an enum should be serialized an an object. eg. { "prop": { "name":"name()" } } The advantage of the latter case is that additional properties of the enumeration can be included.

My quick survey of implementations are:
xstream with JsonHierarchicalStream and default converter : { "prop": {"name"}} xstream with jettisonMapper driver and default converter : { "prop":"name" }
json-rpc-java:  { "prop":{ "_name":"name", properties... }}
json.net:  {"prop":ordinal}

I think json-rpc-java is strictly correct by serializing the enum as bean with a special _name property, however I think the Jettison driver by value approach is closer to what a javascript developer would naturally expect. The JsonHierarchicalStream driver is unnatural because it supports deserialization. Json.net doesn't have to contend with additional properties in enums and the ordinal value is explicit.

Has anyone seen a convention used a lot that's worth borrowing? Or is this worthy of a yet another option for the result?

regards,
Jeromy Eans



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to