git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Restore JSON backwards compatibility for PSStrings
16c6aaeHarry Garrood
src/Language/PureScript/PSString.hs+9−1
@@ -12,6 +12,7 @@ module Language.PureScript.PSString
12
12
13import Prelude.Compat
13import Prelude.Compat
14import Control.Exception (try, evaluate)
14import Control.Exception (try, evaluate)
15+import Control.Applicative ((<|>))
15import Data.Char (chr)
16import Data.Char (chr)
16import Data.Bits (shiftR)
17import Data.Bits (shiftR)
17import Data.List (unfoldr)
18import Data.List (unfoldr)
@@ -119,13 +120,20 @@ instance A.ToJSON PSString where
119 toJSON = A.toJSON . toUTF16CodeUnits
120 toJSON = A.toJSON . toUTF16CodeUnits
120
121
121instance A.FromJSON PSString where
122instance A.FromJSON PSString where
122− parseJSON a = PSString <$> parseArrayOfCodeUnits a
123+ parseJSON a = currentParser <|> backwardsCompat
123 where
124 where
125+ currentParser = PSString <$> parseArrayOfCodeUnits a
126+
124 parseArrayOfCodeUnits :: A.Value -> A.Parser [Word16]
127 parseArrayOfCodeUnits :: A.Value -> A.Parser [Word16]
125 parseArrayOfCodeUnits = A.withArray "array of UTF-16 code units" (traverse parseCodeUnit . V.toList)
128 parseArrayOfCodeUnits = A.withArray "array of UTF-16 code units" (traverse parseCodeUnit . V.toList)
129+
126 parseCodeUnit :: A.Value -> A.Parser Word16
130 parseCodeUnit :: A.Value -> A.Parser Word16
127 parseCodeUnit b = A.withScientific "two-byte non-negative integer" (maybe (A.typeMismatch "" b) return . toBoundedInteger) b
131 parseCodeUnit b = A.withScientific "two-byte non-negative integer" (maybe (A.typeMismatch "" b) return . toBoundedInteger) b
128
132
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+
129-- |
137-- |
130-- Pretty print a PSString, using JavaScript escape sequences. Intended for
138-- Pretty print a PSString, using JavaScript escape sequences. Intended for
131-- use in compiled JS output.
139-- use in compiled JS output.