OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 analysis_server.src.protocol.protocol_internal; | 5 library analysis_server.src.protocol.protocol_internal; |
6 | 6 |
7 import 'dart:collection'; | 7 import 'dart:collection'; |
8 import 'dart:convert' hide JsonDecoder; | 8 import 'dart:convert' hide JsonDecoder; |
9 | 9 |
10 import 'package:analysis_server/plugin/protocol/protocol.dart'; | 10 import 'package:analysis_server/plugin/protocol/protocol.dart'; |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 return new RenameOptions.fromJson(jsonDecoder, jsonPath, json); | 215 return new RenameOptions.fromJson(jsonDecoder, jsonPath, json); |
216 } | 216 } |
217 return null; | 217 return null; |
218 } | 218 } |
219 | 219 |
220 /** | 220 /** |
221 * Type of callbacks used to decode parts of JSON objects. [jsonPath] is a | 221 * Type of callbacks used to decode parts of JSON objects. [jsonPath] is a |
222 * string describing the part of the JSON object being decoded, and [value] is | 222 * string describing the part of the JSON object being decoded, and [value] is |
223 * the part to decode. | 223 * the part to decode. |
224 */ | 224 */ |
225 typedef Object JsonDecoderCallback(String jsonPath, Object value); | 225 typedef E JsonDecoderCallback<E>(String jsonPath, Object value); |
226 | 226 |
227 /** | 227 /** |
228 * Instances of the class [HasToJson] implement [toJson] method that returns | 228 * Instances of the class [HasToJson] implement [toJson] method that returns |
229 * a JSON presentation. | 229 * a JSON presentation. |
230 */ | 230 */ |
231 abstract class HasToJson { | 231 abstract class HasToJson { |
232 /** | 232 /** |
233 * Returns a JSON presentation of the object. | 233 * Returns a JSON presentation of the object. |
234 */ | 234 */ |
235 Map<String, Object> toJson(); | 235 Map<String, Object> toJson(); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
296 return json; | 296 return json; |
297 } else if (json is String) { | 297 } else if (json is String) { |
298 return int.parse(json, onError: (String value) { | 298 return int.parse(json, onError: (String value) { |
299 throw mismatch(jsonPath, 'int', json); | 299 throw mismatch(jsonPath, 'int', json); |
300 }); | 300 }); |
301 } | 301 } |
302 throw mismatch(jsonPath, 'int', json); | 302 throw mismatch(jsonPath, 'int', json); |
303 } | 303 } |
304 | 304 |
305 /** | 305 /** |
306 * Decode a JSON object that is expected to be a List. [decoder] is used to | 306 * Decode a JSON object that is expected to be a List. The [decoder] is used |
307 * decode the items in the list. | 307 * to decode the items in the list. |
| 308 * |
| 309 * The type parameter [E] is the expected type of the elements in the list. |
308 */ | 310 */ |
309 List decodeList(String jsonPath, Object json, [JsonDecoderCallback decoder]) { | 311 List/*<E>*/ decodeList/*<E>*/(String jsonPath, Object json, |
| 312 [JsonDecoderCallback/*<E>*/ decoder]) { |
310 if (json == null) { | 313 if (json == null) { |
311 return []; | 314 return/*<E>*/ []; |
312 } else if (json is List) { | 315 } else if (json is List) { |
313 List result = []; | 316 List/*<E>*/ result = /*<E>*/ []; |
314 for (int i = 0; i < json.length; i++) { | 317 for (int i = 0; i < json.length; i++) { |
315 result.add(decoder('$jsonPath[$i]', json[i])); | 318 result.add(decoder('$jsonPath[$i]', json[i])); |
316 } | 319 } |
317 return result; | 320 return result; |
318 } else { | 321 } else { |
319 throw mismatch(jsonPath, 'List', json); | 322 throw mismatch(jsonPath, 'List', json); |
320 } | 323 } |
321 } | 324 } |
322 | 325 |
323 /** | 326 /** |
324 * Decode a JSON object that is expected to be a Map. [keyDecoder] is used | 327 * Decode a JSON object that is expected to be a Map. [keyDecoder] is used |
325 * to decode the keys, and [valueDecoder] is used to decode the values. | 328 * to decode the keys, and [valueDecoder] is used to decode the values. |
326 */ | 329 */ |
327 Map decodeMap(String jsonPath, Object json, | 330 Map/*<K, V>*/ decodeMap/*<K, V>*/(String jsonPath, Object json, |
328 {JsonDecoderCallback keyDecoder, JsonDecoderCallback valueDecoder}) { | 331 {JsonDecoderCallback/*<K>*/ keyDecoder, |
| 332 JsonDecoderCallback/*<V>*/ valueDecoder}) { |
329 if (json == null) { | 333 if (json == null) { |
330 return {}; | 334 return {}; |
331 } else if (json is Map) { | 335 } else if (json is Map) { |
332 Map result = {}; | 336 Map/*<K, V>*/ result = /*<K, V>*/ {}; |
333 json.forEach((String key, value) { | 337 json.forEach((String key, value) { |
334 Object decodedKey; | 338 Object/*=K*/ decodedKey; |
335 if (keyDecoder != null) { | 339 if (keyDecoder != null) { |
336 decodedKey = keyDecoder('$jsonPath.key', key); | 340 decodedKey = keyDecoder('$jsonPath.key', key); |
337 } else { | 341 } else { |
338 decodedKey = key; | 342 decodedKey = key as Object/*=K*/; |
339 } | 343 } |
340 if (valueDecoder != null) { | 344 if (valueDecoder != null) { |
341 value = valueDecoder('$jsonPath[${JSON.encode(key)}]', value); | 345 value = valueDecoder('$jsonPath[${JSON.encode(key)}]', value); |
342 } | 346 } |
343 result[decodedKey] = value; | 347 result[decodedKey] = value as Object/*=V*/; |
344 }); | 348 }); |
345 return result; | 349 return result; |
346 } else { | 350 } else { |
347 throw mismatch(jsonPath, 'Map', json); | 351 throw mismatch(jsonPath, 'Map', json); |
348 } | 352 } |
349 } | 353 } |
350 | 354 |
351 /** | 355 /** |
352 * Decode a JSON object that is expected to be a string. | 356 * Decode a JSON object that is expected to be a string. |
353 */ | 357 */ |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
456 buffer.write(' at '); | 460 buffer.write(' at '); |
457 buffer.write(jsonPath); | 461 buffer.write(jsonPath); |
458 return new Exception(buffer.toString()); | 462 return new Exception(buffer.toString()); |
459 } | 463 } |
460 | 464 |
461 @override | 465 @override |
462 dynamic missingKey(String jsonPath, String key) { | 466 dynamic missingKey(String jsonPath, String key) { |
463 return new Exception('Missing key $key at $jsonPath'); | 467 return new Exception('Missing key $key at $jsonPath'); |
464 } | 468 } |
465 } | 469 } |
OLD | NEW |