Last active
October 26, 2020 03:42
-
-
Save imanabu/322e18507f8c5b8ec649a24280a8f8af to your computer and use it in GitHub Desktop.
Scala Fully Parse CSV lines from a File, supports ignoring commans in quoted strings. Can generate a JSON DTO.
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
package dtos | |
import play.api.libs.json.{Format, Json} | |
case class ITableRow( | |
cols: Seq[String] | |
) | |
object ITableRow { | |
implicit val jsonFormatter: Format[ITableRow] = Format(Json.reads[ITableRow], Json.writes[ITableRow]) | |
} | |
// AND | |
package dtos | |
import play.api.libs.json.{Format, Json} | |
case class ITable( | |
rows: Seq[ITableRow] | |
) | |
object ITable { | |
implicit val jsonFormatter: Format[ITable] = Format(Json.reads[ITable], Json.writes[ITable]) | |
} | |
// AND Use the following snip in your code | |
// ----------------------------------------------------------------------- | |
val lines = Files.readAllLines(Paths.get(uri)).asScala | |
val parseCsvLine = (line: String) => { | |
var v = Vector.empty[String] | |
var inQuote = false | |
var s = "" | |
for(c:Char <- line.toCharArray) { | |
if (c == '"') { | |
inQuote = !inQuote | |
} else { | |
if (c == ',' && !inQuote) { | |
if (s == "") { | |
s = " " | |
} | |
v = v :+ s | |
s = "" | |
} else { | |
if (s.contains("________")) { | |
s = "________" | |
} | |
s += c | |
} | |
} | |
} | |
v = v :+ s | |
v | |
} | |
val rows = lines.map(line => { | |
val l = parseCsvLine(line) | |
ITableRow(l) | |
}) | |
ITable(rows) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment