forked from fsprojects/FSharp.Json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Enum.fs
85 lines (71 loc) · 2.71 KB
/
Enum.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
namespace FSharp.Json
module Enum =
open NUnit.Framework
type NumberEnum =
| One = 1
| Two = 2
| Three = 3
type TheNumberEnum = {
value: NumberEnum
}
[<Test>]
let ``Enum serialization/deserialization`` () =
let expected = { TheNumberEnum.value = NumberEnum.Three }
let json = Json.serialize(expected)
let actual = Json.deserialize<TheNumberEnum> json
Assert.AreEqual(expected, actual)
[<Test>]
let ``Enum deserialization from string`` () =
let expected = { TheNumberEnum.value = NumberEnum.One }
let json = """{"value":"One"}"""
let actual = Json.deserialize<TheNumberEnum> json
Assert.AreEqual(expected, actual)
[<Test>]
let ``Enum serialization to string`` () =
let value = { TheNumberEnum.value = NumberEnum.Two }
let actual = Json.serializeU value
let expected = """{"value":"Two"}"""
Assert.AreEqual(expected, actual)
[<Test>]
let ``Enum serialization - config setting`` () =
let value = { TheNumberEnum.value = NumberEnum.Two }
let config = JsonConfig.create(unformatted = true, enumValue = EnumMode.Value)
let actual = Json.serializeEx config value
let expected = """{"value":2}"""
Assert.AreEqual(expected, actual)
type LetterEnum =
| LetterA = 'a'
| LetterB = 'b'
| LetterC = 'c'
type TheAttributedLetterEnum = {
[<JsonField(EnumValue = EnumMode.Value)>]
value: LetterEnum
}
[<Test>]
let ``Letter Enum value deserialization`` () =
let expected = { TheAttributedLetterEnum.value = LetterEnum.LetterB }
let json = """{"value":"b"}"""
let actual = Json.deserialize<TheAttributedLetterEnum> json
Assert.AreEqual(expected, actual)
[<Test>]
let ``Letter Enum value serialization`` () =
let value = { TheAttributedLetterEnum.value = LetterEnum.LetterC }
let actual = Json.serializeU value
let expected = """{"value":"c"}"""
Assert.AreEqual(expected, actual)
type TheAttributedNumberEnum = {
[<JsonField(EnumValue = EnumMode.Value)>]
value: NumberEnum
}
[<Test>]
let ``Number Enum value deserialization`` () =
let expected = { TheAttributedNumberEnum.value = NumberEnum.Two }
let json = """{"value":2}"""
let actual = Json.deserialize<TheAttributedNumberEnum> json
Assert.AreEqual(expected, actual)
[<Test>]
let ``Number Enum value serialization`` () =
let value = { TheAttributedNumberEnum.value = NumberEnum.Three }
let actual = Json.serializeU value
let expected = """{"value":3}"""
Assert.AreEqual(expected, actual)