計算機科学のブログ

Haskell - 実践Haskell - Aesonfを使ったJSONデータの処理 - NOAAデータを読む取る - 独自のList型, ToJSON

入門Haskellプログラミング (Will Kurt(著)、株式会社クイープ(監修、翻訳)、翔泳社)の UNIT 7(実践Haskell)、LESSON 40(Aesonfを使ったJSONデータの処理)、40.6(練習問題)、Q40-2の解答を求めてみる。

軽量マークアップ言語

json-lesson/package.yaml

name:                json-lesson
version:             0.1.0.0
github:              "githubuser/json-lesson"
license:             BSD-3-Clause
author:              "Author name here"
maintainer:          "example@example.com"
copyright:           "2025 Author name here"

extra-source-files:
- README.md
- CHANGELOG.md

# Metadata used when publishing your package
# synopsis:            Short description of your package
# category:            Web

# To avoid duplicated efforts in documentation and dealing with the
# complications of embedding Haddock markup inside cabal files, it is
# common to point users to the README.md file.
description:         Please see the README on GitHub at <https://github.com/githubuser/json-lesson#readme>

dependencies:
- base >= 4.7 && < 5
- aeson
# - bytestring
# - text

default-extensions:
# - OverloadedStrings
- DeriveGeneric

ghc-options:
- -Wall
- -Wcompat
- -Widentities
- -Wincomplete-record-updates
- -Wincomplete-uni-patterns
- -Wmissing-export-lists
- -Wmissing-home-modules
- -Wpartial-fields
- -Wredundant-constraints

library:
  source-dirs: src

executables:
  json-lesson-exe:
    main:                Main.hs
    source-dirs:         app
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    dependencies:
    - json-lesson

tests:
  json-lesson-test:
    main:                Spec.hs
    source-dirs:         test
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    dependencies:
    - json-lesson

コード

json-lesson/app/Main.hs

module Main (main) where

-- import Lib
-- import qualified Data.Text as T
-- import qualified Data.ByteString.Lazy as B
import GHC.Generics
import Data.Aeson

data IntList = Empty | Cons Int IntList deriving (Show, Generic)

instance ToJSON IntList

xs :: [IntList]
xs = [Empty, Cons 1 Empty, Cons 1 (Cons 2 Empty)]


main :: IO ()
main = do
    mapM_ (\x -> do print x
                    print $ encode x)
          xs

入出力結果(Terminal, Zsh)

% stack run
json-lesson-0.1.0.0: unregistering (local file changes: app/Main.hs
json-lesson.cabal package.yaml)
json-lesson> configure (lib + exe)
Configuring json-lesson-0.1.0.0...
json-lesson> build (lib + exe) with ghc-9.8.4
Preprocessing library for json-lesson-0.1.0.0..
Building library for json-lesson-0.1.0.0..
[1 of 2] Compiling Lib
[2 of 2] Compiling Paths_json_lesson
Preprocessing executable 'json-lesson-exe' for json-lesson-0.1.0.0..
Building executable 'json-lesson-exe' for json-lesson-0.1.0.0..
[1 of 2] Compiling Main [Source file changed]
[2 of 2] Compiling Paths_json_lesson [Source file changed]
[3 of 3] Linking .stack-work/dist/aarch64-osx/ghc-9.8.4/build/json-lesson-exe/json-lesson-exe [Objects changed]
ld: warning: -U option is redundant when using -undefined dynamic_lookup
json-lesson> copy/register
Installing library in /Users/kamimura/posts/cs/入門Haskellプログラミング/unit7/lesson40/json-lesson/.stack-work/install/aarch64-osx/27e00dd04a6f7fc061662d4eebb16cc69859af4840b4ab18e626ce23aacafb60/9.8.4/lib/aarch64-osx-ghc-9.8.4/json-lesson-0.1.0.0-JJfj5suk4ST7rzl52JAMwx
Installing executable json-lesson-exe in /Users/kamimura/posts/cs/入門Haskellプログラミング/unit7/lesson40/json-lesson/.stack-work/install/aarch64-osx/27e00dd04a6f7fc061662d4eebb16cc69859af4840b4ab18e626ce23aacafb60/9.8.4/bin
Registering library for json-lesson-0.1.0.0..
Empty
"{\"tag\":\"Empty\"}"
Cons 1 Empty
"{\"contents\":[1,{\"tag\":\"Empty\"}],\"tag\":\"Cons\"}"
Cons 1 (Cons 2 Empty)
"{\"contents\":[1,{\"contents\":[2,{\"tag\":\"Empty\"}],\"tag\":\"Cons\"}],\"tag\":\"Cons\"}"
%