Add json definition for API result

This commit is contained in:
Oystein Kristoffer Tveit 2020-06-23 16:55:39 +02:00
parent f9b1aaeec3
commit 0f606b7d1b
3 changed files with 77 additions and 22 deletions

View File

@ -0,0 +1,10 @@
import 'dart:convert';
import 'package:unofficial_jisho_api/unofficial_jisho_api.dart';
final jisho = JishoApi();
final encoder = JsonEncoder.withIndent(' ');
void main() async {
await jisho.searchForPhrase('反対').then((data) {
print(encoder.convert(data));
});
}

View File

@ -258,8 +258,7 @@ class PhrasePageScrapeResult {
this.notes this.notes
}); });
Map<String, dynamic> toJson() => Map<String, dynamic> toJson() => {
{
'found': found, 'found': found,
'query': query, 'query': query,
'uri': uri, 'uri': uri,
@ -285,8 +284,12 @@ class JishoJapaneseWord {
word: json['word'] as String, word: json['word'] as String,
reading: json['reading'] as String reading: json['reading'] as String
); );
} }
Map<String, dynamic> toJson() => {
'word': word,
'reading': reading
};
} }
class JishoSenseLink { class JishoSenseLink {
@ -301,6 +304,11 @@ class JishoSenseLink {
url: json['url'] as String url: json['url'] as String
); );
} }
Map<String, dynamic> toJson() => {
'text': text,
'url': url
};
} }
class JishoWordSense { class JishoWordSense {
@ -328,17 +336,29 @@ class JishoWordSense {
factory JishoWordSense.fromJson(Map<String, dynamic> json){ factory JishoWordSense.fromJson(Map<String, dynamic> json){
return JishoWordSense( return JishoWordSense(
english_definitions: json['english_definitions'] as List<String>, english_definitions: (json['english_definitions'] as List).map((result) => result as String).toList(),
parts_of_speech: json['parts_of_speech'] as List<String>, parts_of_speech: (json['parts_of_speech'] as List).map((result) => result as String).toList(),
links: json['links'] as List<JishoSenseLink>, links: (json['links'] as List).map((result) => JishoSenseLink.fromJson(result)).toList(),
tags: json['tags'] as List<String>, tags: (json['tags'] as List).map((result) => result as String).toList(),
see_also: json['see_also'] as List<String>, see_also: (json['see_also'] as List).map((result) => result as String).toList(),
antonyms: json['antonyms'] as List<String>, antonyms: (json['antonyms'] as List).map((result) => result as String).toList(),
source: json['source'] as List<dynamic>, source: json['source'] as List<dynamic>,
info: json['info'] as List<String>, info: (json['info'] as List).map((result) => result as String).toList(),
restrictions: json['restrictions'] as List<dynamic> restrictions: json['restrictions'] as List<dynamic>
); );
} }
Map<String, dynamic> toJson() => {
'english_definitions': english_definitions,
'parts_of_speech': parts_of_speech,
'links': links,
'tags': tags,
'see_also': see_also,
'antonyms': antonyms,
'source': source,
'info': info,
'restrictions': restrictions
};
} }
class JishoAttribution { class JishoAttribution {
@ -353,12 +373,18 @@ class JishoAttribution {
}); });
factory JishoAttribution.fromJson(Map<String, dynamic> json){ factory JishoAttribution.fromJson(Map<String, dynamic> json){
return JishoAttribution( return JishoAttribution( //TODO: This is broken. Find the potential values of a json result and fix
jmdict: json['jmdict'] as bool, jmdict: (json['jmdict'] == 'true'),
jmnedict: json['jmnedict'] as bool, jmnedict: (json['jmnedict'] == 'true'),
dbpedia: json['dbpedia'] as bool dbpedia: (json['dbpedia'] == 'true')
); );
} }
Map<String, dynamic> toJson() => {
'jmdict': jmdict,
'jmnedict': jmnedict,
'dbpedia': dbpedia
};
} }
class JishoResult { class JishoResult {
@ -384,13 +410,23 @@ class JishoResult {
return JishoResult( return JishoResult(
slug: json['slug'] as String, slug: json['slug'] as String,
is_common: json['is_common'] as bool, is_common: json['is_common'] as bool,
tags: json['tags'] as List<String>, tags: (json['tags'] as List).map((result) => result as String).toList(),
jlpt: json['jlpt'] as List<String>, jlpt: (json['jlpt'] as List).map((result) => result as String).toList(),
japanese: json['japanese'] as List<JishoJapaneseWord>, japanese: (json['japanese'] as List).map((result) => JishoJapaneseWord.fromJson(result)).toList(),
senses: json['senses'] as List<JishoWordSense>, senses: (json['senses'] as List).map((result) => JishoWordSense.fromJson(result)).toList(),
attribution: json['attribution'] as JishoAttribution attribution: JishoAttribution.fromJson(json['attribution'])
); );
} }
Map<String, dynamic> toJson() => {
'slug': slug,
'is_common': is_common,
'tags': tags,
'jlpt': jlpt,
'japanese': japanese,
'senses': senses,
'attribution': attribution
};
} }
class JishoResultMeta { class JishoResultMeta {
@ -403,6 +439,10 @@ class JishoResultMeta {
status: json['status'] as int status: json['status'] as int
); );
} }
Map<String, dynamic> toJson() => {
'status': status
};
} }
class JishoAPIResult { class JishoAPIResult {
@ -413,8 +453,13 @@ class JishoAPIResult {
factory JishoAPIResult.fromJson(Map<String, dynamic> json){ factory JishoAPIResult.fromJson(Map<String, dynamic> json){
return JishoAPIResult( return JishoAPIResult(
meta: json['meta'] as JishoResultMeta, meta: JishoResultMeta.fromJson(json['meta']),
data: json['data'] as List<JishoResult> data: (json['data'] as List).map((result) => JishoResult.fromJson(result)).toList()
); );
} }
Map<String, dynamic> toJson() => {
'meta': meta.toJson(),
'data': data
};
} }

View File

@ -20,7 +20,7 @@ class JishoApi {
/// @async /// @async
Future<List<JishoResult>> searchForPhrase(String phrase) async { Future<List<JishoResult>> searchForPhrase(String phrase) async {
final uri = uriForPhraseSearch(phrase); final uri = uriForPhraseSearch(phrase);
final JishoAPIResult jsonData = await http.get(uri).then((response) => jsonDecode(response.body)); final jsonData = await http.get(uri).then((response) => JishoAPIResult.fromJson(jsonDecode(response.body)));
return jsonData.data; return jsonData.data;
} }