parse json better

master
小赖 4 years ago
parent d9c728f310
commit f21ea828e0

@ -0,0 +1,58 @@
import 'package:dio/dio.dart';
enum ContentType {
TEXT_PLAIN,
TEXT_HTML,
TEXT_CSS,
TEXT_JS,
IMAGE,
AUDIO,
VIDEO,
JSON,
UNCATCH,
}
class DioParser {
Response _response;
DioParser(Response response) {
_response = response;
}
RequestOptions get request => _response.request;
Response get response => _response;
ContentType get type {
String _ctype = response.headers.map['content-type'].first;
switch (_ctype) {
case 'text/plain':
return ContentType.TEXT_PLAIN;
case 'text/html':
return ContentType.TEXT_HTML;
case 'text/css':
return ContentType.TEXT_CSS;
case 'text/javascript':
return ContentType.TEXT_JS;
case 'application/json':
return ContentType.JSON;
}
if (_ctype.contains('image/')) return ContentType.IMAGE;
if (_ctype.contains('audio/')) return ContentType.AUDIO;
if (_ctype.contains('video/'))
return ContentType.VIDEO;
else
return ContentType.UNCATCH;
}
String get highlight {
switch (type) {
case ContentType.TEXT_HTML:
return 'html';
case ContentType.TEXT_CSS:
return 'css';
case ContentType.TEXT_JS:
return 'javascript';
case ContentType.JSON:
return 'json';
default:
return '';
}
}
}

@ -4,11 +4,14 @@ import 'package:dio/dio.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_highlight/flutter_highlight.dart'; import 'package:flutter_highlight/flutter_highlight.dart';
import 'package:flutter_highlight/themes/atom-one-light.dart'; import 'package:flutter_highlight/themes/atom-one-light.dart';
import 'package:power_logger/src/parser/dio_parser/dio_parser.dart';
import 'package:power_logger/src/view/box_view.dart'; import 'package:power_logger/src/view/box_view.dart';
import 'package:power_logger/src/view/table_view.dart'; import 'package:power_logger/src/view/table_view.dart';
import 'package:power_logger/src/view/title_view.dart'; import 'package:power_logger/src/view/title_view.dart';
import 'package:pretty_json/pretty_json.dart'; import 'package:pretty_json/pretty_json.dart';
// enum
class DioResponseView extends StatefulWidget { class DioResponseView extends StatefulWidget {
final Response data; final Response data;
DioResponseView({Key key, @required this.data}) : super(key: key); DioResponseView({Key key, @required this.data}) : super(key: key);
@ -19,7 +22,8 @@ class DioResponseView extends StatefulWidget {
class _DioResponseViewState extends State<DioResponseView> { class _DioResponseViewState extends State<DioResponseView> {
bool _showRawData = false; bool _showRawData = false;
RequestOptions get _request => widget.data.request; RequestOptions get _request => _dioParser.request;
DioParser _dioParser;
_buildBaseURL() { _buildBaseURL() {
return _request.baseUrl == null || _request.baseUrl.length == 0 return _request.baseUrl == null || _request.baseUrl.length == 0
? const SizedBox() ? const SizedBox()
@ -66,6 +70,44 @@ class _DioResponseViewState extends State<DioResponseView> {
} }
_buildData() { _buildData() {
switch (_dioParser.type) {
case ContentType.TEXT_PLAIN:
case ContentType.TEXT_HTML:
return BoxView(
title: Text('Data'),
child: HighlightView(
widget.data.data,
language: _dioParser.highlight,
theme: atomOneLightTheme,
),
);
case ContentType.JSON:
//JSON.
//parse json of String type.
if (widget.data.data is String) {
String json = '';
json = prettyJson(jsonDecode(widget.data.data));
return BoxView(
title: Text('Data'),
child: HighlightView(
json,
language: 'json',
theme: atomOneLightTheme,
),
);
}
return BoxView(
title: Text('Data'),
child: HighlightView(
prettyJson(widget.data.data),
language: 'json',
theme: atomOneLightTheme,
),
);
break;
default:
break;
}
return BoxView( return BoxView(
title: Text('Data'), title: Text('Data'),
child: HighlightView( child: HighlightView(
@ -92,6 +134,12 @@ class _DioResponseViewState extends State<DioResponseView> {
); );
} }
@override
void initState() {
super.initState();
_dioParser = DioParser(widget.data);
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(

Loading…
Cancel
Save