Convert composite java object to CSV with Jackson












2















With Jackson, I need to convert an instance of my class Test in CSV but I'm getting problems with a class that contains one list (Inner)



Ex:



public class Test {

String testName;

@JsonUnwrapped
Simple simple;

@JsonUnwrapped
Inner inner;

public Test(String testName, Simple simple, Inner inner) {
this.testName = testName;
this.simple = simple;
this.inner = inner;
}

public String getTestName() {
return testName;
}

public void setTestName(String testName) {
this.testName = testName;
}

public Simple getSimple() {
return simple;
}

public void setSimple(Simple simple) {
this.simple = simple;
}

public Inner getInner() {
return inner;
}

public void setInner(Inner inner) {
this.inner = inner;
}
}

class Inner {
@JsonUnwrapped
List<Person> persons;

public Inner(List<Person> persons) {
this.persons = persons;
}

public List<Person> getPersons() {
return persons;
}

public void setPersons(List<Person> persons) {
this.persons = persons;
}
}

class Person {
String name;

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

public String getName() {
return name;
}

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

class Simple {
String simpleName;

public Simple(String simpleName) {
this.simpleName = simpleName;
}

public String getSimpleName() {
return simpleName;
}

public void setSimpleName(String simpleName) {
this.simpleName = simpleName;
}
}

class Main {
public static void main(String args) {
Simple simple = new Simple("simple");
Person person = new Person("jesus");
Inner inner = new Inner(Arrays.asList(person));

Test test = new Test("test", simple, inner);
CsvMapper mapper = new CsvMapper();
CsvSchema schema = mapper.schemaFor(Test.class);
try {
String csv = mapper.writer(schema).writeValueAsString(test);
} catch (JsonProcessingException e) {
e.printStackTrace();
}

}
}


For objects properties, I used the annotation @JsonUnwrapped as recommended on this link ,
but I get one exception when jackson try to convert the list Inner.persons:



enter image description here



How can I fix that ?










share|improve this question

























  • The docs (fasterxml.github.io/jackson-annotations/javadoc/2.0.0/com/…) says "can not unwrap JSON arrays using this mechanism"

    – Perdi Estaquel
    Nov 23 '18 at 0:40
















2















With Jackson, I need to convert an instance of my class Test in CSV but I'm getting problems with a class that contains one list (Inner)



Ex:



public class Test {

String testName;

@JsonUnwrapped
Simple simple;

@JsonUnwrapped
Inner inner;

public Test(String testName, Simple simple, Inner inner) {
this.testName = testName;
this.simple = simple;
this.inner = inner;
}

public String getTestName() {
return testName;
}

public void setTestName(String testName) {
this.testName = testName;
}

public Simple getSimple() {
return simple;
}

public void setSimple(Simple simple) {
this.simple = simple;
}

public Inner getInner() {
return inner;
}

public void setInner(Inner inner) {
this.inner = inner;
}
}

class Inner {
@JsonUnwrapped
List<Person> persons;

public Inner(List<Person> persons) {
this.persons = persons;
}

public List<Person> getPersons() {
return persons;
}

public void setPersons(List<Person> persons) {
this.persons = persons;
}
}

class Person {
String name;

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

public String getName() {
return name;
}

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

class Simple {
String simpleName;

public Simple(String simpleName) {
this.simpleName = simpleName;
}

public String getSimpleName() {
return simpleName;
}

public void setSimpleName(String simpleName) {
this.simpleName = simpleName;
}
}

class Main {
public static void main(String args) {
Simple simple = new Simple("simple");
Person person = new Person("jesus");
Inner inner = new Inner(Arrays.asList(person));

Test test = new Test("test", simple, inner);
CsvMapper mapper = new CsvMapper();
CsvSchema schema = mapper.schemaFor(Test.class);
try {
String csv = mapper.writer(schema).writeValueAsString(test);
} catch (JsonProcessingException e) {
e.printStackTrace();
}

}
}


For objects properties, I used the annotation @JsonUnwrapped as recommended on this link ,
but I get one exception when jackson try to convert the list Inner.persons:



enter image description here



How can I fix that ?










share|improve this question

























  • The docs (fasterxml.github.io/jackson-annotations/javadoc/2.0.0/com/…) says "can not unwrap JSON arrays using this mechanism"

    – Perdi Estaquel
    Nov 23 '18 at 0:40














2












2








2








With Jackson, I need to convert an instance of my class Test in CSV but I'm getting problems with a class that contains one list (Inner)



Ex:



public class Test {

String testName;

@JsonUnwrapped
Simple simple;

@JsonUnwrapped
Inner inner;

public Test(String testName, Simple simple, Inner inner) {
this.testName = testName;
this.simple = simple;
this.inner = inner;
}

public String getTestName() {
return testName;
}

public void setTestName(String testName) {
this.testName = testName;
}

public Simple getSimple() {
return simple;
}

public void setSimple(Simple simple) {
this.simple = simple;
}

public Inner getInner() {
return inner;
}

public void setInner(Inner inner) {
this.inner = inner;
}
}

class Inner {
@JsonUnwrapped
List<Person> persons;

public Inner(List<Person> persons) {
this.persons = persons;
}

public List<Person> getPersons() {
return persons;
}

public void setPersons(List<Person> persons) {
this.persons = persons;
}
}

class Person {
String name;

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

public String getName() {
return name;
}

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

class Simple {
String simpleName;

public Simple(String simpleName) {
this.simpleName = simpleName;
}

public String getSimpleName() {
return simpleName;
}

public void setSimpleName(String simpleName) {
this.simpleName = simpleName;
}
}

class Main {
public static void main(String args) {
Simple simple = new Simple("simple");
Person person = new Person("jesus");
Inner inner = new Inner(Arrays.asList(person));

Test test = new Test("test", simple, inner);
CsvMapper mapper = new CsvMapper();
CsvSchema schema = mapper.schemaFor(Test.class);
try {
String csv = mapper.writer(schema).writeValueAsString(test);
} catch (JsonProcessingException e) {
e.printStackTrace();
}

}
}


For objects properties, I used the annotation @JsonUnwrapped as recommended on this link ,
but I get one exception when jackson try to convert the list Inner.persons:



enter image description here



How can I fix that ?










share|improve this question
















With Jackson, I need to convert an instance of my class Test in CSV but I'm getting problems with a class that contains one list (Inner)



Ex:



public class Test {

String testName;

@JsonUnwrapped
Simple simple;

@JsonUnwrapped
Inner inner;

public Test(String testName, Simple simple, Inner inner) {
this.testName = testName;
this.simple = simple;
this.inner = inner;
}

public String getTestName() {
return testName;
}

public void setTestName(String testName) {
this.testName = testName;
}

public Simple getSimple() {
return simple;
}

public void setSimple(Simple simple) {
this.simple = simple;
}

public Inner getInner() {
return inner;
}

public void setInner(Inner inner) {
this.inner = inner;
}
}

class Inner {
@JsonUnwrapped
List<Person> persons;

public Inner(List<Person> persons) {
this.persons = persons;
}

public List<Person> getPersons() {
return persons;
}

public void setPersons(List<Person> persons) {
this.persons = persons;
}
}

class Person {
String name;

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

public String getName() {
return name;
}

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

class Simple {
String simpleName;

public Simple(String simpleName) {
this.simpleName = simpleName;
}

public String getSimpleName() {
return simpleName;
}

public void setSimpleName(String simpleName) {
this.simpleName = simpleName;
}
}

class Main {
public static void main(String args) {
Simple simple = new Simple("simple");
Person person = new Person("jesus");
Inner inner = new Inner(Arrays.asList(person));

Test test = new Test("test", simple, inner);
CsvMapper mapper = new CsvMapper();
CsvSchema schema = mapper.schemaFor(Test.class);
try {
String csv = mapper.writer(schema).writeValueAsString(test);
} catch (JsonProcessingException e) {
e.printStackTrace();
}

}
}


For objects properties, I used the annotation @JsonUnwrapped as recommended on this link ,
but I get one exception when jackson try to convert the list Inner.persons:



enter image description here



How can I fix that ?







java csv jackson






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 23:13







Jesus Zavarce

















asked Nov 22 '18 at 21:06









Jesus ZavarceJesus Zavarce

1,069819




1,069819













  • The docs (fasterxml.github.io/jackson-annotations/javadoc/2.0.0/com/…) says "can not unwrap JSON arrays using this mechanism"

    – Perdi Estaquel
    Nov 23 '18 at 0:40



















  • The docs (fasterxml.github.io/jackson-annotations/javadoc/2.0.0/com/…) says "can not unwrap JSON arrays using this mechanism"

    – Perdi Estaquel
    Nov 23 '18 at 0:40

















The docs (fasterxml.github.io/jackson-annotations/javadoc/2.0.0/com/…) says "can not unwrap JSON arrays using this mechanism"

– Perdi Estaquel
Nov 23 '18 at 0:40





The docs (fasterxml.github.io/jackson-annotations/javadoc/2.0.0/com/…) says "can not unwrap JSON arrays using this mechanism"

– Perdi Estaquel
Nov 23 '18 at 0:40












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437981%2fconvert-composite-java-object-to-csv-with-jackson%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437981%2fconvert-composite-java-object-to-csv-with-jackson%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini