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

Unified Diff: sdk/lib/convert/json.dart

Issue 1964953003: Make dart:convert strong mode clean. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Add comment for startChunkedConversion. Created 4 years, 7 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: sdk/lib/convert/json.dart
diff --git a/sdk/lib/convert/json.dart b/sdk/lib/convert/json.dart
index e80a9868fab20b1c881dc54f198c90467ac8c569..7ec54683ad64a68957d17dae9c48ac09e53049d4 100644
--- a/sdk/lib/convert/json.dart
+++ b/sdk/lib/convert/json.dart
@@ -139,7 +139,7 @@ class JsonCodec extends Codec<Object, String> {
* If [toEncodable] is omitted, it defaults to a function that returns the
* result of calling `.toJson()` on the unencodable object.
*/
- String encode(Object value, {toEncodable(var object)}) {
+ String encode(Object value, {dynamic toEncodable(dynamic object)}) {
Lasse Reichstein Nielsen 2016/05/11 09:06:14 Can you just remove the "dynamic" here? Or does th
floitsch 2016/05/11 11:20:44 I had to add it. I'm not really sure which one is
floitsch 2016/05/11 14:14:54 As discussed in person: removed them again.
if (toEncodable == null) toEncodable = _toEncodable;
if (toEncodable == null) return encoder.convert(value);
return new JsonEncoder(toEncodable).convert(value);
@@ -159,7 +159,7 @@ class JsonCodec extends Codec<Object, String> {
/**
* This class converts JSON objects to strings.
*/
-class JsonEncoder extends ChunkedConverter<Object, String, Object, String> {
+class JsonEncoder extends Converter<Object, String> {
/**
* The string used for indention.
*
@@ -174,7 +174,7 @@ class JsonEncoder extends ChunkedConverter<Object, String, Object, String> {
* Function called on non-encodable objects to return a replacement
* encodable object that will be encoded in the orignal's place.
*/
- final Function _toEncodable;
+ final _ToEncodable _toEncodable;
/**
* Creates a JSON encoder.
@@ -188,7 +188,7 @@ class JsonEncoder extends ChunkedConverter<Object, String, Object, String> {
* If [toEncodable] is omitted, it defaults to calling `.toJson()` on
* the object.
*/
- const JsonEncoder([Object toEncodable(Object nonSerializable)])
+ const JsonEncoder([dynamic toEncodable(dynamic nonSerializable)])
: this.indent = null,
this._toEncodable = toEncodable;
@@ -211,7 +211,7 @@ class JsonEncoder extends ChunkedConverter<Object, String, Object, String> {
* the object.
*/
const JsonEncoder.withIndent(this.indent,
- [Object toEncodable(Object nonSerializable)])
+ [dynamic toEncodable(dynamic nonSerializable)])
Lasse Reichstein Nielsen 2016/05/11 09:06:14 Is this necessary? Is it because an int->int funct
floitsch 2016/05/11 11:20:45 Unfortunately necessary. Example output of the ana
: this._toEncodable = toEncodable;
/**
@@ -268,11 +268,13 @@ class JsonEncoder extends ChunkedConverter<Object, String, Object, String> {
// Override the base class's bind, to provide a better type.
Stream<String> bind(Stream<Object> stream) => super.bind(stream);
- Converter<Object, dynamic> fuse(Converter<String, dynamic> other) {
+ Converter<Object, dynamic/*=T*/> fuse/*<T>*/(
+ Converter<String, dynamic/*=T*/> other) {
if (other is Utf8Encoder) {
- return new JsonUtf8Encoder(indent, _toEncodable);
+ return new JsonUtf8Encoder(indent, _toEncodable)
+ as dynamic/*=Converter<Object, T>*/;
}
- return super.fuse(other);
+ return super.fuse/*<T>*/(other);
}
}
@@ -283,14 +285,13 @@ class JsonEncoder extends ChunkedConverter<Object, String, Object, String> {
* a JSON string, and then UTF-8 encoding the string, but without
* creating an intermediate string.
*/
-class JsonUtf8Encoder extends
- ChunkedConverter<Object, List<int>, Object, List<int>> {
+class JsonUtf8Encoder extends Converter<Object, List<int>> {
/** Default buffer size used by the JSON-to-UTF-8 encoder. */
static const int DEFAULT_BUFFER_SIZE = 256;
/** Indentation used in pretty-print mode, `null` if not pretty. */
final List<int> _indent;
/** Function called with each un-encodable object encountered. */
- final Function _toEncodable;
+ final _ToEncodable _toEncodable;
/** UTF-8 buffer size. */
final int _bufferSize;
@@ -320,7 +321,7 @@ class JsonUtf8Encoder extends
* object.
*/
JsonUtf8Encoder([String indent,
- toEncodable(Object object),
+ dynamic toEncodable(dynamic object),
int bufferSize = DEFAULT_BUFFER_SIZE])
: _indent = _utf8Encode(indent),
_toEncodable = toEncodable,
@@ -395,10 +396,6 @@ class JsonUtf8Encoder extends
Stream<List<int>> bind(Stream<Object> stream) {
return super.bind(stream);
}
-
- Converter<Object, dynamic> fuse(Converter<List<int>, dynamic> other) {
- return super.fuse(other);
- }
}
/**
@@ -408,7 +405,7 @@ class JsonUtf8Encoder extends
*/
class _JsonEncoderSink extends ChunkedConversionSink<Object> {
final String _indent;
- final Function _toEncodable;
+ final _ToEncodable _toEncodable;
final StringConversionSink _sink;
bool _isDone = false;
@@ -441,7 +438,7 @@ class _JsonUtf8EncoderSink extends ChunkedConversionSink<Object> {
/** The byte sink receiveing the encoded chunks. */
final ByteConversionSink _sink;
final List<int> _indent;
- final Function _toEncodable;
+ final _ToEncodable _toEncodable;
final int _bufferSize;
bool _isDone = false;
_JsonUtf8EncoderSink(this._sink, this._toEncodable, this._indent,
@@ -474,7 +471,7 @@ class _JsonUtf8EncoderSink extends ChunkedConversionSink<Object> {
/**
* This class parses JSON strings and builds the corresponding objects.
*/
-class JsonDecoder extends ChunkedConverter<String, Object, String, Object> {
+class JsonDecoder extends Converter<String, Object> {
final _Reviver _reviver;
/**
* Constructs a new JsonDecoder.
@@ -517,7 +514,7 @@ external _parseJson(String source, reviver(key, value));
// Implementation of encoder/stringifier.
-Object _defaultToEncodable(object) => object.toJson();
+dynamic _defaultToEncodable(dynamic object) => object.toJson();
/**
* JSON encoder that traverses an object structure and writes JSON source.
@@ -545,11 +542,10 @@ abstract class _JsonStringifier {
/** List of objects currently being traversed. Used to detect cycles. */
final List _seen = new List();
/** Function called for each un-encodable object encountered. */
- final Function _toEncodable;
+ final _ToEncodable _toEncodable;
- _JsonStringifier(Object _toEncodable(Object o))
- : _toEncodable = (_toEncodable != null) ? _toEncodable
- : _defaultToEncodable;
+ _JsonStringifier(dynamic toEncodable(dynamic o))
+ : _toEncodable = toEncodable ?? _defaultToEncodable;
/** Append a string to the JSON output. */
void writeString(String characters);
@@ -719,7 +715,7 @@ abstract class _JsonStringifier {
}
/** Serialize a [Map]. */
- bool writeMap(Map<String, Object> map) {
+ bool writeMap(Map map) {
if (map.isEmpty) {
writeString("{}");
return true;
@@ -761,7 +757,7 @@ abstract class _JsonPrettyPrintMixin implements _JsonStringifier {
/**
* Add [indentLevel] indentations to the JSON output.
*/
- void writeIndentation(indentLevel);
+ void writeIndentation(int indentLevel);
void writeList(List list) {
if (list.isEmpty) {
@@ -838,7 +834,8 @@ class _JsonStringStringifier extends _JsonStringifier {
* for each indentation level. It should only contain valid JSON whitespace
* characters (space, tab, carriage return or line feed).
*/
- static String stringify(object, toEncodable(object), String indent) {
+ static String stringify(
+ object, dynamic toEncodable(dynamic o), String indent) {
StringBuffer output = new StringBuffer();
printOn(object, output, toEncodable, indent);
return output.toString();
@@ -849,7 +846,7 @@ class _JsonStringStringifier extends _JsonStringifier {
*
* The result is written piecemally to the sink.
*/
- static void printOn(object, StringSink output, toEncodable(object),
+ static void printOn(object, StringSink output, dynamic toEncodable(dynamic o),
String indent) {
var stringifier;
if (indent == null) {
@@ -879,7 +876,7 @@ class _JsonStringStringifierPretty extends _JsonStringStringifier
with _JsonPrettyPrintMixin {
final String _indent;
- _JsonStringStringifierPretty(StringSink sink, Function toEncodable,
+ _JsonStringStringifierPretty(StringSink sink, dynamic toEncodable(dynamic o),
this._indent)
: super(sink, toEncodable);
@@ -888,6 +885,8 @@ class _JsonStringStringifierPretty extends _JsonStringStringifier
}
}
+typedef void _AddChunk(Uint8List list, int start, int end);
+
/**
* Specialization of [_JsonStringifier] that writes the JSON as UTF-8.
*
@@ -896,11 +895,12 @@ class _JsonStringStringifierPretty extends _JsonStringStringifier
*/
class _JsonUtf8Stringifier extends _JsonStringifier {
final int bufferSize;
- final Function addChunk;
+ final _AddChunk addChunk;
Uint8List buffer;
int index = 0;
- _JsonUtf8Stringifier(toEncodable, int bufferSize, this.addChunk)
+ _JsonUtf8Stringifier(
+ dynamic toEncodable(dynamic o), int bufferSize, this.addChunk)
: this.bufferSize = bufferSize,
buffer = new Uint8List(bufferSize),
super(toEncodable);
@@ -918,16 +918,15 @@ class _JsonUtf8Stringifier extends _JsonStringifier {
*/
static void stringify(Object object,
List<int> indent,
- toEncodableFunction(Object o),
+ dynamic toEncodable(dynamic o),
int bufferSize,
void addChunk(Uint8List chunk, int start, int end)) {
_JsonUtf8Stringifier stringifier;
if (indent != null) {
- stringifier = new _JsonUtf8StringifierPretty(toEncodableFunction, indent,
+ stringifier = new _JsonUtf8StringifierPretty(toEncodable, indent,
bufferSize, addChunk);
} else {
- stringifier = new _JsonUtf8Stringifier(toEncodableFunction,
- bufferSize, addChunk);
+ stringifier = new _JsonUtf8Stringifier(toEncodable, bufferSize, addChunk);
}
stringifier.writeObject(object);
stringifier.flush();
@@ -1037,8 +1036,9 @@ class _JsonUtf8Stringifier extends _JsonStringifier {
class _JsonUtf8StringifierPretty extends _JsonUtf8Stringifier
with _JsonPrettyPrintMixin {
final List<int> indent;
- _JsonUtf8StringifierPretty(toEncodableFunction, this.indent,
- bufferSize, addChunk)
+ _JsonUtf8StringifierPretty(
+ dynamic toEncodableFunction(dynamic o), this.indent,
+ bufferSize, void addChunk(Uint8List buffer, int start, int end))
: super(toEncodableFunction, bufferSize, addChunk);
void writeIndentation(int count) {

Powered by Google App Engine
This is Rietveld 408576698