Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(109)

Unified Diff: pkg/compiler/lib/src/serialization/json_serializer.dart

Issue 1192103002: Support serialization of the compiler backbone. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Handle (bypass) external const constructors. Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/compiler/lib/src/serialization/json_serializer.dart
diff --git a/pkg/compiler/lib/src/serialization/json_serializer.dart b/pkg/compiler/lib/src/serialization/json_serializer.dart
new file mode 100644
index 0000000000000000000000000000000000000000..399bdbfb7157eacc0765d29ccae38985b611c53c
--- /dev/null
+++ b/pkg/compiler/lib/src/serialization/json_serializer.dart
@@ -0,0 +1,212 @@
+// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
+// 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 dart2js.serialization.json;
+
+import 'dart:convert';
+import 'keys.dart';
+import 'serialization.dart';
+import 'values.dart';
+
+/// Serialization encoder for JSON.
+class JsonSerializationEncoder implements SerializationEncoder {
+ const JsonSerializationEncoder();
+
+ String encode(ObjectValue objectValue) {
+ return new JsonEncoder.withIndent(' ').convert(
+ const JsonValueEncoder().convert(objectValue));
+ }
+}
+
+/// Serialization decoder for JSON.
+class JsonSerializationDecoder implements SerializationDecoder {
+ const JsonSerializationDecoder();
+
+ Map decode(String text) => JSON.decode(text);
+
+ /// Returns the name of the [key] which used for to store a [Key] into a
+ /// [Map]; corresponding to the encoding of object properties in
+ /// [JsonValueEncoder.visitObject].
+ getObjectPropertyValue(Key key) => key.name;
+}
+
+/// A [ValueVisitor] that computes a JSON object value.
+class JsonValueEncoder implements ValueVisitor {
+ const JsonValueEncoder();
+
+ convert(Value value) => visit(value, null);
+
+ @override
+ visit(Value value, [arg]) => value.accept(this, arg);
+
+ @override
+ bool visitBool(BoolValue value, arg) => value.value;
+
+ @override
+ visitConstant(ConstantValue value, arg) => visit(value.id);
+
+ @override
+ double visitDouble(DoubleValue value, arg) => value.value;
+
+ @override
+ visitElement(ElementValue value, arg) => visit(value.id);
+
+ @override
+ visitEnum(EnumValue value, arg) => value.value.index;
+
+ @override
+ int visitInt(IntValue value, arg) => value.value;
+
+ @override
+ List visitList(ListValue value, arg) => value.values.map(visit).toList();
+
+ @override
+ Map visitMap(MapValue value, arg) {
+ Map<String, dynamic> map = <String, dynamic>{};
+ value.map.forEach((String key, Value value) {
+ map[key] = visit(value);
+ });
+ return map;
+ }
+
+ @override
+ Map visitObject(ObjectValue value, arg) {
+ Map<String, dynamic> map = <String, dynamic>{};
+ value.map.forEach((Key key, Value value) {
+ map[key.name] = visit(value);
+ });
+ return map;
+ }
+
+ @override
+ String visitString(StringValue value, arg) => value.value;
+
+ @override
+ visitType(TypeValue value, arg) => visit(value.id);
+
+ @override
+ visitUri(UriValue value, arg) => '${value.value}';
+}
+
+/// [ValueVisitor] that generates a verbose JSON-like output.
+class PrettyPrintEncoder implements ValueVisitor {
+ StringBuffer buffer;
+
+ String toText(Value value) {
+ buffer = new StringBuffer();
+ visit(value, '');
+ String text = buffer.toString();
+ buffer = null;
+ return text;
+ }
+
+ @override
+ void visit(Value value, String indentation) {
+ value.accept(this, indentation);
+ }
+
+ @override
+ void visitBool(BoolValue value, String indentation) {
+ buffer.write(value.value);
+ }
+
+ @override
+ void visitConstant(ConstantValue value, String indentation) {
+ buffer.write('Constant(${value.id}):${value.constant.getText()}');
+ }
+
+ @override
+ void visitDouble(DoubleValue value, String indentation) {
+ buffer.write(value.value);
+ }
+ @override
+ void visitElement(ElementValue value, String indentation) {
+ buffer.write('Element(${value.id}):${value.element}');
+ }
+
+ @override
+ void visitEnum(EnumValue value, String indentation) {
+ buffer.write(value.value);
+ }
+
+ @override
+ void visitInt(IntValue value, String indentation) {
+ buffer.write(value.value);
+ }
+
+ @override
+ void visitList(ListValue value, String indentation) {
+ if (value.values.isEmpty) {
+ buffer.write('[]');
+ } else {
+ buffer.write('[');
+ bool needsComma = false;
+ String nextIndentation = '${indentation} ';
+ for (Value element in value.values) {
+ if (needsComma) {
+ buffer.write(',');
+ }
+ buffer.write('\n$nextIndentation');
+ visit(element, nextIndentation);
+ needsComma = true;
+ }
+ buffer.write('\n$indentation]');
+ }
+ }
+
+ @override
+ void visitMap(MapValue value, String indentation) {
+ if (value.map.isEmpty) {
+ buffer.write('{}');
+ } else {
+ buffer.write('{');
+ bool needsComma = false;
+ String nextIndentation = '${indentation} ';
+ value.map.forEach((String key, Value subvalue) {
+ if (needsComma) {
+ buffer.write(',');
+ }
+ buffer.write('\n$nextIndentation$key: ');
+ visit(subvalue, nextIndentation);
+ needsComma = true;
+ });
+ buffer.write('\n$indentation}');
+ }
+ }
+
+ @override
+ void visitObject(ObjectValue value, String indentation) {
+ if (value.map.isEmpty) {
+ buffer.write('{}');
+ } else {
+ buffer.write('{');
+ bool needsComma = false;
+ String nextIndentation = '${indentation} ';
+ value.map.forEach((Key key, Value subvalue) {
+ if (needsComma) {
+ buffer.write(',');
+ }
+ buffer.write('\n$nextIndentation$key: ');
+ visit(subvalue, nextIndentation);
+ needsComma = true;
+ });
+ buffer.write('\n$indentation}');
+ }
+ }
+
+ @override
+ void visitString(StringValue value, String indentation) {
+ buffer.write('"${value.value}"');
+ }
+
+ @override
+ void visitType(TypeValue value, String indentation) {
+ buffer.write('Type(${value.id}):${value.type}');
+ }
+
+ @override
+ void visitUri(UriValue value, String indentation) {
+ buffer.write('Uri(${value.value})');
+ }
+}
« no previous file with comments | « pkg/compiler/lib/src/serialization/element_serialization.dart ('k') | pkg/compiler/lib/src/serialization/keys.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698