FroMage / jax-doclets

Set of JavaDoc doclets for modern Java annotations APIs
http://www.lunatech-labs.com/open-source/jax-doclets
GNU Lesser General Public License v3.0
26 stars 23 forks source link

Provide switch to omit @ in JSON mapped XML attributes #23

Closed stanio closed 12 years ago

stanio commented 12 years ago

Jackson supports mapping Java Beans to/from JSON using JAXB annotations:

http://wiki.fasterxml.com/JacksonJAXBAnnotations

Its mapping is really simple and doesn't try to distinguish XML features like attributes (vs. elements) and namespaces, so both of the following two versions:

public class Foo {

    @XmlElement 
    public String bar;

    @XmlElement 
    public String baz;

}

and:

public class Foo {

    @XmlAttribute
    public String bar;

    @XmlElement 
    public String baz;

}

get serialized as:

{
    "bar" : "...",
    "baz" : "..."
}

and not as:

{
    "@bar" : "...",
    "baz" : "..."
}

It would be nice if there's an option to omit the code>@</code in attribute names in the generated JSON examples, e.g. -useplainjsonattributenames, though this is a bit lengthy one.

FroMage commented 12 years ago

Good idea.

anikitin commented 12 years ago

Need this change as well.

ilyavolk commented 12 years ago

but, is there any case, in which pre>@</pre is needed in attribute name in JSON example ? probably, it would be enough to remove pre>@</pre printing in JSON example. patch JAXBClassWriter.printJSONExample was:

      print("  \"@");

become:

      print("  \"");
FroMage commented 12 years ago

It depends on your JSON marshaller. In RESTEasy the default is to get @.

ilyavolk commented 12 years ago

ok. I'm not sure if the fix is enough.

  1. JAXBDoclet.optionLength was:
    if ("-disablejaxbmethodoutput".equals(option)
       || "-disablejsontypename".equals(option)
       || "-disablexmlexample".equals(option)
       || "-disablejsonexample".equals(option)
     return 1;
    }
    

become

    if ("-disablejaxbmethodoutput".equals(option)
        || "-disablejsontypename".equals(option)
        || "-disablexmlexample".equals(option)
        || "-disablejsonexample".equals(option)
        || "-useplainjsonattributenames".equals(option)) {
      return 1;
    }

2.JAXBConfiguration 2.1. Add variable

public boolean useJSONPlainAttribute = false;

2.2. At the end of the function setOptions add code

useJSONPlainAttribute = Utils.hasOption(options, "-useplainjsonattributenames");

3.JAXBClassWriter.printJSONExample in the loop by attributes was:

print("  \"@");

become:

      print("   \"");
      if(!((JAXBConfiguration) configuration).useJSONPlainAttribute)
          print("@");
stanio commented 12 years ago

Just for information, the requested behavior matches what's called Natural JSON notation:

http://jersey.java.net/nonav/documentation/latest/json.html#d4e1058

There appear also Mapped, Jettison-mapped and Badgerfish notations:

http://jersey.java.net/nonav/documentation/latest/json.html#d4e949

These could be used like -jsonnotaton natural in future to switch between whole different behavior, if implemented.

FroMage commented 12 years ago

Done with -jsonconvention mapped in latest git.