-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExamples.fs
179 lines (147 loc) · 5.94 KB
/
Examples.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
namespace FSharp.Exchange.Tests
module JsonData =
open NUnit.Framework
open FSharp.Data
open FSharp.Data.JsonExtensions
type Person = { Name: string; Born: int }
[<Test>]
let ``Record deserialization - optional member`` () =
let data = JsonValue.Parse(""" { "name": "Tomas", "born": 1956 } """)
let person = {
Person.Name = data?name.AsString()
Born = data?born.AsInteger()
}
Assert.AreEqual(person, { Person.Name = "Tomas"; Born = 1956 })
module JsonDataOptional =
open NUnit.Framework
open FSharp.Data
open FSharp.Data.JsonExtensions
type Person = { Name: string; Born: int; MiddleName: string option }
[<Test>]
let ``Record deserialization - optional member`` () =
let data = JsonValue.Parse(""" { "name": "Teresa", "born": 1956 } """)
let person = {
Person.Name = data?name.AsString()
Born = data?born.AsInteger()
MiddleName =
match data.TryGetProperty("middle_name") with
| None -> None
| Some JsonValue.Null -> None
| Some value -> Some (value.AsString())
}
Assert.AreEqual({ Person.Name = "Teresa"; Born = 1956; MiddleName = None }, person)
module JsonProvider =
open NUnit.Framework
open FSharp.Data
type Person = JsonProvider<""" { "name":"John", "age":94 } """>
[<Test>]
let ``Record deserialization`` () =
let person = Person.Parse(""" { "name":"Tomas", "age":4 } """)
Assert.AreEqual("Tomas", person.Name)
Assert.AreEqual(4, person.Age)
module FSharpLuJsonNullSafety =
open NUnit.Framework
open Microsoft.FSharpLu.Json
type Person = { Name: string; Born: int; MiddleName: string option }
[<Test>]
let ``Record deserialization - nulls`` () =
let json1 = """ { "Name": "Teresa", "Born": 1956 } """
let person1 = Compact.deserialize<Person> json1
Assert.AreEqual({ Person.Name = "Teresa"; Born = 1956; MiddleName = None }, person1)
let json2 = """ { "Name": null, "Born": 1956 } """
let person2 = Compact.deserialize<Person> json2
Assert.AreEqual({ Person.Name = null; Born = 1956; MiddleName = None }, person2)
module FSharpLuJsonSingleCaseUnion =
open NUnit.Framework
open Microsoft.FSharpLu.Json
type Address = Address of string
type Person = { Name: string; Address: Address }
[<Test>]
let ``Record deserialization - single case union`` () =
let person = { Name = "Teresa"; Address = Address "10 Downing St" }
let json = Compact.serialize person
let expected = """{
"Name": "Teresa",
"Address": {
"Address": "10 Downing St"
}
}"""
Assert.AreEqual(expected, json)
module ThothJson =
open NUnit.Framework
open Thoth.Json.Net
type Person = {
Name: string
Born: int
MiddleName: string option
}
[<Test>]
let ``Record deserialization - optional member`` () =
let json = """ { "name": "Teresa", "born": 1956 } """
let person = Decode.Auto.unsafeFromString<Person>(json, isCamelCase=true)
Assert.AreEqual({ Person.Name = "Teresa"; Born = 1956; MiddleName = None }, person)
module ThothJsonSingleCaseUnion =
open NUnit.Framework
open Thoth.Json.Net
open Microsoft.FSharp.Core
type Address = Value of string
type Person = {
Name: string
Born: int
MiddleName: string option
Address: Address
}
[<Test>]
let ``Record deserialization - non optional member`` () =
let person = { Person.Name = "Teresa"; Born = 1956; MiddleName = None; Address = Address.Value "10 Downing St" }
let json = Encode.Auto.toString(0, person, isCamelCase=true)
let expected = """{"name":"Teresa","born":1956,"address":["Value","10 Downing St"]}"""
Assert.AreEqual(expected, json)
module FSharpJson =
open NUnit.Framework
open FSharp.Json
type Person = { Name: string; Born: int }
[<Test>]
let ``Deserialization + Serialization`` () =
let expectedJson = """{"Name":"Teresa","Born":1956}"""
let config = JsonConfig.create(unformatted = true)
let person = Json.deserializeEx<Person> config expectedJson
Assert.AreEqual({ Person.Name = "Teresa"; Born = 1956 }, person)
let json = Json.serializeEx config person
Assert.AreEqual(expectedJson, json)
module FSharpJsonNullSafety =
open NUnit.Framework
open FSharp.Json
type Person = { Name: string; MiddleName: string option }
[<Test>]
let ``Null safety examples`` () =
let json1 = """{"Name":"Teresa","MiddleName":null}"""
let person1 = Json.deserialize<Person> json1
Assert.AreEqual({ Person.Name = "Teresa"; MiddleName = None }, person1)
let json2 = """{"Name":"Teresa","MiddleName":"Mary"}"""
let person2 = Json.deserialize<Person> json2
Assert.AreEqual({ Person.Name = "Teresa"; MiddleName = Some "Mary" }, person2)
let json3 = """{"Name":null,"MiddleName":null}"""
Assert.Throws<JsonDeserializationError>(fun () -> Json.deserialize<Person> json3 |> ignore) |> ignore
//module Chiron =
// open NUnit.Framework
// open Chiron
// open Chiron.Operators
//
// type Person = { Name: string; Born: int; MiddleName: string option }
//
// module D = Json.Decode
// module E = Json.Encode
//
// module Person =
// let make name born middleName = { Name = name; Born = born; MiddleName = middleName }
// let encode x jObj =
// jObj
// |> E.required E.string "name" x.Name
// |> E.required E.int "born" x.Born
// |> E.required (E.optionWith E.string) "middle_name" x.MiddleName
// let decode =
// make
// <*> D.required D.string "name"
// <*> D.required D.int "born"
// <!> D.required (D.optionWith D.string) "middle_name"