2

Please help me how to get JSON output as below:

{
   "_costMethod": "Average",
   "fundingDate": 2008-10-02,
   "fundingAmount": 2510959.95
}

Instead of:

{
   "@type": "sma",
   "costMethod": "Average",
   "fundingDate": "2008-10-02",
   "fundingAmount": "2510959.95"
}
bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • Possible duplicate: http://stackoverflow.com/questions/6417758/is-there-a-possibility-to-hide-the-type-entry-when-marshalling-subclasses-to?rq=1 – Blender Dec 26 '12 at 22:32
  • Could you add details about your object model? Then I can help you remove the inheritance indicator. – bdoughan Dec 28 '12 at 16:55
  • @Blender I'm not sure it's a pure duplicate. The question also wants to change the variable names. – Mark Robinson Dec 29 '12 at 00:43

1 Answers1

1

Based on the output from your question, you are currently not using EclipseLink JAXB (MOXy)'s native JSON binding. The following should help.

Java Model

Below is my best guess at your object model based on your post. I have added the metadata necessary to get the output that you are looking for.

  • The @XmlElement annotation can be used to change the name of a key.
  • I have used the @XmlSchemaType annotation to control the format of the Date property.

package forum14047050;

import java.util.Date;
import javax.xml.bind.annotation.*;

@XmlType(propOrder={"costMethod", "fundingDate", "fundingAmount"})
public class Sma {

    private String costMethod;
    private Date fundingDate;
    private double fundingAmount;

    @XmlElement(name="_costMethod")
    public String getCostMethod() {
        return costMethod;
    }

    public void setCostMethod(String costMethod) {
        this.costMethod = costMethod;
    }

    @XmlSchemaType(name="date")
    public Date getFundingDate() {
        return fundingDate;
    }

    public void setFundingDate(Date fundingDate) {
        this.fundingDate = fundingDate;
    }

    public double getFundingAmount() {
        return fundingAmount;
    }

    public void setFundingAmount(double fundingAmount) {
        this.fundingAmount = fundingAmount;
    }

}

jaxb.properties

To use MOXy as your JAXB (JSR-222) provider you need to include a file called jaxb.properties in the same package as your domain model (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html).

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

Demo Code

package forum14047050;

import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextProperties;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String, Object>(2);
        properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
        properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
        JAXBContext jc = JAXBContext.newInstance(new Class[] {Sma.class}, properties);

        Sma sma = new Sma();
        sma.setCostMethod("Average");
        sma.setFundingDate(new Date(108, 9, 2));
        sma.setFundingAmount(2510959.95);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(sma, System.out);
    }

}

Output

Below is the output from running the demo code. Unlike your question I have quotes around the date value. This is required to make it valid JSON.

{
   "_costMethod" : "Average",
   "fundingDate" : "2008-10-02",
   "fundingAmount" : 2510959.95
}

For More Information

bdoughan
  • 147,609
  • 23
  • 300
  • 400