Can't parse as Text fields that contain only numbers. #8

Closed
meteficha opened this Issue May 24, 2012 · 5 comments

Comments

Projects
None yet
3 participants

If you have a field whose value is parseable as a number, then you can't parse it as Text. Simple test case follows:

{-# LANGUAGE OverloadedStrings #-}
import Data.Text (Text)
import qualified Data.Yaml as Y

data S = S Text deriving (Show)

instance Y.FromJSON S where
  parseJSON (Y.Object obj) = fmap S $ obj Y..: "val"

test :: Either String S
test = Y.decodeEither "val: \"1\""

I'm not sure what would be the best solution to this problem, so I'm just opening an issue =).

Owner

snoyberg commented May 24, 2012

I actually just got bitten by this the other day too. It's strange, I was sure I wrote this patch previously, but apparently not.

Anyway, basic solution: if it's quoted, treat it as a string. If it's not quoted, try to parse it. Sound reasonable?

Works for me! =)

It just sounded hacky when I thought about this solution (that's why I didn't even suggest it on this issue), but OTOH I don't have any better ideas.

Owner

snoyberg commented May 24, 2012

I agree that it sounds a bit hacky, but I think it's the same approach taken in JSON parsers. I remember having discussions about how to implement this all properly back in the days of data-object-json.

meteficha closed this May 28, 2012

I have the same issue... If we place for example 0034, it will output strange numbers. You have a fix for that?

Owner

snoyberg commented Dec 7, 2014

That sounds like a different issue: the leading 0 is probably indicating that the value should be parsed as an octal. Does that match the output you're seeing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment