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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 library dart2js.serialization.json;
6
7 import 'dart:convert';
8 import 'keys.dart';
9 import 'serialization.dart';
10 import 'values.dart';
11
12 /// Serialization encoder for JSON.
13 class JsonSerializationEncoder implements SerializationEncoder {
14 const JsonSerializationEncoder();
15
16 String encode(ObjectValue objectValue) {
17 return new JsonEncoder.withIndent(' ').convert(
18 const JsonValueEncoder().convert(objectValue));
19 }
20 }
21
22 /// Serialization decoder for JSON.
23 class JsonSerializationDecoder implements SerializationDecoder {
24 const JsonSerializationDecoder();
25
26 Map decode(String text) => JSON.decode(text);
27
28 /// Returns the name of the [key] which used for to store a [Key] into a
29 /// [Map]; corresponding to the encoding of object properties in
30 /// [JsonValueEncoder.visitObject].
31 getObjectPropertyValue(Key key) => key.name;
32 }
33
34 /// A [ValueVisitor] that computes a JSON object value.
35 class JsonValueEncoder implements ValueVisitor {
36 const JsonValueEncoder();
37
38 convert(Value value) => visit(value, null);
39
40 @override
41 visit(Value value, [arg]) => value.accept(this, arg);
42
43 @override
44 bool visitBool(BoolValue value, arg) => value.value;
45
46 @override
47 visitConstant(ConstantValue value, arg) => visit(value.id);
48
49 @override
50 double visitDouble(DoubleValue value, arg) => value.value;
51
52 @override
53 visitElement(ElementValue value, arg) => visit(value.id);
54
55 @override
56 visitEnum(EnumValue value, arg) => value.value.index;
57
58 @override
59 int visitInt(IntValue value, arg) => value.value;
60
61 @override
62 List visitList(ListValue value, arg) => value.values.map(visit).toList();
63
64 @override
65 Map visitMap(MapValue value, arg) {
66 Map<String, dynamic> map = <String, dynamic>{};
67 value.map.forEach((String key, Value value) {
68 map[key] = visit(value);
69 });
70 return map;
71 }
72
73 @override
74 Map visitObject(ObjectValue value, arg) {
75 Map<String, dynamic> map = <String, dynamic>{};
76 value.map.forEach((Key key, Value value) {
77 map[key.name] = visit(value);
78 });
79 return map;
80 }
81
82 @override
83 String visitString(StringValue value, arg) => value.value;
84
85 @override
86 visitType(TypeValue value, arg) => visit(value.id);
87
88 @override
89 visitUri(UriValue value, arg) => '${value.value}';
90 }
91
92 /// [ValueVisitor] that generates a verbose JSON-like output.
93 class PrettyPrintEncoder implements ValueVisitor {
94 StringBuffer buffer;
95
96 String toText(Value value) {
97 buffer = new StringBuffer();
98 visit(value, '');
99 String text = buffer.toString();
100 buffer = null;
101 return text;
102 }
103
104 @override
105 void visit(Value value, String indentation) {
106 value.accept(this, indentation);
107 }
108
109 @override
110 void visitBool(BoolValue value, String indentation) {
111 buffer.write(value.value);
112 }
113
114 @override
115 void visitConstant(ConstantValue value, String indentation) {
116 buffer.write('Constant(${value.id}):${value.constant.getText()}');
117 }
118
119 @override
120 void visitDouble(DoubleValue value, String indentation) {
121 buffer.write(value.value);
122 }
123 @override
124 void visitElement(ElementValue value, String indentation) {
125 buffer.write('Element(${value.id}):${value.element}');
126 }
127
128 @override
129 void visitEnum(EnumValue value, String indentation) {
130 buffer.write(value.value);
131 }
132
133 @override
134 void visitInt(IntValue value, String indentation) {
135 buffer.write(value.value);
136 }
137
138 @override
139 void visitList(ListValue value, String indentation) {
140 if (value.values.isEmpty) {
141 buffer.write('[]');
142 } else {
143 buffer.write('[');
144 bool needsComma = false;
145 String nextIndentation = '${indentation} ';
146 for (Value element in value.values) {
147 if (needsComma) {
148 buffer.write(',');
149 }
150 buffer.write('\n$nextIndentation');
151 visit(element, nextIndentation);
152 needsComma = true;
153 }
154 buffer.write('\n$indentation]');
155 }
156 }
157
158 @override
159 void visitMap(MapValue value, String indentation) {
160 if (value.map.isEmpty) {
161 buffer.write('{}');
162 } else {
163 buffer.write('{');
164 bool needsComma = false;
165 String nextIndentation = '${indentation} ';
166 value.map.forEach((String key, Value subvalue) {
167 if (needsComma) {
168 buffer.write(',');
169 }
170 buffer.write('\n$nextIndentation$key: ');
171 visit(subvalue, nextIndentation);
172 needsComma = true;
173 });
174 buffer.write('\n$indentation}');
175 }
176 }
177
178 @override
179 void visitObject(ObjectValue value, String indentation) {
180 if (value.map.isEmpty) {
181 buffer.write('{}');
182 } else {
183 buffer.write('{');
184 bool needsComma = false;
185 String nextIndentation = '${indentation} ';
186 value.map.forEach((Key key, Value subvalue) {
187 if (needsComma) {
188 buffer.write(',');
189 }
190 buffer.write('\n$nextIndentation$key: ');
191 visit(subvalue, nextIndentation);
192 needsComma = true;
193 });
194 buffer.write('\n$indentation}');
195 }
196 }
197
198 @override
199 void visitString(StringValue value, String indentation) {
200 buffer.write('"${value.value}"');
201 }
202
203 @override
204 void visitType(TypeValue value, String indentation) {
205 buffer.write('Type(${value.id}):${value.type}');
206 }
207
208 @override
209 void visitUri(UriValue value, String indentation) {
210 buffer.write('Uri(${value.value})');
211 }
212 }
OLDNEW
« 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