Data.Aeson

安装 aeson

$ cabal install aeson
Installed aeson-1.2.3.0
Prelude> :m +Data.Aeson
Prelude Data.Aeson>

Data.Aeson 是一个处理 JSON 数据的库。

处理 AST

Prelude Data.Aeson> decode "{\"foo\": 123}" :: Maybe Value
Just (Object (fromList [("foo",Number 123.0)]))
Prelude Data.Aeson> decode "{\"foo\": [\"abc\",\"def\"]}" :: Maybe Value
Just (Object (fromList [("foo",Array [String "abc",String "def"])]))
Prelude Data.Aeson> decode "[1,2,3]" :: Maybe [Int]
Just [1,2,3]
Prelude Data.Aeson> decode "{\"foo\":1,\"bar\":2}" :: Maybe (Data.Map.Map String Int)
Just (fromList [("bar",2),("foo",1)])

处理 Person(1)

{-# LANGUAGE DeriveGeneric #-}

import GHC.Generics
import Data.Aeson
import Data.Text data Person = Person {
name :: Text
, age :: Int
} deriving (Generic, Show) data Persons = Persons {
persons :: [Person]
} deriving (Generic, Show) instance ToJSON Person
instance FromJSON Person instance ToJSON Persons
instance FromJSON Persons
*Main> :set -XOverloadedStrings
*Main> encode (Person {name = "Joe", age = 12})
"{\"name\":\"Joe\",\"age\":12}"
*Main> decode "{\"name\":\"Joe\",\"age\":12}" :: Maybe Person
Just (Person {name = "Joe", age = 12})
*Main> encode (Persons {persons = [Person {name = "Joe", age = 12}]})
"{\"persons\":[{\"name\":\"Joe\",\"age\":12}]}"
*Main> decode "{\"persons\":[{\"name\":\"Joe\",\"age\":12}]}" :: Maybe Persons
Just (Persons {persons = [Person {name = "Joe", age = 12}]})
  • instance ToJSON Persons

    要将 Persons 类型的对象序列化成 JSON 字符串,Persons 类型必须是 ToJSON 类型类的实例
  • instance FromJSON Persons

    要将 JSON 字符串反序列化成 Persons 类型的对象,Persons 类型必须是 FromJSON 类型类的实例

处理 Person(2)

{-# LANGUAGE OverloadedStrings #-}

import Data.Aeson
import Data.Text data Person = Person {
name :: Text
, age :: Int
} deriving Show data Persons = Persons {
persons :: [Person]
} deriving Show instance FromJSON Person where
parseJSON = withObject "Person" $ \v -> Person
<$> v .: "name"
<*> v .: "age" instance ToJSON Person where
toJSON (Person name age) =
object ["name" .= name, "age" .= age] instance FromJSON Persons where
parseJSON = withObject "Persons" $ \v -> Persons
<$> v .: "persons" instance ToJSON Persons where
toJSON (Persons persons) =
object ["persons" .= persons]
*Main> :set -XOverloadedStrings
*Main> encode (Person {name = "Joe", age = 12})
"{\"name\":\"Joe\",\"age\":12}"
*Main> decode "{\"name\":\"Joe\",\"age\":12}" :: Maybe Person
Just (Person {name = "Joe", age = 12})
*Main> encode (Persons {persons = [Person {name = "Joe", age = 12}]})
"{\"persons\":[{\"name\":\"Joe\",\"age\":12}]}"
*Main> decode "{\"persons\":[{\"name\":\"Joe\",\"age\":12}]}" :: Maybe Persons
Just (Persons {persons = [Person {name = "Joe", age = 12}]})

处理 Person(3)

{-# LANGUAGE OverloadedStrings #-}

import Data.Aeson
import Data.Text data Person = Person {
name :: Text
, age :: Int
} deriving Show instance FromJSON Person where
parseJSON = withObject "Person" $ \v -> Person
<$> v .: "name"
<*> v .:? "age" .!= 20
*Main> :set -XOverloadedStrings
*Main> decode "{\"name\":\"Joe\"}" :: Maybe Person
Just (Person {name = "Joe", age = 20})

处理 Person(4)

{-# LANGUAGE DeriveGeneric #-}

import GHC.Generics
import Data.Aeson
import Data.Text (Text) data Person = Person {
field_NAME :: Text
, field_AGE :: Int
} deriving (Generic, Show) customOptions = defaultOptions
{ fieldLabelModifier = drop $ length ("field_" :: String)
} instance ToJSON Person where
toJSON = genericToJSON customOptions
instance FromJSON Person where
parseJSON = genericParseJSON customOptions
*Main> :set -XOverloadedStrings
*Main> encode (Person {field_NAME = "Joe", field_AGE = 12})
"{\"AGE\":12,\"NAME\":\"Joe\"}"
*Main> decode "{\"NAME\":\"Joe\",\"AGE\":12}" :: Maybe Person
Just (Person {field_NAME = "Joe", field_AGE = 12})

Haskell语言学习笔记(66)Aeson的更多相关文章

  1. Haskell语言学习笔记(88)语言扩展(1)

    ExistentialQuantification {-# LANGUAGE ExistentialQuantification #-} 存在类型专用的语言扩展 Haskell语言学习笔记(73)Ex ...

  2. Haskell语言学习笔记(79)lambda演算

    lambda演算 根据维基百科,lambda演算(英语:lambda calculus,λ-calculus)是一套从数学逻辑中发展,以变量绑定和替换的规则,来研究函数如何抽象化定义.函数如何被应用以 ...

  3. Haskell语言学习笔记(69)Yesod

    Yesod Yesod 是一个使用 Haskell 语言的 Web 框架. 安装 Yesod 首先更新 Haskell Platform 到最新版 (Yesod 依赖的库非常多,版本不一致的话很容易安 ...

  4. Haskell语言学习笔记(20)IORef, STRef

    IORef 一个在IO monad中使用变量的类型. 函数 参数 功能 newIORef 值 新建带初值的引用 readIORef 引用 读取引用的值 writeIORef 引用和值 设置引用的值 m ...

  5. Haskell语言学习笔记(39)Category

    Category class Category cat where id :: cat a a (.) :: cat b c -> cat a b -> cat a c instance ...

  6. Haskell语言学习笔记(72)Free Monad

    安装 free 包 $ cabal install free Installed free-5.0.2 Free Monad data Free f a = Pure a | Free (f (Fre ...

  7. Haskell语言学习笔记(44)Lens(2)

    自定义 Lens 和 Isos -- Some of the examples in this chapter require a few GHC extensions: -- TemplateHas ...

  8. Haskell语言学习笔记(38)Lens(1)

    Lens Lens是一个接近语言级别的库,使用它可以方便的读取,设置,修改一个大的数据结构中某一部分的值. view, over, set Prelude> :m +Control.Lens P ...

  9. Haskell语言学习笔记(92)HXT

    HXT The Haskell XML Toolbox (hxt) 是一个解析 XML 的库. $ cabal install hxt Installed hxt-9.3.1.16 Prelude&g ...

随机推荐

  1. 三个php加密解密算法

    三个功能强大的php加密解密函数 //加密函数 function lock_url($txt,$key='www.fyunw.com') { $chars = "ABCDEFGHIJKLMN ...

  2. git: fatal: Could not read from remote repository

    This is probably an Intellij problem. Your key are managed natively by ssh, and Intellij has it's ow ...

  3. gc之六--Minor GC、Major GC、Full GC以及Mixed GC之间的区别

    目录: GC之一--GC 的算法分析.垃圾收集器.内存分配策略介绍 GC之二--GC日志分析(jdk1.8)整理中 GC之三--GC 触发Full GC执行的情况及应对策略 gc之四--Minor G ...

  4. Linux 期中架构 MySQL

     MySQL基础部分 mysql安装脚本 安装前请将相关安装包copy放到/server/tools目录下 -rw-r--r-- 1 root root 314149697 Mar 23  2017 ...

  5. java UTC时间和local时间相互转换

    java UTC时间和local时间相互转换 1.local时间转UTC时间 /** * local时间转换成UTC时间 * @param localTime * @return */ public ...

  6. django从请求到响应的过程深入讲解

    django启动 我们在启动一个django项目的时候,无论你是在命令行执行还是在pycharm直接点击运行,其实都是执行'runserver'的操作,而ruserver是使用django自带的的we ...

  7. diskspd的使用

    参数翻译 可测试目标: file_path 文件abc.file #<physical drive number> #1为第一块物理磁盘[谨慎,别拿系统盘测试,一般用于准备投入的数据磁盘测 ...

  8. Java集合类性能分析

    [转]对于Android开发者来说深入了解Java的集合类很有必要主要是从Collection和Map接口衍生出来的,目前主要提供了List.Set和Map这三大类的集合,今天Android吧(ard ...

  9. Flutter,最好的跨平台开发框架

    今天说说使用flutter的一些体会 对于Flutter,从发现到接触再到使用,不知不觉,已经有大半年了!在这段时间里,谷歌几乎每天都会更新Flutter,有时甚至一天更新几次,这让我对它更加充满信心 ...

  10. oracle创建表空间,表及用户

    oracle要创建表要首先创建表空间,当然默认是有表空间的.而mysql创建表时,会自动创建表空间,myisam会自动建三个文 件.MYD,.MYI,.frm.innodb呢,如果没有配置独立表空间的 ...