How can I get Gson to use accessors rather than fields?

后端 未结 2 949
再見小時候
再見小時候 2021-02-20 00:18

By default Gson uses fields as a basis for it\'s serialization. Is there a way to get it to use accessors instead?

2条回答
  •  醉酒成梦
    2021-02-20 00:46

    Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

    If you can't get Gson to do what you want, below is how you can accomplish this using MOXy's native JSON binding. MOXy like any JAXB implementation will use property (public) access by default. You can configure field access using @XmlAccessorType(XmlAccessType.FIELD). Below is an example:

    Customer

    package forum11385214;
    
    public class Customer {
    
        private String foo;
        private Address bar;
    
        public String getName() {
            return foo;
        }
    
        public void setName(String name) {
            this.foo = name;
        }
    
        public Address getAddress() {
            return bar;
        }
    
        public void setAddress(Address address) {
            this.bar = address;
        }
    
    }
    

    Address

    package forum11385214;
    
    public class Address {
    
        private String foo;
    
        public String getStreet() {
            return foo;
        }
    
        public void setStreet(String street) {
            this.foo = street;
        }
    
    }
    

    jaxb.properties

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

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

    Demo

    package forum11385214;
    
    import java.util.*;
    import javax.xml.bind.*;
    import javax.xml.transform.stream.StreamSource;
    import org.eclipse.persistence.jaxb.JAXBContextProperties;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            Map properties = new HashMap(2);
            properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
            properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
            JAXBContext jc = JAXBContext.newInstance(new Class[] {Customer.class}, properties);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            StreamSource json = new StreamSource("src/forum11385214/input.json");
            Customer customer = (Customer) unmarshaller.unmarshal(json, Customer.class).getValue();
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(customer, System.out);
        }
    
    }
    

    input.json/Output

    {
        "name" : "Jane Doe",
        "address" : {
            "street" : "1 Any Street"
        }
    }
    

    For More Information

    • http://blog.bdoughan.com/2011/08/json-binding-with-eclipselink-moxy.html
    • http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html
    • http://blog.bdoughan.com/2012/04/jaxb-and-unmapped-properties.html

提交回复
热议问题