We have released a new open source library for CSV parsing that provides good error-messages and plays nicely with Cassava.
Many libraries in the Haskell ecosystem are designed for speed at the expense of other merits such as flexibility and quality of error messages. Examples of such libraries include:
There is nothing bad with this approach, but sometimes speed is just not that important and you wish you could provide user-friendly error messages or make your life easier by using a more flexible library instead.
Now CSV parsing in Haskell can be done with good error reporting with help
library. It replaces some built-in functions in Cassava:
The new functions work just the same as the old ones and play with the rest of Cassava library seamlessly, except in case of parser failure they return typed error message generated by Megaparsec. The error message can be inspected and rendered.
As an example of why you would want to use
cassava, here are some malformed CSV data and rendered
error messages produced by the built-in
decode function and the
function from the
Input (trying to parse
(String, Maybe Int, Double)):
parse error (Failed reading: conversion error: cannot unpack array of length 1 into a 3-tuple. Input record: ["fo"]) at ""
unexpected end of input
expecting '"', escaped double-quote, or unescaped character
parse error (Failed reading: conversion error: expected Double, got "boo" (Failed reading: takeWhile1)) at ""
conversion error: expected Double, got "boo" (Failed reading: takeWhile1)
The quotes speak for themselves. Note how Cassava's type conversion is integrated right into Megaparsec parser.
Here are some guides on how to use the Cassava library:
cassava-megaparsec just import
decode (and its friends if needed)
Data.Csv.Parser.Megaparsec and add
hiding clause to
import Data.Csv to
avoid name conflicts:
import Data.Csv hiding (decode, decodeWith, decodeByName, decodeByNameWith)
import Data.Csv.Parser.Megaparsec (decode, decodeWith, decodeByName, decodeByNameWith)
cassava-megaparsec with Stack, add it to
extra-deps or use a
recent nightly resolver.
Published on Jun. 29, 2016
Join our community of avid readers and stay informed with the latest articles, tips, and insights delivered straight to your inbox. Don't miss out on valuable content – subscribe now and be part of the conversation!