惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
B
Blog RSS Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
MyScale Blog
MyScale Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
SegmentFault 最新的问题
Y
Y Combinator Blog
月光博客
月光博客
IT之家
IT之家
T
Tailwind CSS Blog
Last Week in AI
Last Week in AI
L
LangChain Blog
博客园_首页
MongoDB | Blog
MongoDB | Blog
P
Proofpoint News Feed
博客园 - Franky
WordPress大学
WordPress大学
云风的 BLOG
云风的 BLOG
M
MIT News - Artificial intelligence
V
Visual Studio Blog
小众软件
小众软件
博客园 - 叶小钗
博客园 - 三生石上(FineUI控件)
N
Netflix TechBlog - Medium

Jiajun的技术笔记

你好,2026! TiDB 源码阅读(六):TiDB Coprocessor 源码解析 性能优化的核心思想 TiDB 源码阅读(五):索引 TiDB 源码阅读(四):AST、逻辑计划、物理计划 CockroachDB Serverless Architecture podman 无故退出 Cursor Control-L (CTRL-L) Keyboard Shortcuts in Terminal Replace docker with podman Using xmonad with xfce4 A RC script for freebsd frpc 自己动手写一个k8s controller AI 会取代你的(编程)岗位吗? 自建DERP服务器提升Tailscale连接速度(使用Nginx转发) 自动升级Docker容器 再读《程序员修炼之道-从小工到专家》 让浏览器下载文件 再读《软件随想录》/《黑客与画家》/《软技能》 HTTP 压力测试中的 Coordinated Omission 2的补码 编程语言中的 context 是什么? flutter macOS 构建出错 Flatpak 使用小记 Golang CAS 操作是怎么实现的 PostgreSQL 当MQ来使用 Clash 结合 工作VPN 的网络设计 使用 PostgreSQL 搭建 JuiceFS PostgreSQL 配置优化和日志分析 有GitHub Copilot?那就可以搭建你的ChatGPT4服务 窗口函数的使用(以PG为例)
Haskell简明教程(五):处理JSON
Jiajun Huang · 2017-09-26 · via Jiajun的技术笔记

这一系列是我学习 Learn You a Haskell For Great Good 之后,总结,编写的学习笔记。

这个系列主要分为五个部分:

本文有参考 《Real World Haskell》

Haskell如何表示JSON

json 是一种数据表现形式,来源于 javascript。JSON中有四种基本的值的类型:

  • number
  • boolean: true/false
  • string
  • null

此外有两种容器类型:

  • array
  • object

那么Haskell我们需要如何表示成JSON呢?我们需要一个 value constructor 加上Haskell自带的类型来表示:

data JValue = JNumber Integer
          | JString String
          | JBoolean Bool
          | JNull
          | JArray [JValue]
          | JObject [(String, JValue)]
            deriving (Show, Ord, Eq)

那么我们要如何输出JSON呢?其类型肯定是这样的:

echoJValue :: JValue -> String

我们分别为 string, bool, null, number 定义好如何输出成JSON:

echoJValue (JNumber i) = show i
echoJValue (JString s) = show s
echoJValue (JBoolean True) = "true"
echoJValue (JBoolean False) = "false"
echoJValue JNull = "null"

array该怎么处理?输出 “[” 之后输出array中的内容,然后输出 “]“,所以是:

echoJValue (JArray a) = "[" ++ handleArray a ++ "]"
    where handleArray [] = ""
          handleArray a = intercalate ", " (map echoJValue a)

而object则是:

echoJValue (JObject a) = "{" ++ handleObject a ++ "}"
    where handleObject [] = ""
          handleObject a = intercalate ", " (map handleKV a)
          handleKV (k,v) = k ++ ": " ++ echoJValue v

此处我们需要导入 Data.List 中的 intercalate 函数:

Prelude> :m Data.List
Prelude Data.List> :t intercalate 
intercalate :: [a] -> [[a]] -> [a]
Prelude Data.List> intercalate ", " ["hello", "world"]
"hello, world"

于是我们便可以输出自定义的JSON了。

Prelude> :l JSON
[1 of 1] Compiling Main             ( JSON.hs, interpreted )
Ok, 1 module loaded.
*Main> JNull
JNull
*Main> JNumber 1
JNumber 1
*Main> JString "hello"
JString "hello"
*Main> JBoolean True
JBoolean True
*Main> JBoolean False
JBoolean False
*Main> JArray [JBoolean True, JBoolean False]
JArray [JBoolean True,JBoolean False]
*Main> echoJValue $ JArray [JBoolean True, JBoolean False]
"[true, false]"
*Main> echoJValue $ JObject [("hello", JNumber 1), ("world", JString "world")]
"{hello: 1, world: \"world\"}"

我们可以把整个文件做成一个Module:

module JSON (
    JValue(..),
    echoJValue
    ) where 

import Data.List (intercalate)


data JValue = JNumber Integer
          | JString String
          | JBoolean Bool
          | JNull
          | JArray [JValue]
          | JObject [(String, JValue)]
            deriving (Show, Ord, Eq)


echoJValue :: JValue -> String
echoJValue (JNumber i) = show i
echoJValue (JString s) = show s
echoJValue (JBoolean True) = "true"
echoJValue (JBoolean False) = "false"
echoJValue JNull = "null"
echoJValue (JArray a) = "[" ++ handleArray a ++ "]"
    where handleArray [] = ""
          handleArray a = intercalate ", " (map echoJValue a)
echoJValue (JObject a) = "{" ++ handleObject a ++ "}"
    where handleObject [] = ""
          handleObject a = intercalate ", " (map handleKV a)
          handleKV (k,v) = k ++ ": " ++ echoJValue v

那我们该要怎样 pretty print呢?这就留作思考吧 :) (提示:Haskell的世界,递归总是那么重要)

总结

到此Haskell教程便结束了,我们从最开始的命令式语言如何抽象,到介绍Haskell的基本语法和要素, TypeClass, Functor, Applicative, Monoid, Monad,到随后我们封装一个简单的JSON模块。简略的 浏览了一下Haskell的面貌,但是Haskell抽象程度很高,为此付出的代价便是不那么容易一眼就看出来, 有时候甚至需要来回看,来回琢磨才能领会其中的意义。

之后我将会写一些散篇,主要是Haskell和现实世界的应用,或者是Haskell的源代码分析。

参考资料: