0

I have a list which contains Objects of OClass . I want to create the JSON of this Objects . Here's my code:

public void getClasss() {

    // returns all the top classes present in the ontology.

    classlist = new ArrayList<OClass>();     
    Set<OClass> topclasses = ontology.getOClasses(true);
    for (OClass oClasses : topclasses) {                
        classlist.add(oClasses);                
    }   

    OntoCreationClass ocreat = new OntoCreationClass();

    ocreat.OntoCreation(classlist);   
}

public class OntoCreationClass {    
    public static String data;
    public void OntoCreation(List<OClass> list) {

        System.out.println(list.get(0));

        OutputStream out = new ByteArrayOutputStream();

        ObjectMapper mapper = new ObjectMapper();

        try {
            mapper.writeValue(out, list);
        } catch (JsonGenerationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

     }
}

Exception is :

com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class com.ontotext.trree.owlim_ext.r and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: java.util.ArrayList[0]->gate.creole.ontology.impl.OClassImpl["ontology"]-gate.creole.ontology.impl.sesame.OWLIMOntology["service"]-gate.creole.ontology.impl.sesame.OntologyServiceImplSesame["sesameManager"]-gate.creole.ontology.impl.sesame.SesameManager["repositoryConnection"]-org.openrdf.repository.sail.SailRepositoryConnection["repository"]-org

.openrdf.repository.sail.SailRepository["sail"]-com.ontotext.trree.owlim_ext.SailImpl["pool"])

>

How can i solve this error ? can anyone help ?

1 Answer 1

1

If the Access Modifiers of fields in your class are private, then you must put Setter and Getter to make this class Serializable.

Look this sample code to store list of objects of Person class:

1- Person class:

public class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

2- Add Person object to the List:

List<Person> persons = new ArrayList<Person>();
persons.add(new Person("Mike", 26));
persons.add(new Person("David", 20));
persons.add(new Person("Sara", 21));
persons.add(new Person("Amanda", 15));

3- Then you can use writeValue right now:

ObjectMapper objectMapper = new ObjectMapper();
    try {
        objectMapper.writeValue(new FileOutputStream(jsonPath), persons);
    } catch (IOException e) {
        e.printStackTrace();
    }

4- Finally you can see you JSON file:

[{"name":"Mike","age":26},{"name":"David","age":20},{"name":"Sara","age":21},{"name":"Amanda","age":15}]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.