Remove traces of json_god

This commit is contained in:
Tobe O 2018-08-19 22:31:08 -04:00
parent 45c3206ff6
commit 074b640980
7 changed files with 41 additions and 43 deletions

View file

@ -7,6 +7,7 @@
* Changed view engine to use `Map<String, dynamic>`.
* Remove dependency on `package:json_god` by default.
* Remove dependency on `package:dart2_constant`.
* Remove `contentType` argument in `ResponseContext.serialize`.
# 1.1.5+1
* Patched annoying error that prevented MapServices from working,

View file

@ -2,7 +2,8 @@ library angel_framework.http.response_context;
import 'dart:async';
import 'dart:convert' show Converter, Encoding;
import 'dart:io' show BytesBuilder, HttpResponse;
import 'dart:convert' as c show json;
import 'dart:io' show BytesBuilder, Cookie, HttpResponse;
import 'package:angel_route/angel_route.dart';
import 'package:dart2_constant/convert.dart';
@ -15,9 +16,6 @@ import '../http/http.dart';
import 'request_context.dart';
import 'server.dart' show Angel;
final RegExp _contentType =
new RegExp(r'([^/\n]+)\/\s*([^;\n]+)\s*(;\s*charset=([^$;\n]+))?');
final RegExp _straySlashes = new RegExp(r'(^/+)|(/+$)');
/// Serializes response data into a String.
@ -81,7 +79,7 @@ abstract class ResponseContext implements StreamSink<List<int>>, StringSink {
/// ```dart
/// app.injectSerializer(JSON.encode);
/// ```
String Function(dynamic) serializer = json.encode;
String Function(dynamic) serializer = c.json.encode;
/// This response's status code.
int get statusCode => _statusCode;
@ -169,7 +167,9 @@ abstract class ResponseContext implements StreamSink<List<int>>, StringSink {
}
/// Serializes JSON to the response.
void json(value) => serialize(value, contentType: 'application/json');
void json(value) => this
..contentType = MediaType('application', 'json')
..serialize(value);
/// Returns a JSONP response.
///
@ -302,21 +302,11 @@ abstract class ResponseContext implements StreamSink<List<int>>, StringSink {
}
/// Serializes data to the response.
///
/// [contentType] can be either a [String], or a [ContentType].
bool serialize(value, {contentType}) {
bool serialize(value) {
if (!isOpen) throw closed();
var text = serializer(value);
if (text.isEmpty) return true;
if (contentType is String)
headers['content-type'] = contentType;
else if (contentType is ContentType) this.contentType = contentType;
write(text);
end();
return false;
}

View file

@ -4,6 +4,7 @@ import 'dart:async';
import 'dart:collection' show HashMap;
import 'dart:convert';
import 'dart:io';
import 'package:angel_container/angel_container.dart';
import 'package:angel_container/mirrors.dart';
import 'package:angel_http_exception/angel_http_exception.dart';
@ -11,6 +12,7 @@ import 'package:angel_route/angel_route.dart';
import 'package:combinator/combinator.dart';
import 'package:logging/logging.dart';
import 'package:tuple/tuple.dart';
import '../http/http.dart';
import 'angel_base.dart';
import 'request_context.dart';
@ -305,8 +307,7 @@ class Angel extends AngelBase {
else if (result is bool) {
return result;
} else if (result != null) {
return res.serialize(result,
contentType: res.headers['content-type'] ?? 'application/json');
return res.serialize(result);
} else
return res.isOpen;
});

View file

@ -1,5 +1,6 @@
import 'dart:async';
import 'dart:mirrors';
import '../../common.dart';
import 'service.dart';
@ -54,9 +55,11 @@ class TypedService<T> extends Service {
}
serialize(x) {
if (x is Model)
return json.encodeObject(x);
else if (x is Map)
// TODO: Custom serializer, i.e. json_god?
// if (x is Model)
// return json.encodeObject(x);
// else
if (x is Map)
return x;
else if (x is Iterable)
return x.map(serialize).toList();

View file

@ -1,3 +1,4 @@
import 'dart:convert';
import 'dart:io';
import 'package:angel_container/mirrors.dart';
import 'package:angel_framework/angel_framework.dart';
@ -74,7 +75,7 @@ main() {
body: json.encode({"arbitrary": "data"}),
headers: headers as Map<String, String>);
print(response.body);
Map result = god.deserialize(response.body);
Map result = json.decode(response.body);
expect(result["hello"], equals("hooked world"));
});
@ -92,7 +93,7 @@ main() {
var response = await client.get("$url/todos");
print(response.body);
List result = god.deserialize(response.body);
List result = json.decode(response.body);
expect(result[0]["angel"], equals("framework"));
});
@ -115,7 +116,7 @@ main() {
var response = await client.get('$url/books');
print(response.body);
var result = god.deserialize(response.body);
var result = json.decode(response.body);
expect(result, isList);
expect(result, isNotEmpty);
expect(result[0], equals({'foo': 'bar'}));

View file

@ -1,7 +1,9 @@
import 'dart:io';
import 'package:angel_container/mirrors.dart';
import 'package:angel_framework/angel_framework.dart';
import 'package:http/http.dart' as http;
import 'package:http_parser/http_parser.dart';
import 'package:test/test.dart';
main() {
@ -13,10 +15,10 @@ main() {
setUp(() async {
app = new Angel(MirrorsReflector())
..get('/foo', () => {'hello': 'world'})
..get(
'/bar',
(req, ResponseContext res) =>
res.serialize({'hello': 'world'}, contentType: 'text/html'));
..get('/bar', (req, ResponseContext res) async {
res.contentType = new MediaType('text', 'html');
await res.serialize({'hello': 'world'});
});
client = new http.Client();
server = await new AngelHttp(app).startServer();

View file

@ -54,9 +54,9 @@ main() {
var response = await client.post("$url/todos",
headers: headers as Map<String, String>, body: postData);
expect(response.statusCode, 201);
var json = god.deserialize(response.body);
print(json);
expect(json['text'], equals('Hello, world!'));
var jsons = json.decode(response.body);
print(jsons);
expect(jsons['text'], equals('Hello, world!'));
});
test('can fetch data', () async {
@ -65,9 +65,9 @@ main() {
headers: headers as Map<String, String>, body: postData);
var response = await client.get("$url/todos/0");
expect(response.statusCode, 200);
var json = god.deserialize(response.body);
print(json);
expect(json['text'], equals('Hello, world!'));
var jsons = json.decode(response.body);
print(jsons);
expect(jsons['text'], equals('Hello, world!'));
});
test('can modify data', () async {
@ -78,9 +78,9 @@ main() {
var response = await client.patch("$url/todos/0",
headers: headers as Map<String, String>, body: postData);
expect(response.statusCode, 200);
var json = god.deserialize(response.body);
print(json);
expect(json['text'], equals('modified'));
var jsons = json.decode(response.body);
print(jsons);
expect(jsons['text'], equals('modified'));
});
test('can overwrite data', () async {
@ -91,10 +91,10 @@ main() {
var response = await client.post("$url/todos/0",
headers: headers as Map<String, String>, body: postData);
expect(response.statusCode, 200);
var json = god.deserialize(response.body);
print(json);
expect(json['text'], equals(null));
expect(json['over'], equals('write'));
var jsons = json.decode(response.body);
print(jsons);
expect(jsons['text'], equals(null));
expect(jsons['over'], equals('write'));
});
test('can delete data', () async {
@ -105,7 +105,7 @@ main() {
.then((r) => json.decode(r.body));
var response = await client.delete("$url/todos/${created['id']}");
expect(response.statusCode, 200);
var json_ = god.deserialize(response.body);
var json_ = json.decode(response.body);
print(json_);
expect(json_['text'], equals('Hello, world!'));
});