Created
April 27, 2018 08:34
-
-
Save drealecs/205433bbba136f712632e40fc17a6dfc to your computer and use it in GitHub Desktop.
json csv escaping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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