2014-11-02 55 views
5

我是Haskell的新手,並且正在測試JSON序列化。這裏是測試案例的樣子:如何在Haskell中創建更可讀的多行字符串?

{-# LANGUAGE OverloadedStrings #-} 

module WetlandsTest where 

import Control.Exception (evaluate) 
import Test.Hspec 
import Wetlands 

main :: IO() 
main = hspec $ do 
    describe "wetlands" $ do 
    describe "spotting" $ do 
     it "returns a json encoded spotting" $ do 
     let record = spotting "Snowy Egret" "California" "low tide" 
     record `shouldBe` "{\"bird\":\"Snowy Eget\",\"state\":\"California\",\"meta\":\"low tide\"}" 

有沒有辦法以更可讀的方式編寫?也許沿着這樣的路線:

record `shouldBe` """ 
{"bird":"Snowy Eget","city":"California","meta":"low tide"} 
""" 

這不一定是多行字符串,但如果你美化JSON那麼它會。只是想知道一般情況。

+1

這是毫無關係的具體問題,但所有這些'do's是不必要的,因爲它的立場,所以你可以通過消除它來清理它。 – 2014-11-02 22:47:54

回答

9

只需使用準報價延伸和string-qq包:

{-# LANGUAGE QuasiQuotes #-} 
import Data.String.QQ 

someString :: String 
someString = [s| 
This is" 
some string with "" quotes and stuff"! 
|] 

隨着輸出:

*Main> someString 
"This is\"\nsome string with \"\" quotes and stuff\"!\n" 
相關問題