Remove traces of json_god
This commit is contained in:
parent
45c3206ff6
commit
074b640980
7 changed files with 41 additions and 43 deletions
|
@ -7,6 +7,7 @@
|
||||||
* Changed view engine to use `Map<String, dynamic>`.
|
* Changed view engine to use `Map<String, dynamic>`.
|
||||||
* Remove dependency on `package:json_god` by default.
|
* Remove dependency on `package:json_god` by default.
|
||||||
* Remove dependency on `package:dart2_constant`.
|
* Remove dependency on `package:dart2_constant`.
|
||||||
|
* Remove `contentType` argument in `ResponseContext.serialize`.
|
||||||
|
|
||||||
# 1.1.5+1
|
# 1.1.5+1
|
||||||
* Patched annoying error that prevented MapServices from working,
|
* Patched annoying error that prevented MapServices from working,
|
||||||
|
|
|
@ -2,7 +2,8 @@ library angel_framework.http.response_context;
|
||||||
|
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:convert' show Converter, Encoding;
|
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:angel_route/angel_route.dart';
|
||||||
import 'package:dart2_constant/convert.dart';
|
import 'package:dart2_constant/convert.dart';
|
||||||
|
@ -15,9 +16,6 @@ import '../http/http.dart';
|
||||||
import 'request_context.dart';
|
import 'request_context.dart';
|
||||||
import 'server.dart' show Angel;
|
import 'server.dart' show Angel;
|
||||||
|
|
||||||
final RegExp _contentType =
|
|
||||||
new RegExp(r'([^/\n]+)\/\s*([^;\n]+)\s*(;\s*charset=([^$;\n]+))?');
|
|
||||||
|
|
||||||
final RegExp _straySlashes = new RegExp(r'(^/+)|(/+$)');
|
final RegExp _straySlashes = new RegExp(r'(^/+)|(/+$)');
|
||||||
|
|
||||||
/// Serializes response data into a String.
|
/// Serializes response data into a String.
|
||||||
|
@ -81,7 +79,7 @@ abstract class ResponseContext implements StreamSink<List<int>>, StringSink {
|
||||||
/// ```dart
|
/// ```dart
|
||||||
/// app.injectSerializer(JSON.encode);
|
/// app.injectSerializer(JSON.encode);
|
||||||
/// ```
|
/// ```
|
||||||
String Function(dynamic) serializer = json.encode;
|
String Function(dynamic) serializer = c.json.encode;
|
||||||
|
|
||||||
/// This response's status code.
|
/// This response's status code.
|
||||||
int get statusCode => _statusCode;
|
int get statusCode => _statusCode;
|
||||||
|
@ -169,7 +167,9 @@ abstract class ResponseContext implements StreamSink<List<int>>, StringSink {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Serializes JSON to the response.
|
/// 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.
|
/// Returns a JSONP response.
|
||||||
///
|
///
|
||||||
|
@ -302,21 +302,11 @@ abstract class ResponseContext implements StreamSink<List<int>>, StringSink {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Serializes data to the response.
|
/// Serializes data to the response.
|
||||||
///
|
bool serialize(value) {
|
||||||
/// [contentType] can be either a [String], or a [ContentType].
|
|
||||||
bool serialize(value, {contentType}) {
|
|
||||||
if (!isOpen) throw closed();
|
if (!isOpen) throw closed();
|
||||||
|
|
||||||
var text = serializer(value);
|
var text = serializer(value);
|
||||||
|
|
||||||
if (text.isEmpty) return true;
|
if (text.isEmpty) return true;
|
||||||
|
|
||||||
if (contentType is String)
|
|
||||||
headers['content-type'] = contentType;
|
|
||||||
else if (contentType is ContentType) this.contentType = contentType;
|
|
||||||
|
|
||||||
write(text);
|
write(text);
|
||||||
|
|
||||||
end();
|
end();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import 'dart:async';
|
||||||
import 'dart:collection' show HashMap;
|
import 'dart:collection' show HashMap;
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:angel_container/angel_container.dart';
|
import 'package:angel_container/angel_container.dart';
|
||||||
import 'package:angel_container/mirrors.dart';
|
import 'package:angel_container/mirrors.dart';
|
||||||
import 'package:angel_http_exception/angel_http_exception.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:combinator/combinator.dart';
|
||||||
import 'package:logging/logging.dart';
|
import 'package:logging/logging.dart';
|
||||||
import 'package:tuple/tuple.dart';
|
import 'package:tuple/tuple.dart';
|
||||||
|
|
||||||
import '../http/http.dart';
|
import '../http/http.dart';
|
||||||
import 'angel_base.dart';
|
import 'angel_base.dart';
|
||||||
import 'request_context.dart';
|
import 'request_context.dart';
|
||||||
|
@ -305,8 +307,7 @@ class Angel extends AngelBase {
|
||||||
else if (result is bool) {
|
else if (result is bool) {
|
||||||
return result;
|
return result;
|
||||||
} else if (result != null) {
|
} else if (result != null) {
|
||||||
return res.serialize(result,
|
return res.serialize(result);
|
||||||
contentType: res.headers['content-type'] ?? 'application/json');
|
|
||||||
} else
|
} else
|
||||||
return res.isOpen;
|
return res.isOpen;
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:mirrors';
|
import 'dart:mirrors';
|
||||||
|
|
||||||
import '../../common.dart';
|
import '../../common.dart';
|
||||||
import 'service.dart';
|
import 'service.dart';
|
||||||
|
|
||||||
|
@ -54,9 +55,11 @@ class TypedService<T> extends Service {
|
||||||
}
|
}
|
||||||
|
|
||||||
serialize(x) {
|
serialize(x) {
|
||||||
if (x is Model)
|
// TODO: Custom serializer, i.e. json_god?
|
||||||
return json.encodeObject(x);
|
// if (x is Model)
|
||||||
else if (x is Map)
|
// return json.encodeObject(x);
|
||||||
|
// else
|
||||||
|
if (x is Map)
|
||||||
return x;
|
return x;
|
||||||
else if (x is Iterable)
|
else if (x is Iterable)
|
||||||
return x.map(serialize).toList();
|
return x.map(serialize).toList();
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import 'dart:convert';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
import 'package:angel_container/mirrors.dart';
|
import 'package:angel_container/mirrors.dart';
|
||||||
import 'package:angel_framework/angel_framework.dart';
|
import 'package:angel_framework/angel_framework.dart';
|
||||||
|
@ -74,7 +75,7 @@ main() {
|
||||||
body: json.encode({"arbitrary": "data"}),
|
body: json.encode({"arbitrary": "data"}),
|
||||||
headers: headers as Map<String, String>);
|
headers: headers as Map<String, String>);
|
||||||
print(response.body);
|
print(response.body);
|
||||||
Map result = god.deserialize(response.body);
|
Map result = json.decode(response.body);
|
||||||
expect(result["hello"], equals("hooked world"));
|
expect(result["hello"], equals("hooked world"));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -92,7 +93,7 @@ main() {
|
||||||
|
|
||||||
var response = await client.get("$url/todos");
|
var response = await client.get("$url/todos");
|
||||||
print(response.body);
|
print(response.body);
|
||||||
List result = god.deserialize(response.body);
|
List result = json.decode(response.body);
|
||||||
expect(result[0]["angel"], equals("framework"));
|
expect(result[0]["angel"], equals("framework"));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -115,7 +116,7 @@ main() {
|
||||||
var response = await client.get('$url/books');
|
var response = await client.get('$url/books');
|
||||||
print(response.body);
|
print(response.body);
|
||||||
|
|
||||||
var result = god.deserialize(response.body);
|
var result = json.decode(response.body);
|
||||||
expect(result, isList);
|
expect(result, isList);
|
||||||
expect(result, isNotEmpty);
|
expect(result, isNotEmpty);
|
||||||
expect(result[0], equals({'foo': 'bar'}));
|
expect(result[0], equals({'foo': 'bar'}));
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:angel_container/mirrors.dart';
|
import 'package:angel_container/mirrors.dart';
|
||||||
import 'package:angel_framework/angel_framework.dart';
|
import 'package:angel_framework/angel_framework.dart';
|
||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
|
import 'package:http_parser/http_parser.dart';
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
main() {
|
main() {
|
||||||
|
@ -13,10 +15,10 @@ main() {
|
||||||
setUp(() async {
|
setUp(() async {
|
||||||
app = new Angel(MirrorsReflector())
|
app = new Angel(MirrorsReflector())
|
||||||
..get('/foo', () => {'hello': 'world'})
|
..get('/foo', () => {'hello': 'world'})
|
||||||
..get(
|
..get('/bar', (req, ResponseContext res) async {
|
||||||
'/bar',
|
res.contentType = new MediaType('text', 'html');
|
||||||
(req, ResponseContext res) =>
|
await res.serialize({'hello': 'world'});
|
||||||
res.serialize({'hello': 'world'}, contentType: 'text/html'));
|
});
|
||||||
client = new http.Client();
|
client = new http.Client();
|
||||||
|
|
||||||
server = await new AngelHttp(app).startServer();
|
server = await new AngelHttp(app).startServer();
|
||||||
|
|
|
@ -54,9 +54,9 @@ main() {
|
||||||
var response = await client.post("$url/todos",
|
var response = await client.post("$url/todos",
|
||||||
headers: headers as Map<String, String>, body: postData);
|
headers: headers as Map<String, String>, body: postData);
|
||||||
expect(response.statusCode, 201);
|
expect(response.statusCode, 201);
|
||||||
var json = god.deserialize(response.body);
|
var jsons = json.decode(response.body);
|
||||||
print(json);
|
print(jsons);
|
||||||
expect(json['text'], equals('Hello, world!'));
|
expect(jsons['text'], equals('Hello, world!'));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('can fetch data', () async {
|
test('can fetch data', () async {
|
||||||
|
@ -65,9 +65,9 @@ main() {
|
||||||
headers: headers as Map<String, String>, body: postData);
|
headers: headers as Map<String, String>, body: postData);
|
||||||
var response = await client.get("$url/todos/0");
|
var response = await client.get("$url/todos/0");
|
||||||
expect(response.statusCode, 200);
|
expect(response.statusCode, 200);
|
||||||
var json = god.deserialize(response.body);
|
var jsons = json.decode(response.body);
|
||||||
print(json);
|
print(jsons);
|
||||||
expect(json['text'], equals('Hello, world!'));
|
expect(jsons['text'], equals('Hello, world!'));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('can modify data', () async {
|
test('can modify data', () async {
|
||||||
|
@ -78,9 +78,9 @@ main() {
|
||||||
var response = await client.patch("$url/todos/0",
|
var response = await client.patch("$url/todos/0",
|
||||||
headers: headers as Map<String, String>, body: postData);
|
headers: headers as Map<String, String>, body: postData);
|
||||||
expect(response.statusCode, 200);
|
expect(response.statusCode, 200);
|
||||||
var json = god.deserialize(response.body);
|
var jsons = json.decode(response.body);
|
||||||
print(json);
|
print(jsons);
|
||||||
expect(json['text'], equals('modified'));
|
expect(jsons['text'], equals('modified'));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('can overwrite data', () async {
|
test('can overwrite data', () async {
|
||||||
|
@ -91,10 +91,10 @@ main() {
|
||||||
var response = await client.post("$url/todos/0",
|
var response = await client.post("$url/todos/0",
|
||||||
headers: headers as Map<String, String>, body: postData);
|
headers: headers as Map<String, String>, body: postData);
|
||||||
expect(response.statusCode, 200);
|
expect(response.statusCode, 200);
|
||||||
var json = god.deserialize(response.body);
|
var jsons = json.decode(response.body);
|
||||||
print(json);
|
print(jsons);
|
||||||
expect(json['text'], equals(null));
|
expect(jsons['text'], equals(null));
|
||||||
expect(json['over'], equals('write'));
|
expect(jsons['over'], equals('write'));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('can delete data', () async {
|
test('can delete data', () async {
|
||||||
|
@ -105,7 +105,7 @@ main() {
|
||||||
.then((r) => json.decode(r.body));
|
.then((r) => json.decode(r.body));
|
||||||
var response = await client.delete("$url/todos/${created['id']}");
|
var response = await client.delete("$url/todos/${created['id']}");
|
||||||
expect(response.statusCode, 200);
|
expect(response.statusCode, 200);
|
||||||
var json_ = god.deserialize(response.body);
|
var json_ = json.decode(response.body);
|
||||||
print(json_);
|
print(json_);
|
||||||
expect(json_['text'], equals('Hello, world!'));
|
expect(json_['text'], equals('Hello, world!'));
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue