dart-sdk/tests/lib/convert/chunked_conversion_json_encode1_test.dart
Lasse R.H. Nielsen f6ac970290 Reland "Add class modifiers to dart:convert." again.
This is a reland of commit 608934e330

Can be landed when Flutter with https://github.com/flutter/flutter/pull/123211
has been rolled into internal repository.



Original change's description:
> Reland "Add class modifiers to `dart:convert`."
>
> This is a reland of commit b2f4cf3e01
>
> Commented out deprecation for now.
>
> Original change's description:
> > Add class modifiers to `dart:convert`.
> >
> > The usual approach:
> > Pure interfaces marked `interface`.
> > Pure implementation classes marked `final`.
> > Base classes marked `base` or nothing, and `mixin class` if reasonable.
> > Combined X/XBase/XMixin where possible.
> >
> > CoreLibraryReviewExempt: Aske is away
> > Change-Id: I927f9bd488fb385ff9c17c8fc94920a1f5076347
> > Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/289200
> > Reviewed-by: Stephen Adams <sra@google.com>
> > Reviewed-by: Slava Egorov <vegorov@google.com>
> > Reviewed-by: Nate Bosch <nbosch@google.com>
> > Commit-Queue: Lasse Nielsen <lrn@google.com>
>
> CoreLibraryReviewExempt: Approved in original.
> Change-Id: I1bc14f99b742567e2634dcfcbc52f332dbcc5364
> Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/290521
> Reviewed-by: Nate Bosch <nbosch@google.com>
> Commit-Queue: Lasse Nielsen <lrn@google.com>

CoreLibraryReviewExempt: Approved in original.
Change-Id: If157e1ef2339d7a06e99a1e402f2f8ac93550b83
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/290960
Reviewed-by: Nate Bosch <nbosch@google.com>
Commit-Queue: Lasse Nielsen <lrn@google.com>
2023-03-29 16:54:23 +00:00

105 lines
2.1 KiB
Dart

// Copyright (c) 2013, 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.
import "package:expect/expect.dart";
import 'dart:convert';
final TESTS = [
[5, '5'],
[-42, '-42'],
[3.14, '3.14'],
[true, 'true'],
[false, 'false'],
[null, 'null'],
['quote"or\'', '"quote\\"or\'"'],
['', '""'],
[[], "[]"],
[
[3, -4.5, true, "hi", false],
'[3,-4.5,true,"hi",false]'
],
[
[null],
"[null]"
],
[
[
[null]
],
"[[null]]"
],
[
[
[3]
],
"[[3]]"
],
[{}, "{}"],
[
{"x": 3, "y": 4.5, "z": "hi", "u": true, "v": false},
'{"x":3,"y":4.5,"z":"hi","u":true,"v":false}'
],
[
{"x": null},
'{"x":null}'
],
[
{"x": {}},
'{"x":{}}'
],
// Note that -0.0 won't be treated the same in JS. The Json spec seems to
// allow it, though.
[
{"hi there": 499, "'": -0.0},
'{"hi there":499,"\'":-0.0}'
],
[r'\foo', r'"\\foo"'],
];
class MyStringConversionSink extends StringConversionSink {
var buffer = new StringBuffer();
var callback;
MyStringConversionSink(this.callback);
addSlice(str, start, end, bool isLast) {
buffer.write(str.substring(start, end));
if (isLast) close();
}
close() {
callback(buffer.toString());
}
}
String encode(Object? o) {
var result;
ChunkedConversionSink<String> stringSink =
new MyStringConversionSink((x) => result = x);
var objectSink = new JsonEncoder().startChunkedConversion(stringSink);
objectSink.add(o);
objectSink.close();
return result;
}
String encode2(Object? o) {
var result;
var encoder = new JsonEncoder();
ChunkedConversionSink<String> stringSink =
new StringConversionSink.withCallback((x) => result = x);
var objectSink = encoder.startChunkedConversion(stringSink);
objectSink.add(o);
objectSink.close();
return result;
}
main() {
for (var test in TESTS) {
var o = test[0];
var expected = test[1];
Expect.equals(expected, encode(o));
Expect.equals(expected, encode2(o));
}
}