1 import "StandardLibrary"
3 import "JavaBuiltin" as Java
5 importJava "com.fasterxml.jackson.core.JsonGenerator" where
9 importJava "com.fasterxml.jackson.core.JsonGenerator" where
10 writeNull :: JsonGenerator -> <Proc> ()
12 writeStartArray :: JsonGenerator -> <Proc> ()
13 @JavaName writeStartArray
14 writeStartArrayN :: JsonGenerator -> Integer -> <Proc> ()
15 writeEndArray :: JsonGenerator -> <Proc> ()
17 writeStartObject :: JsonGenerator -> <Proc> ()
18 writeFieldName :: JsonGenerator -> String -> <Proc> ()
19 writeEndObject :: JsonGenerator -> <Proc> ()
21 writeBoolean :: JsonGenerator -> Boolean -> <Proc> ()
23 writeString :: JsonGenerator -> String -> <Proc> ()
26 writeNumberDouble :: JsonGenerator -> Double -> <Proc> ()
28 writeNumberInteger :: JsonGenerator -> Integer -> <Proc> ()
30 writeNumberLong :: JsonGenerator -> Long -> <Proc> ()
32 writeNumberShort :: JsonGenerator -> Short -> <Proc> ()
34 writeNumberFloat :: JsonGenerator -> Float -> <Proc> ()
37 closeGenerator :: JsonGenerator -> <Proc> ()
40 importJava "com.fasterxml.jackson.core.JsonToken" where
42 END_ARRAY :: JsonToken
43 END_OBJECT :: JsonToken
44 FIELD_NAME :: JsonToken
45 NOT_AVAILABLE :: JsonToken
46 START_ARRAY :: JsonToken
47 START_OBJECT :: JsonToken
48 VALUE_EMBEDDED_OBJECT :: JsonToken
49 VALUE_FALSE :: JsonToken
50 VALUE_NULL :: JsonToken
51 VALUE_NUMBER_FLOAT :: JsonToken
52 VALUE_NUMBER_INT :: JsonToken
53 VALUE_STRING :: JsonToken
54 VALUE_TRUE :: JsonToken
56 importJava "com.fasterxml.jackson.core.JsonParser" where
60 importJava "com.fasterxml.jackson.core.JsonParser" where
61 nextToken :: JsonParser -> <Proc> JsonToken
62 currentToken :: JsonParser -> <Proc> JsonToken
63 getDoubleValue :: JsonParser -> <Proc> Double
64 getIntValue :: JsonParser -> <Proc> Integer
65 getText :: JsonParser -> <Proc> String
66 getShortValue :: JsonParser -> <Proc> Short
67 getFloatValue :: JsonParser -> <Proc> Float
68 getLongValue :: JsonParser -> <Proc> Long
69 nextFieldName :: JsonParser -> <Proc> Maybe String
72 importJava "com.fasterxml.jackson.core.JsonFactory" where
76 createJsonFactory :: <Proc> JsonFactory
78 @JavaName createGenerator
79 createWriterGenerator :: JsonFactory -> Writer -> <Proc> JsonGenerator
81 @JavaName createParser
82 createStringParser :: JsonFactory -> String -> <Proc> JsonParser
85 defaultFactory = createJsonFactory
89 assertStartArray :: JsonParser -> <Proc> ()
90 assertStartArray p = if currentToken p == START_ARRAY
92 else fail "Expected START_ARRAY token."
96 assertEndArray :: JsonParser -> <Proc> ()
97 assertEndArray p = if nextToken p == END_ARRAY
99 else fail "Expected END_ARRAY token."
101 // *** Json type class ********************************************************
104 writeJson :: JsonGenerator -> a -> <Proc> ()
105 readJson :: JsonParser -> <Proc> a
107 fromJson :: Json -> a
109 writeJson g v = writeJson g (toJson v)
110 readJson p = fromJson (readJson p)
113 readNextJson :: Json a => JsonParser -> <Proc> a
119 Converts the value to a string encoded with JSON
121 toJsonString :: Json a => a -> String
122 toJsonString v = runProc do
123 writer = createStringWriter
124 generator = createWriterGenerator defaultFactory (toWriter writer)
125 writeJson generator v
126 closeGenerator generator
127 resultOfStringWriter writer
130 Parses a JSON encoded string into a value
132 fromJsonString :: Json a => String -> a
133 fromJsonString str = runProc do
134 parser = createStringParser defaultFactory str
137 instance Json String where
138 writeJson = writeString
141 fromJson (JsonString value) = value
143 instance Json Boolean where
144 writeJson = writeBoolean
146 if currentToken p == VALUE_TRUE
150 fromJson (JsonBoolean value) = value
152 instance Json Double where
153 writeJson = writeNumberDouble
154 readJson = getDoubleValue
156 fromJson (JsonDouble value) = value
158 instance Json Float where
159 writeJson = writeNumberFloat
160 readJson = getFloatValue
161 toJson = JsonDouble . toDouble
162 fromJson (JsonDouble value) = fromDouble value
164 instance Json Integer where
165 writeJson = writeNumberInteger
166 readJson = getIntValue
167 toJson = JsonLong . fromInteger
168 fromJson (JsonLong value) = Java.l2i value
170 instance Json Long where
171 writeJson = writeNumberLong
172 readJson = getLongValue
174 fromJson (JsonLong value) = value
176 instance Json Short where
177 writeJson = writeNumberShort
178 readJson = getShortValue
179 toJson = JsonLong . Java.i2l . Java.s2i
180 fromJson (JsonLong value) = Java.i2s (Java.l2i value)
182 instance (Json a) => Json (Maybe a) where
183 writeJson g (Just v) = writeJson g v
184 writeJson g Nothing = writeNull g
186 if currentToken p == VALUE_NULL
188 else Just (readJson p)
189 toJson (Just value) = toJson value
190 toJson Nothing = JsonNull
191 fromJson JsonNull = Nothing
192 fromJson json = Just (fromJson json)
194 instance (Json a) => Json [a] where
199 readJson p = MList.freeze result
201 result = MList.create ()
203 while (nextToken p != END_ARRAY)
204 (MList.add result $ readJson p)
205 toJson l = JsonArray (map toJson l)
206 fromJson (JsonArray l) = map fromJson l
208 instance Json () where
216 toJson _ = JsonArray []
217 fromJson (JsonArray []) = ()
219 instance (Json a, Json b) => Json (a, b) where
220 writeJson g (a, b) = do
231 toJson (a, b) = JsonArray [toJson a, toJson b]
232 fromJson (JsonArray [a, b]) = (fromJson a, fromJson b)
234 instance (Json a, Json b, Json c) => Json (a, b, c) where
235 writeJson g (a, b, c) = do
241 readJson p = (a, b, c)
248 toJson (a, b, c) = JsonArray [toJson a, toJson b, toJson c]
249 fromJson (JsonArray [a, b, c]) = (fromJson a, fromJson b, fromJson c)
251 instance (Json a, Json b, Json c, Json d) => Json (a, b, c, d) where
252 writeJson g (a, b, c, d) = do
259 readJson p = (a, b, c, d)
267 toJson (a, b, c, d) = JsonArray [toJson a, toJson b, toJson c, toJson d]
268 fromJson (JsonArray [a, b, c, d]) = (fromJson a, fromJson b, fromJson c, fromJson d)
270 instance (Json a, Json b, Json c, Json d, Json e) => Json (a, b, c, d, e) where
271 writeJson g (a, b, c, d, e) = do
279 readJson p = (a, b, c, d, e)
288 toJson (a, b, c, d, e) = JsonArray [toJson a, toJson b, toJson c, toJson d, toJson e]
289 fromJson (JsonArray [a, b, c, d, e]) = (fromJson a, fromJson b, fromJson c, fromJson d, fromJson e)
296 | JsonBoolean Boolean
298 | JsonObject [JsonField]
299 data JsonField = JsonField String Json
301 deriving instance Show Json
302 deriving instance Show JsonField
304 instance Json Json where
305 writeJson g (JsonString value) = writeString g value
306 writeJson g (JsonDouble value) = writeNumberDouble g value
307 writeJson g (JsonLong value) = writeNumberLong g value
308 writeJson g (JsonBoolean value) = writeBoolean g value
309 writeJson g JsonNull = writeNull g
310 writeJson g (JsonArray values) = do
312 iter (writeJson g) values
314 writeJson g (JsonObject fields) = do
316 iter (\(JsonField name value) -> do
317 writeFieldName g name
318 writeJson g value) fields
322 token = currentToken p
323 if token == VALUE_STRING
324 then JsonString (getText p)
325 else if token == VALUE_NUMBER_FLOAT
326 then JsonDouble (getDoubleValue p)
327 else if token == VALUE_NUMBER_INT
328 then JsonLong (getLongValue p)
329 else if token == VALUE_TRUE
330 then JsonBoolean True
331 else if token == VALUE_FALSE
332 then JsonBoolean False
333 else if token == VALUE_NULL
335 else if token == START_ARRAY
337 result = MList.create ()
338 while (nextToken p != END_ARRAY)
339 (MList.add result $ readJson p)
340 JsonArray (MList.freeze result)
341 else if token == START_OBJECT
343 result = MList.create ()
344 readJsonObjectContents result p
345 JsonObject (MList.freeze result)
346 else fail "Unsupported token type."
351 readJsonObjectContents :: MList.T JsonField -> JsonParser -> <Proc> ()
352 readJsonObjectContents result p =
353 match nextFieldName p with
355 MList.add result $ JsonField name (readNextJson p)
356 readJsonObjectContents result p
361 makeTypeEqual :: a -> a -> ()
362 makeTypeEqual _ _ = ()
365 testValue :: Json a => Show a => a -> <Proc> ()
368 v3 = fromJsonString v2
370 print "\(v1) -> \(v2) -> \(v3)"
372 then fail "Values differ"
375 testGenericJson :: String -> <Proc> ()
376 testGenericJson v1 = do
377 v2 = fromJsonString v1 :: Json
379 print "\(v1) -> \(v2) -> \(v3)"
381 then fail "Values differ"
389 testValue (123 :: Short)
390 testValue (123 :: Integer)
391 testValue (123 :: Long)
392 testValue (123 :: Double)
393 testValue (123 :: Float)
394 testValue (Nothing :: Maybe String)
395 testValue (Just "asd")
396 testValue ["a", "b", "c"]
397 testValue [[],["a"],["b","c"]]
400 testValue ("a", "b", "c")
401 testValue ("a", "b", "c", "d")
402 testValue [Just "a", Nothing]
403 testValue [("a", "b"), ("c", "d")]
404 testValue (("a", "b"), ("c", "d"))
406 testGenericJson "\"asd\""
407 testGenericJson "123"
408 testGenericJson "123.0"
409 testGenericJson "true"
410 testGenericJson "false"
411 testGenericJson "null"
412 testGenericJson "[1,2,3]"
413 testGenericJson "[[1],[2,3],[]]"
415 testGenericJson "{\"a\":123,\"b\":[]}"
416 testGenericJson "{\"a\":{}}"