Search in sources :

Example 1 with CSVSerializer

use of org.apache.pivot.serialization.CSVSerializer in project pivot by apache.

the class CSVSerializerTest method testQuotedQuoteReadObject.

@Test
public void testQuotedQuoteReadObject() throws IOException, SerializationException {
    StringBuilder buf = new StringBuilder();
    buf.append("a,\"\"\"b\"\"\",c\r\n");
    StringReader reader = new StringReader(buf.toString());
    CSVSerializer serializer = new CSVSerializer();
    serializer.setKeys("A", "B", "C");
    List<?> result = serializer.readObject(reader);
    @SuppressWarnings("unchecked") Dictionary<String, Object> row = (Dictionary<String, Object>) result.get(0);
    assertEquals("a", row.get("A"));
    assertEquals("\"b\"", row.get("B"));
    assertEquals("c", row.get("C"));
}
Also used : Dictionary(org.apache.pivot.collections.Dictionary) StringReader(java.io.StringReader) CSVSerializer(org.apache.pivot.serialization.CSVSerializer) Test(org.junit.Test)

Example 2 with CSVSerializer

use of org.apache.pivot.serialization.CSVSerializer in project pivot by apache.

the class CSVSerializerTest method testQuotedQuoteWriteObject.

@SuppressWarnings("unchecked")
// or it will generate a warning during build with Java 7
@Test
public void testQuotedQuoteWriteObject() throws IOException {
    List<Object> items = new ArrayList<>();
    items.add(new HashMap<>(new Dictionary.Pair<String, Object>("A", "a"), new Dictionary.Pair<String, Object>("B", "\"b\""), new Dictionary.Pair<String, Object>("C", "c")));
    StringWriter writer = new StringWriter();
    CSVSerializer serializer = new CSVSerializer();
    serializer.setKeys("A", "B", "C");
    serializer.writeObject(items, writer);
    assertEquals("a,\"\"\"b\"\"\",c\r\n", writer.toString());
}
Also used : StringWriter(java.io.StringWriter) ArrayList(org.apache.pivot.collections.ArrayList) CSVSerializer(org.apache.pivot.serialization.CSVSerializer) Test(org.junit.Test)

Example 3 with CSVSerializer

use of org.apache.pivot.serialization.CSVSerializer in project pivot by apache.

the class CSVSerializerTest method testInlineKeys.

@Test
public void testInlineKeys() throws IOException, SerializationException {
    StringBuilder buf = new StringBuilder();
    buf.append("A \t, B ,C \n");
    buf.append("a1,b1,c1\n");
    StringReader reader = new StringReader(buf.toString());
    CSVSerializer serializer = new CSVSerializer();
    List<?> result = serializer.readObject(reader);
    @SuppressWarnings("unchecked") Dictionary<String, Object> row = (Dictionary<String, Object>) result.get(0);
    assertEquals(row.get("A"), "a1");
    assertEquals(row.get("B"), "b1");
    assertEquals(row.get("C"), "c1");
}
Also used : Dictionary(org.apache.pivot.collections.Dictionary) StringReader(java.io.StringReader) CSVSerializer(org.apache.pivot.serialization.CSVSerializer) Test(org.junit.Test)

Example 4 with CSVSerializer

use of org.apache.pivot.serialization.CSVSerializer in project pivot by apache.

the class CSVSerializerTest method testQuotedCommaWriteObject.

@SuppressWarnings("unchecked")
// or it will generate a warning during build with Java 7
@Test
public void testQuotedCommaWriteObject() throws IOException {
    List<Object> items = new ArrayList<>();
    items.add(new HashMap<>(new Dictionary.Pair<String, Object>("A", "a"), new Dictionary.Pair<String, Object>("B", ",b,"), new Dictionary.Pair<String, Object>("C", "c")));
    StringWriter writer = new StringWriter();
    CSVSerializer serializer = new CSVSerializer();
    serializer.setKeys("A", "B", "C");
    serializer.writeObject(items, writer);
    assertEquals("a,\",b,\",c\r\n", writer.toString());
}
Also used : StringWriter(java.io.StringWriter) ArrayList(org.apache.pivot.collections.ArrayList) CSVSerializer(org.apache.pivot.serialization.CSVSerializer) Test(org.junit.Test)

Example 5 with CSVSerializer

use of org.apache.pivot.serialization.CSVSerializer in project pivot by apache.

the class CSVSerializerTest method testQuotedCommaReadObject.

@Test
public void testQuotedCommaReadObject() throws IOException, SerializationException {
    StringBuilder buf = new StringBuilder();
    buf.append("a,\",b,\",c\r\n");
    StringReader reader = new StringReader(buf.toString());
    CSVSerializer serializer = new CSVSerializer();
    serializer.setKeys("A", "B", "C");
    List<?> result = serializer.readObject(reader);
    @SuppressWarnings("unchecked") Dictionary<String, Object> row = (Dictionary<String, Object>) result.get(0);
    assertEquals("a", row.get("A"));
    assertEquals(",b,", row.get("B"));
    assertEquals("c", row.get("C"));
}
Also used : Dictionary(org.apache.pivot.collections.Dictionary) StringReader(java.io.StringReader) CSVSerializer(org.apache.pivot.serialization.CSVSerializer) Test(org.junit.Test)

Aggregations

CSVSerializer (org.apache.pivot.serialization.CSVSerializer)11 Test (org.junit.Test)9 StringReader (java.io.StringReader)5 ArrayList (org.apache.pivot.collections.ArrayList)5 StringWriter (java.io.StringWriter)4 Dictionary (org.apache.pivot.collections.Dictionary)4 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 DateFormat (java.text.DateFormat)1 Date (java.util.Date)1 ServletException (javax.servlet.ServletException)1 List (org.apache.pivot.collections.List)1 CSVSerializerListener (org.apache.pivot.serialization.CSVSerializerListener)1 SerializationException (org.apache.pivot.serialization.SerializationException)1 Task (org.apache.pivot.util.concurrent.Task)1 TaskListener (org.apache.pivot.util.concurrent.TaskListener)1 GetQuery (org.apache.pivot.web.GetQuery)1