@@ -12,6 +12,7 @@ module Language.PureScript.PSString
1212
1313import Prelude.Compat
1414import Control.Exception (try, evaluate)
15+import Control.Applicative ((<|>))
1516import Data.Char (chr)
1617import Data.Bits (shiftR)
1718import Data.List (unfoldr)
@@ -119,13 +120,20 @@ instance A.ToJSON PSString where
119120 toJSON = A.toJSON . toUTF16CodeUnits
120121
121122instance A.FromJSON PSString where
122− parseJSON a = PSString <$> parseArrayOfCodeUnits a
123+ parseJSON a = currentParser <|> backwardsCompat
123124 where
125+ currentParser = PSString <$> parseArrayOfCodeUnits a
126+
124127 parseArrayOfCodeUnits :: A.Value -> A.Parser [Word16]
125128 parseArrayOfCodeUnits = A.withArray "array of UTF-16 code units" (traverse parseCodeUnit . V.toList)
129+
126130 parseCodeUnit :: A.Value -> A.Parser Word16
127131 parseCodeUnit b = A.withScientific "two-byte non-negative integer" (maybe (A.typeMismatch "" b) return . toBoundedInteger) b
128132
133+ -- For backwards compatibility: this allows us to parse JSON produced by
134+ -- 0.10.4 or earlier
135+ backwardsCompat = fromString <$> A.parseJSON a
136+
129137-- |
130138-- Pretty print a PSString, using JavaScript escape sequences. Intended for
131139-- use in compiled JS output.