TrudleR TrudleR - 1 month ago 6x
Java Question

Pretty output always inserts "\r\n"

I'm working on an expenses calculator. Before, I had a working XML output that didn't print the linebreaks. I think something got messed up, maybe that the editor doesn't see the linebreak as a linebreak, no idea.

But when I save my expenses, I get a very ugly output:

"[ {\r\n \"title\" : \"2\",\r\n \"category\" : \"[None]\",\r\n \"period\" : \"Year\",\r\n \"value\" : \"2\"\r\n}, {\r\n \"title\" : \"3\",\r\n \"category\" : \"[None]\",\r\n \"period\" : \"Year\",\r\n \"value\" : \"3\"\r\n} ]"

If I look at the console (doing a print there), I get what I want:

[ {
"title" : "2",
"category" : "[None]",
"period" : "Year",
"value" : "2"
}, {
"title" : "3",
"category" : "[None]",
"period" : "Year",
"value" : "3"
} ]

Currently, my code is quite simple:

ObservableList<Expense> expenseList = FXCollections.observableArrayList();
//The list gets edited by a TableView.
final ObjectMapper mapper = new ObjectMapper();
final String s = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(expenseList);
FileOutputStream fos = new FileOutputStream(path);
OutputStreamWriter outputFile = new OutputStreamWriter(fos, Charset.forName("UTF8"));

mapper.writeValue(outputFile, s);

This is how the entity class looks:

public class Expense {
private StringProperty title = new SimpleStringProperty();
private StringProperty category = new SimpleStringProperty();
private StringProperty period = new SimpleStringProperty();
private StringProperty value = new SimpleStringProperty();

public void setTitle(String title) {

public void setCategory(String category) {

public void setPeriod(String period) {

public void setValue(String value) {

public Expense() {} //Default constructor is needed for XML-handling

public Expense(String title, String value, String period, String category) {
this.title = new SimpleStringProperty(title);
this.value = new SimpleStringProperty(value);
this.period = new SimpleStringProperty(period);
this.category = new SimpleStringProperty(category);

public String getTitle() {
return this.title.get();

public String getCategory() {
return this.category.get();

public String getPeriod() {
return this.period.get();

public String getValue() {
return this.value.get();

public StringProperty titleProperty() {
return this.title;

public StringProperty categoryProperty() {
return this.category;

public StringProperty periodProperty() {
return this.period;

public StringProperty valueProperty() {
return this.value;

I tried to get every single in the list and convert it to a json string -> concat it to a big single string which I printed out. Same result though.


What this line does

mapper.writeValue(outputFile, s);

Is take a String and write it as a value in a JSON message. If that String already contains special characters, they have to be escaped out.

What you wanted most likely is

 try (FileOutputStream fos = new FileOutputStream(path);
      OutputStreamWriter outputFile = new OutputStreamWriter(fos, StandardCharsets.UTF_8)) {
    outputFile .write(s); // just write the String as it is