git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Restore JSON backwards compatibility for PSStrings

This allows the parser to deal with JSON produced by earlier versions of the compiler (which were generated as JSON strings) as well as the current generator (which produces arrays of integers).

commit16c6aaeb6d47d1f4f4c220b28793cd2b11c7bde8 authorHarry Garrood <harry@garrood.me> dateWed 4 Jan 2017, 22:05 (10 years ago) parent086bbe6
1 file changed+9−1
src/Language/PureScript/PSString.hs+9−1
src/Language/PureScript/PSString.hs+9−1
@@ -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.