+1 for the JSON-RPC approach. We could also have a flag in the result to make just serialize the "name" of the enum value, instead of the bean.
musachy On Nov 10, 2007 12:51 AM, Ted Husted <[EMAIL PROTECTED]> wrote: > +1 for the JSON-RPC approach. > > > On Nov 9, 2007 9:54 PM, Jeromy Evans <[EMAIL PROTECTED]> wrote: > > 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] > > -- "Hey you! Would you help me to carry the stone?" Pink Floyd --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]