2

次のような構造を解析しようとして苦労しています

{"tree": [5, [[1, 4], [2, 3]]]}

二分木に

data Tree a = Leaf a | Node (Tree a) (Tree a)

しかし、タイプを正しく取得することさえできないようです。この問題の簡単な解決策はありますか?

各 JSON 配列には正確に 2 つの要素が含まれていると想定しているため、結果は次のようになります。

Node (Leaf 5) (Node (Node (Leaf 1) (Leaf 4)) (Node (Leaf 2) (Leaf 3))) :: Tree Int

編集:私が試したこと:

インポート用の新しいデータ型を追加してみました

data IntTree = IntTree { jsonTree :: Tree Int }

instance FromJSON IntTree
  where
    parseJSON (Object v) = do
        inttree <- (v .: "tree")
        -- now I am stuck
4

1 に答える 1