forked from charleso/introduction-to-fp-in-scala
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJsonParser.scala
176 lines (163 loc) · 4.43 KB
/
JsonParser.scala
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
package challenge
import intro._
import challenge.MoreParser._
sealed trait Json
case class JsonString(value: String) extends Json
case class JsonNumber(value: Double) extends Json
case class JsonObject(value: List[(String, Json)]) extends Json
case class JsonArray(value: List[Json]) extends Json
case class JsonBoolean(value: Boolean) extends Json
case object JsonNull extends Json
sealed trait SpecialCharacter {
def toChar: Char =
this match {
case BackSpace =>
'\b'
case FormFeed =>
'\f'
case NewLine =>
'\n'
case CarriageReturn =>
'\r'
case Tab =>
'\t'
case VerticalTab =>
'\u000b'
case SingleQuote =>
'\''
case DoubleQuote =>
'"'
case Backslash =>
'\\'
}
}
case object BackSpace extends SpecialCharacter
case object FormFeed extends SpecialCharacter
case object NewLine extends SpecialCharacter
case object CarriageReturn extends SpecialCharacter
case object Tab extends SpecialCharacter
case object VerticalTab extends SpecialCharacter
case object SingleQuote extends SpecialCharacter
case object DoubleQuote extends SpecialCharacter
case object Backslash extends SpecialCharacter
object JsonParser {
def toSpecialChar(c: Char): Optional[SpecialCharacter] =
c match {
case 'b' =>
Full(BackSpace)
case 'f' =>
Full(FormFeed)
case 'n' =>
Full(NewLine)
case 'r' =>
Full(CarriageReturn)
case 't' =>
Full(Tab)
case 'v' =>
Full(VerticalTab)
case '\'' =>
Full(SingleQuote)
case '"' =>
Full(DoubleQuote)
case '\\' =>
Full(Backslash)
case _ =>
Empty()
}
/**
* Parse a JSON string. Handle double-quotes, special characters, hexadecimal characters.
*
* See http://json.org for the full list of control characters in JSON.
* Feel free to keep it simple * to start with to pass the basic tests.
*
* scala> JsonParser.stringParser.parseAll("\"abc\"")
* = Ok(abc)
*
* scala> JsonParser.stringParser.parseAll("\" abc\"")
* = Ok( abc)
*/
def stringParser: Parser[String] =
???
/**
* Parse a JSON rational number.
*
* Feel free to start by parsing natural numbers (int), and move on to the rest of the challenge.
*
* scala> JsonParser.numberParser.parseAll("123")
* = Ok(123)
*
* scala> JsonParser.numberParser.parseAll("123.4")
* = Ok(123.4)
*
* scala> JsonParser.numberParser.parseAll("-123")
* = Ok(-123)
*/
def numberParser: Parser[Double] =
???
/**
* Parse a JSON boolean value.
*
* Tip: Use `string` and `|||`
*
* scala> JsonParser.booleanParser.parseAll("true")
* = Ok(true)
*
* scala> JsonParser.booleanParser.parseAll("false")
* = Ok(false)
*/
def booleanParser: Parser[Boolean] =
???
/**
* Parse a JSON `null` literal.
*
* Tip: Use `string`
*
* scala> JsonParser.nullParser.parseAll("null")
* = Ok(())
*/
def nullParser: Parser[Unit] =
???
/**
* Parse a JSON array.
*
* Tip: Use `sepBy` and `jsonParser`
*
* scala> JsonParser.arrayParser.parseAll("""["1", 2, true]""")
* = Ok(List(JsonString(1),JsonNumber(2),JsonBoolean(true)))
*
* scala> JsonParser.arrayParser.parseAll("[]")
* = Ok(List())
*/
def arrayParser: Parser[List[Json]] =
???
/**
* Parse a JSON object.
*
* Tip: Use `jsonString`, `sepBy` and `jsonParser`
*
* scala> JsonParser.objectParser.parseAll("""{"a":"x","b":2,"c":true}""")
* = Ok(List((a,JsonString("x")),(b,JsonNumber(2)),(c,JsonBoolean(true))))
*
* scala> JsonParser.objectParser.parseAll("")
* = Ok(List())
*
*/
def objectParser: Parser[List[(String, Json)]] =
???
/**
* Parse a JSON value.
*
* Tip: Use the other parsers and `|||`
*
* scala> JsonParser.jsonParser.parseAll("""{"key1":true}""")
* = Ok(JsonObject(List((key1,JsonBoolean(true))))
*
* scala> JsonParser.jsonParser.parseAll("""{"key1":true,"key2":[7,false]}""")
* = Ok(JsonObject(List((key1,JsonBoolean(true)),(key2,JsonArray(List(JsonNumber(7), JsonBoolean(false)))))))
*
* scala> JsonParser.jsonParser.parseAll("""{"key1":true,"key2":[7,false],"key3":{"key4":null}}""")
* = Ok(JsonObject(List((key1,JsonBoolean(true)),(key2,JsonArray(List(JsonNumber(7), JsonBoolean(false)))),(key3,JsonObject(List((key4,JsonNull))))
*/
def jsonParser: Parser[Json] =
???
}