Skip to content

Instantly share code, notes, and snippets.

@drealecs
Created April 27, 2018 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drealecs/205433bbba136f712632e40fc17a6dfc to your computer and use it in GitHub Desktop.
Save drealecs/205433bbba136f712632e40fc17a6dfc to your computer and use it in GitHub Desktop.
json csv escaping
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import java.io.IOException;
public class JsonCsvEscapingTest {
public static void main(String[] args) throws IOException {
int intColumnValue = 5;
String stringColumnValue = "asds";
String stringColumnValueWithQuotes = "asds \"test\" sdfg";
final TestObject testObject = new TestObject(12, "test", "asd \"qwe\" zxc");
final ObjectMapper mapper = new ObjectMapper();
String jsonColumnValue = mapper.writeValueAsString(testObject);
Object[] csvColumn = {intColumnValue, stringColumnValue, stringColumnValueWithQuotes, jsonColumnValue};
CSVPrinter csvPrinter = new CSVPrinter(System.out, CSVFormat.DEFAULT);
csvPrinter.printRecord(csvColumn);
}
public static class TestObject {
private int number;
private String string;
private String stringWithQuotes;
public TestObject(int number, String string, String stringWithQuotes) {
this.number = number;
this.string = string;
this.stringWithQuotes = stringWithQuotes;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getString() {
return string;
}
public void setString(String string) {
this.string = string;
}
public String getStringWithQuotes() {
return stringWithQuotes;
}
public void setStringWithQuotes(String stringWithQuotes) {
this.stringWithQuotes = stringWithQuotes;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment