简体   繁体   中英

What's a quick and simple way to generate a sample JSON document from an XSD XML schema (using JAXB)?

My project is using JAXB to turn XSD (XML schema) into POJO and cxf to turn the populated classes into JSON. Is there a tool that can take the schema, and generate a sample JSON document for me? Ideally command line, or a 5 line Java snippet.

Functionality-wise, I want something similar to what SoapUI does when you feed it a WSDL (ie, amongst other things, generate a sample request from schema and pre-populate all strings with a ? question mark).

I basically want a quick way to check if the XSD schema changes produce the JSON structure I want (so I do care about structure and types, not about values).

NB: I don't want to create a JSON schema, and I can't use a JSON schema instead of an XSD.

You can create a json directly from the classes created with jaxb.

Jaxb create pojo classes.

Any json library can create the json from a pojo instance.

Here are the steps:

  • Create your xsd
  • Create the classes from the xsd using the tool xjc
  • Create an instance of the classes
  • Pass the instance to the pojo library and create a String from it

Here an example with faster jackson :

ObjectMapper mapper = new ObjectMapper();

// PojoClass is the class created with xjc from your xsd
PojoClass pojoInstance = new PojoClass();  

// Populate pojoInstance as needed

String jsonString = mapper.writeValueAsString(pojoInstance);
System.out.println(jsonString);  // Print the pojoInstance as json string

Creating a random object can be done with a code similar to the following. Note that this code creates only primitive types and objects with primitive types or references to other objects. For arrays, list, maps you need to enhance it.

public class RandomObjectFiller {

    private Random random = new Random();

    public <T> T createAndFill(Class<T> clazz) throws Exception {
        T instance = clazz.newInstance();
        for(Field field: clazz.getDeclaredFields()) {
            field.setAccessible(true);
            Object value = getRandomValueForField(field);
            field.set(instance, value);
        }
        return instance;
    }

    private Object getRandomValueForField(Field field) throws Exception {
        Class<?> type = field.getType();


        if(type.equals(Integer.TYPE) || type.equals(Integer.class)) {
            return random.nextInt();
        } else if(type.equals(Long.TYPE) || type.equals(Long.class)) {
            return random.nextLong();
        } else if(type.equals(Double.TYPE) || type.equals(Double.class)) {
            return random.nextDouble();
        } else if(type.equals(Float.TYPE) || type.equals(Float.class)) {
            return random.nextFloat();
        } else if(type.equals(String.class)) {
            return UUID.randomUUID().toString();
        } 
        return createAndFill(type);
    }
}

The previous example using this class is the following code:

ObjectMapper mapper = new ObjectMapper();

RandomObjectFiller randomObjectFiller = new RandomObjectFiller();

// PojoClass is the class created with xjc from your xsd
PojoClass pojoInstance = randomObjectFiller.createAndFill(PojoClass.class);

String jsonString = mapper.writeValueAsString(pojoInstance);
System.out.println(jsonString);  // Print the pojoInstance as json string

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM