Index: pkg/analysis_server/lib/src/protocol.dart |
diff --git a/pkg/analysis_server/lib/src/protocol.dart b/pkg/analysis_server/lib/src/protocol.dart |
index 7541aed2dd5f0a109b22122483a3f859e9c1ecb0..0d39c312dbf0a6811b826d9d7ca1661bf18ae985 100644 |
--- a/pkg/analysis_server/lib/src/protocol.dart |
+++ b/pkg/analysis_server/lib/src/protocol.dart |
@@ -75,9 +75,9 @@ class Request { |
} |
String id = result[Request.ID]; |
String method = result[Request.METHOD]; |
- Map<String, Object> params = result[Request.PARAMS]; |
+ var params = result[Request.PARAMS]; |
Request request = new Request(id, method); |
- if (params != null) { |
+ if (params is Map) { |
params.forEach((String key, Object value) { |
request.setParameter(key, value); |
}); |
@@ -153,12 +153,12 @@ class Request { |
* sent to the client to represent this response. |
*/ |
Map<String, Object> toJson() { |
- Map jsonObject = new Map(); |
+ Map<String, Object> jsonObject = new Map<String, Object>(); |
jsonObject[ID] = id; |
jsonObject[METHOD] = method; |
- params.forEach((String key, Object value) { |
- jsonObject[key] = value; |
- }); |
+ if (params.isNotEmpty) { |
+ jsonObject[PARAMS] = params; |
+ } |
return jsonObject; |
} |
} |
@@ -261,6 +261,32 @@ class Response { |
: this(request.id, new RequestError(-7, 'Unknown request')); |
/** |
+ * Initialize a newly created instance based upon the given JSON data |
+ */ |
+ factory Response.fromJson(Map<String, Object> json) { |
+ try { |
+ // TODO process result |
+ String id = json[Response.ID]; |
+ var error = json[Response.ERROR]; |
+ var result = json[Response.RESULT]; |
+ Response response; |
+ if (error is Map) { |
+ response = new Response(id, new RequestError.fromJson(error)); |
+ } else { |
+ response = new Response(id); |
+ } |
+ if (result is Map) { |
+ result.forEach((String key, Object value) { |
+ response.setResult(key, value); |
+ }); |
+ } |
+ return response; |
+ } catch (exception) { |
+ return null; |
+ } |
+ } |
+ |
+ /** |
* Return the value of the result field with the given [name]. |
*/ |
Object getResult(String name) { |
@@ -279,7 +305,7 @@ class Response { |
* sent to the client to represent this response. |
*/ |
Map<String, Object> toJson() { |
- Map jsonObject = new Map(); |
+ Map<String, Object> jsonObject = new Map<String, Object>(); |
jsonObject[ID] = id; |
if (error == null) { |
jsonObject[ERROR] = null; |
@@ -399,6 +425,26 @@ class RequestError { |
RequestError.internalError() : this(CODE_INTERNAL_ERROR, "Internal error"); |
/** |
+ * Initialize a newly created [Error] from the given JSON. |
+ */ |
+ factory RequestError.fromJson(Map<String, Object> json) { |
+ try { |
+ int code = json[RequestError.CODE]; |
+ String message = json[RequestError.MESSAGE]; |
+ Map<String, Object> data = json[RequestError.DATA]; |
+ RequestError requestError = new RequestError(code, message); |
+ if (data != null) { |
+ data.forEach((String key, Object value) { |
+ requestError.setData(key, value); |
+ }); |
+ } |
+ return requestError; |
+ } catch (exception) { |
+ return null; |
+ } |
+ } |
+ |
+ /** |
* Return the value of the data with the given [name], or `null` if there is |
* no such data associated with this error. |
*/ |
@@ -416,7 +462,7 @@ class RequestError { |
* sent to the client to represent this response. |
*/ |
Map<String, Object> toJson() { |
- Map jsonObject = new Map(); |
+ Map<String, Object> jsonObject = new Map<String, Object>(); |
jsonObject[CODE] = code; |
jsonObject[MESSAGE] = message; |
if (!data.isEmpty) { |
@@ -458,6 +504,25 @@ class Notification { |
Notification(this.event); |
/** |
+ * Initialize a newly created instance based upon the given JSON data |
+ */ |
+ factory Notification.fromJson(Map<String, Object> json) { |
+ try { |
+ String event = json[Notification.EVENT]; |
+ var params = json[Notification.PARAMS]; |
+ Notification notification = new Notification(event); |
+ if (params is Map) { |
+ params.forEach((String key, Object value) { |
+ notification.setParameter(key, value); |
+ }); |
+ } |
+ return notification; |
+ } catch (exception) { |
+ return null; |
+ } |
+ } |
+ |
+ /** |
* Return the value of the parameter with the given [name], or `null` if there |
* is no such parameter associated with this notification. |
*/ |
@@ -475,7 +540,7 @@ class Notification { |
* sent to the client to represent this response. |
*/ |
Map<String, Object> toJson() { |
- Map jsonObject = new Map(); |
+ Map<String, Object> jsonObject = new Map<String, Object>(); |
jsonObject[EVENT] = event; |
if (!params.isEmpty) { |
jsonObject[PARAMS] = params; |