Author: brandonwilliams Date: Thu Jun 16 20:03:57 2011 New Revision: 1136637
URL: http://svn.apache.org/viewvc?rev=1136637&view=rev Log: sstable2json escapes quotes. Patch by Pavel Yaskevich, reviewed by brandonwilliams for CASSANDRA-2780 Modified: cassandra/branches/cassandra-0.8/src/java/org/apache/cassandra/tools/SSTableExport.java cassandra/branches/cassandra-0.8/test/unit/org/apache/cassandra/SchemaLoader.java cassandra/branches/cassandra-0.8/test/unit/org/apache/cassandra/tools/SSTableExportTest.java Modified: cassandra/branches/cassandra-0.8/src/java/org/apache/cassandra/tools/SSTableExport.java URL: http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.8/src/java/org/apache/cassandra/tools/SSTableExport.java?rev=1136637&r1=1136636&r2=1136637&view=diff ============================================================================== --- cassandra/branches/cassandra-0.8/src/java/org/apache/cassandra/tools/SSTableExport.java (original) +++ cassandra/branches/cassandra-0.8/src/java/org/apache/cassandra/tools/SSTableExport.java Thu Jun 16 20:03:57 2011 @@ -81,7 +81,12 @@ public class SSTableExport */ private static String quote(String val) { - return String.format("\"%s\"", val); + return String.format("\"%s\"", escapeQuotes(val)); + } + + private static String escapeQuotes(String val) + { + return val.replace("\"", "\\\""); } /** Modified: cassandra/branches/cassandra-0.8/test/unit/org/apache/cassandra/SchemaLoader.java URL: http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.8/test/unit/org/apache/cassandra/SchemaLoader.java?rev=1136637&r1=1136636&r2=1136637&view=diff ============================================================================== --- cassandra/branches/cassandra-0.8/test/unit/org/apache/cassandra/SchemaLoader.java (original) +++ cassandra/branches/cassandra-0.8/test/unit/org/apache/cassandra/SchemaLoader.java Thu Jun 16 20:03:57 2011 @@ -113,6 +113,12 @@ public class SchemaLoader standardCFMD(ks1, "Standard4"), standardCFMD(ks1, "StandardLong1"), standardCFMD(ks1, "StandardLong2"), + new CFMetaData(ks1, + "ValuesWithQuotes", + st, + BytesType.instance, + null) + .defaultValidator(UTF8Type.instance), superCFMD(ks1, "Super1", LongType.instance), superCFMD(ks1, "Super2", LongType.instance), superCFMD(ks1, "Super3", LongType.instance), Modified: cassandra/branches/cassandra-0.8/test/unit/org/apache/cassandra/tools/SSTableExportTest.java URL: http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.8/test/unit/org/apache/cassandra/tools/SSTableExportTest.java?rev=1136637&r1=1136636&r2=1136637&view=diff ============================================================================== --- cassandra/branches/cassandra-0.8/test/unit/org/apache/cassandra/tools/SSTableExportTest.java (original) +++ cassandra/branches/cassandra-0.8/test/unit/org/apache/cassandra/tools/SSTableExportTest.java Thu Jun 16 20:03:57 2011 @@ -22,17 +22,15 @@ import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.PrintStream; -import java.nio.ByteBuffer; -import java.util.Arrays; import org.apache.cassandra.SchemaLoader; -import org.apache.cassandra.config.DatabaseDescriptor; import org.apache.cassandra.db.ColumnFamily; import org.apache.cassandra.db.CounterColumn; import org.apache.cassandra.db.ExpiringColumn; +import org.apache.cassandra.db.Column; import org.apache.cassandra.db.filter.QueryFilter; import org.apache.cassandra.db.filter.QueryPath; -import org.apache.cassandra.dht.IPartitioner; +import org.apache.cassandra.db.marshal.UTF8Type; import org.apache.cassandra.io.sstable.Descriptor; import org.apache.cassandra.io.sstable.SSTableReader; import org.apache.cassandra.io.sstable.SSTableWriter; @@ -243,4 +241,30 @@ public class SSTableExportTest extends S assert ((String) colA.get(3)).equals("c"); assert (Long) colA.get(4) == Long.MIN_VALUE; } + + @Test + public void testEscapingDoubleQuotes() throws IOException + { + File tempSS = tempSSTableFile("Keyspace1", "ValuesWithQuotes"); + ColumnFamily cfamily = ColumnFamily.create("Keyspace1", "ValuesWithQuotes"); + SSTableWriter writer = new SSTableWriter(tempSS.getPath(), 2); + + // Add rowA + cfamily.addColumn(null, new Column(ByteBufferUtil.bytes("data"), UTF8Type.instance.fromString("{\"foo\":\"bar\"}"))); + writer.append(Util.dk("rowA"), cfamily); + cfamily.clear(); + + SSTableReader reader = writer.closeAndOpenReader(); + + // Export to JSON and verify + File tempJson = File.createTempFile("ValuesWithQuotes", ".json"); + SSTableExport.export(reader, new PrintStream(tempJson.getPath()), new String[0]); + + JSONObject json = (JSONObject) JSONValue.parse(new FileReader(tempJson)); + + JSONArray rowA = (JSONArray)json.get(asHex("rowA")); + JSONArray data = (JSONArray)rowA.get(0); + assert hexToBytes((String)data.get(0)).equals(ByteBufferUtil.bytes("data")); + assert data.get(1).equals("{\"foo\":\"bar\"}"); + } }