OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library pirate.messages; | 5 library pirate.messages; |
6 | 6 |
7 import 'dart:convert' show JSON; | 7 // This class is used to send data back and forth between the client and server. |
8 | 8 // It is automatically serialized and deserialized by the RPC package. |
9 class Pirate { | 9 class Pirate { |
10 String name; | 10 String name; |
11 String appellation; | 11 String appellation; |
12 | 12 |
13 // A message class must have a default constructor taking no arguments. | 13 // A message class must have a default constructor taking no arguments. |
14 Pirate(); | 14 Pirate(); |
15 | 15 |
16 // It is fine to have other named constructors. | 16 // It is fine to have other named constructors. |
17 Pirate.fromJSON(String jsonString) { | |
18 Map storedName = JSON.decode(jsonString); | |
19 name = storedName['f']; | |
20 appellation = storedName['a']; | |
21 } | |
22 | |
23 Pirate.fromString(String pirateName) { | 17 Pirate.fromString(String pirateName) { |
24 var parts = pirateName.split(' the '); | 18 var parts = pirateName.split(' the '); |
25 name = parts[0]; | 19 name = parts[0]; |
26 appellation = parts[1]; | 20 appellation = parts[1]; |
27 } | 21 } |
28 | 22 |
29 String get jsonString => JSON.encode({"f": name, "a": appellation}); | |
30 | |
31 String toString() => name.isEmpty ? '' : '$name the $appellation'; | 23 String toString() => name.isEmpty ? '' : '$name the $appellation'; |
32 } | 24 } |
OLD | NEW |