1

I'm writing a JSON parser in Haskell but I'm having trouble parsing unicode values. If I want to convert \u2013 to a character, I can just wrap it in quotes in Python and I get '–'. I'm having trouble figuring out how to do the same in Haskell. If I wrap it in quotes, I get the error "lexical error in string/character literal at character 'u'" If I run putStrLn "\2013", I get the character 'ߝ'. How can I get the '–' character in Haskell?

2
  • Note, it's a coincidence that both JSON and Python use the same syntax for writing arbitrary Unicode characters in ASCII. Commented Oct 14, 2022 at 19:39
  • 1
    "Wrapping in quotes" is a Haskell compile time feature that you wouldn't try to use to parse strings at runtime. You would instead parse \u2013 to extract the integer value 8211 and use Data.Char.chr :: Int -> Char on it. Commented Oct 14, 2022 at 19:43

1 Answer 1

5

Plain digits is base 10; for base 16, use the x prefix.

> putStrLn "\x2013" – 

BUT you shouldn't need to know this. Use aeson for your JSON parsing needs.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this worked. I'm trying to make a JSON parser mostly as a learning exercise but thanks for the recommendation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.