Index: pkg/analysis_server/lib/src/protocol/protocol_internal.dart |
diff --git a/pkg/analysis_server/lib/src/protocol/protocol_internal.dart b/pkg/analysis_server/lib/src/protocol/protocol_internal.dart |
index 38e5fbac53600a898074fbb50e6a75e4073bf176..0d6799d96f669c4a6fb1f4097a9ced346cd504e1 100644 |
--- a/pkg/analysis_server/lib/src/protocol/protocol_internal.dart |
+++ b/pkg/analysis_server/lib/src/protocol/protocol_internal.dart |
@@ -2,13 +2,17 @@ |
// for details. All rights reserved. Use of this source code is governed by a |
// BSD-style license that can be found in the LICENSE file. |
-library analysis_server.src.protocol.protocol_internal; |
- |
import 'dart:collection'; |
import 'dart:convert' hide JsonDecoder; |
import 'package:analysis_server/protocol/protocol.dart'; |
import 'package:analysis_server/protocol/protocol_generated.dart'; |
+import 'package:analyzer_plugin/protocol/protocol_common.dart'; |
+import 'package:analyzer_plugin/src/protocol/protocol_internal.dart' |
+ show JsonDecoder; |
+ |
+export 'package:analyzer_plugin/src/protocol/protocol_internal.dart' |
+ show JsonDecoder; |
final Map<String, RefactoringKind> REQUEST_ID_REFACTORING_KINDS = |
new HashMap<String, RefactoringKind>(); |
@@ -245,146 +249,6 @@ abstract class HasToJson { |
} |
/** |
- * Base class for decoding JSON objects. The derived class must implement |
- * error reporting logic. |
- */ |
-abstract class JsonDecoder { |
- /** |
- * Retrieve the RefactoringKind that should be assumed when decoding |
- * refactoring feedback objects, or null if no refactoring feedback object is |
- * expected to be encountered. |
- */ |
- RefactoringKind get refactoringKind; |
- |
- /** |
- * Decode a JSON object that is expected to be a boolean. The strings "true" |
- * and "false" are also accepted. |
- */ |
- bool decodeBool(String jsonPath, Object json) { |
- if (json is bool) { |
- return json; |
- } else if (json == 'true') { |
- return true; |
- } else if (json == 'false') { |
- return false; |
- } |
- throw mismatch(jsonPath, 'bool', json); |
- } |
- |
- /** |
- * Decode a JSON object that is expected to be an integer. A string |
- * representation of an integer is also accepted. |
- */ |
- int decodeInt(String jsonPath, Object json) { |
- if (json is int) { |
- return json; |
- } else if (json is String) { |
- return int.parse(json, onError: (String value) { |
- throw mismatch(jsonPath, 'int', json); |
- }); |
- } |
- throw mismatch(jsonPath, 'int', json); |
- } |
- |
- /** |
- * Decode a JSON object that is expected to be a List. The [decoder] is used |
- * to decode the items in the list. |
- * |
- * The type parameter [E] is the expected type of the elements in the list. |
- */ |
- List/*<E>*/ decodeList/*<E>*/(String jsonPath, Object json, |
- [JsonDecoderCallback/*<E>*/ decoder]) { |
- if (json == null) { |
- return/*<E>*/ []; |
- } else if (json is List) { |
- List/*<E>*/ result = /*<E>*/ []; |
- for (int i = 0; i < json.length; i++) { |
- result.add(decoder('$jsonPath[$i]', json[i])); |
- } |
- return result; |
- } else { |
- throw mismatch(jsonPath, 'List', json); |
- } |
- } |
- |
- /** |
- * Decode a JSON object that is expected to be a Map. [keyDecoder] is used |
- * to decode the keys, and [valueDecoder] is used to decode the values. |
- */ |
- Map/*<K, V>*/ decodeMap/*<K, V>*/(String jsonPath, Object json, |
- {JsonDecoderCallback/*<K>*/ keyDecoder, |
- JsonDecoderCallback/*<V>*/ valueDecoder}) { |
- if (json == null) { |
- return {}; |
- } else if (json is Map) { |
- Map/*<K, V>*/ result = /*<K, V>*/ {}; |
- json.forEach((String key, value) { |
- Object/*=K*/ decodedKey; |
- if (keyDecoder != null) { |
- decodedKey = keyDecoder('$jsonPath.key', key); |
- } else { |
- decodedKey = key as Object/*=K*/; |
- } |
- if (valueDecoder != null) { |
- value = valueDecoder('$jsonPath[${JSON.encode(key)}]', value); |
- } |
- result[decodedKey] = value as Object/*=V*/; |
- }); |
- return result; |
- } else { |
- throw mismatch(jsonPath, 'Map', json); |
- } |
- } |
- |
- /** |
- * Decode a JSON object that is expected to be a string. |
- */ |
- String decodeString(String jsonPath, Object json) { |
- if (json is String) { |
- return json; |
- } else { |
- throw mismatch(jsonPath, 'String', json); |
- } |
- } |
- |
- /** |
- * Decode a JSON object that is expected to be one of several choices, |
- * where the choices are disambiguated by the contents of the field [field]. |
- * [decoders] is a map from each possible string in the field to the decoder |
- * that should be used to decode the JSON object. |
- */ |
- Object decodeUnion(String jsonPath, Map json, String field, |
- Map<String, JsonDecoderCallback> decoders) { |
- if (json is Map) { |
- if (!json.containsKey(field)) { |
- throw missingKey(jsonPath, field); |
- } |
- var disambiguatorPath = '$jsonPath[${JSON.encode(field)}]'; |
- String disambiguator = decodeString(disambiguatorPath, json[field]); |
- if (!decoders.containsKey(disambiguator)) { |
- throw mismatch( |
- disambiguatorPath, 'One of: ${decoders.keys.toList()}', json); |
- } |
- return decoders[disambiguator](jsonPath, json); |
- } else { |
- throw mismatch(jsonPath, 'Map', json); |
- } |
- } |
- |
- /** |
- * Create an exception to throw if the JSON object at [jsonPath] fails to |
- * match the API definition of [expected]. |
- */ |
- dynamic mismatch(String jsonPath, String expected, [Object actual]); |
- |
- /** |
- * Create an exception to throw if the JSON object at [jsonPath] is missing |
- * the key [key]. |
- */ |
- dynamic missingKey(String jsonPath, String key); |
-} |
- |
-/** |
* JsonDecoder for decoding requests. Errors are reporting by throwing a |
* [RequestFailure]. |
*/ |