Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert POJO to JSON with slash in field name via moxy

Tags:

java

json

jaxb

moxy

I'm trying to put JAXB annotations on POJO to produce following JSON (via moxy):

{
    "apartmentNumber": "404",
    "city/town": "SomeCity",
    "state/province": "NoState",
    "street": "1st Street"
}

Some fields contains slashes. When I'm trying to put annotation @XmlElement(name="city/town")

@XMLRootElement
public class SubscriberAddress {
    private String street;
    private String apartmentNumber;

    @XMLElement(name="city/town")
    private String city;

    @XMLElement(name="state/province")
    private String state;
}

moxy treats such names as XPaths and creates following JSON

{
     "apartmentNumber" : "404",
     "city" : {
         "town" : "SomeCity"
     },
     "state" : {
         "province" : "NoState"
     },
     "street" : "1st Street"
}

Is there any way to escape slash / forbid moxy to treat slashes in element names?

like image 569
stborod Avatar asked Jul 13 '16 20:07

stborod


People also ask

How to convert POJO to JSON using Jackson API?

The ObjectMapper class provided by Jackson API provides functionality for converting between Java Objects and JSON. If you are using Maven as a build tool in your project, then add the following dependency in your pom.xml file. Convert POJO to JSON and vice versa using Jackson API...!!!

How to convert JSON string to Java object?

Here's how you can convert your JSON string to JAVA objects, we will be using the converter and external libraries like Jackson objectmapper to parse our object. 1. Copy and paste your JSON in the first code editor and click "Convert" Make sure that your JSON object is not large (over 5MB) and is formatted.

How do I serialize a field name in Java to JSON?

If you want to serialize your Java object and map a field name in Java to a different property name in JSON, you can use @JsonGetter . Note that there's no @JsonProperty annotation. Also there's no getName () method. Instead, there's a getEmployeeName () method that's annotated with @JsonGetter ("name").

How does jsonschema2pojo map format values to Java types?

How jsonschema2pojo maps format values to Java types: When the extends rule is encountered in your JSON schema (to indicate that one type extends another), this will produce an extends relationship in your generated Java types. That extends value can be a schema given in-line, or referenced using $ref.


1 Answers

Instead of moxy U may try GSON, as it is producing the result.

The POJO is :

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class SubscriberAddress {
    private String street;
    private String apartmentNumber;

    @XmlElement(name = "city/town")
    private String city;

    @XmlElement(name = "state/province")
    private String state;

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public String getApartmentNumber() {
        return apartmentNumber;
    }

    public void setApartmentNumber(String apartmentNumber) {
        this.apartmentNumber = apartmentNumber;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }
}

The JSON generated is :

{
  "SubscriberAddress": {
    "street": "Demo Street",
    "apartmentNumber": "Demo Apartment",
    "city/town": "Demo City",
    "state/province": "Demo State"
  }
}
like image 188
Roushan Kumar Avatar answered Sep 30 '22 09:09

Roushan Kumar