diff options
author | lurchi <lurchi@strangeplace.net> | 2020-06-19 19:21:52 +0200 |
---|---|---|
committer | lurchi <lurchi@strangeplace.net> | 2020-06-19 19:21:52 +0200 |
commit | 66481b77acfa990f53fe94f6268e69c8db9fff6c (patch) | |
tree | 9b2bd735a8c571021c814d23f1e8d9f83830f87d | |
parent | e8e27aa89bb6794be5260c86bd5c79588ca131af (diff) | |
download | groupchat-66481b77acfa990f53fe94f6268e69c8db9fff6c.tar.gz groupchat-66481b77acfa990f53fe94f6268e69c8db9fff6c.zip |
json unmarshalling does not work anymore for object variants
-rw-r--r-- | src/message.nim | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/src/message.nim b/src/message.nim index c16334d..939ba5e 100644 --- a/src/message.nim +++ b/src/message.nim | |||
@@ -2,6 +2,7 @@ | |||
2 | 2 | ||
3 | import json | 3 | import json |
4 | import strutils | 4 | import strutils |
5 | import sequtils | ||
5 | import options | 6 | import options |
6 | 7 | ||
7 | type | 8 | type |
@@ -38,6 +39,25 @@ proc `$`*(message: Message): string = | |||
38 | 39 | ||
39 | proc parse*(input: string): Option[Message] = | 40 | proc parse*(input: string): Option[Message] = |
40 | try: | 41 | try: |
41 | some(parseJson(input).to(Message)) | 42 | let node = parseJson(input) |
43 | var msg: Message | ||
44 | case node["kind"].to(string) | ||
45 | of "talk": | ||
46 | msg = Message(kind: Talk, sender: node["sender"].to(string), | ||
47 | content: node["content"].to(string), | ||
48 | timestamp: node["timestamp"].to(int64)) | ||
49 | of "join": | ||
50 | msg = Message(kind: Join, who: node["who"].to(string), | ||
51 | timestamp: node["timestamp"].to(int64)) | ||
52 | of "leave": | ||
53 | msg = Message(kind: Leave, who: node["who"].to(string), | ||
54 | timestamp: node["timestamp"].to(int64)) | ||
55 | of "info": | ||
56 | msg = Message(kind: Info, | ||
57 | participants: node["participants"].to(seq[string]), | ||
58 | timestamp: node["timestamp"].to(int64)) | ||
59 | else: | ||
60 | raise newException(KeyError, "invalid message kind") | ||
61 | some(msg) | ||
42 | except JsonParsingError, KeyError, JsonKindError: | 62 | except JsonParsingError, KeyError, JsonKindError: |
43 | none(Message) | 63 | none(Message) |