From 0f606b7d1b9342b517dc10654c8aa10939fdeb3f Mon Sep 17 00:00:00 2001 From: h7x4abk3g Date: Tue, 23 Jun 2020 16:55:39 +0200 Subject: [PATCH] Add json definition for API result --- example/phrase_search_example.dart | 10 +++ lib/src/objects.dart | 87 +++++++++++++++++++------- lib/src/unofficial_jisho_api_base.dart | 2 +- 3 files changed, 77 insertions(+), 22 deletions(-) create mode 100644 example/phrase_search_example.dart diff --git a/example/phrase_search_example.dart b/example/phrase_search_example.dart new file mode 100644 index 0000000..831b4ea --- /dev/null +++ b/example/phrase_search_example.dart @@ -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)); + }); +} \ No newline at end of file diff --git a/lib/src/objects.dart b/lib/src/objects.dart index 929204a..dc6c9cf 100644 --- a/lib/src/objects.dart +++ b/lib/src/objects.dart @@ -258,8 +258,7 @@ class PhrasePageScrapeResult { this.notes }); - Map toJson() => - { + Map toJson() => { 'found': found, 'query': query, 'uri': uri, @@ -285,8 +284,12 @@ class JishoJapaneseWord { word: json['word'] as String, reading: json['reading'] as String ); - } + + Map toJson() => { + 'word': word, + 'reading': reading + }; } class JishoSenseLink { @@ -301,6 +304,11 @@ class JishoSenseLink { url: json['url'] as String ); } + + Map toJson() => { + 'text': text, + 'url': url + }; } class JishoWordSense { @@ -328,17 +336,29 @@ class JishoWordSense { factory JishoWordSense.fromJson(Map json){ return JishoWordSense( - english_definitions: json['english_definitions'] as List, - parts_of_speech: json['parts_of_speech'] as List, - links: json['links'] as List, - tags: json['tags'] as List, - see_also: json['see_also'] as List, - antonyms: json['antonyms'] as List, + english_definitions: (json['english_definitions'] as List).map((result) => result as String).toList(), + parts_of_speech: (json['parts_of_speech'] as List).map((result) => result as String).toList(), + links: (json['links'] as List).map((result) => JishoSenseLink.fromJson(result)).toList(), + tags: (json['tags'] as List).map((result) => result as String).toList(), + see_also: (json['see_also'] as List).map((result) => result as String).toList(), + antonyms: (json['antonyms'] as List).map((result) => result as String).toList(), source: json['source'] as List, - info: json['info'] as List, + info: (json['info'] as List).map((result) => result as String).toList(), restrictions: json['restrictions'] as List ); } + + Map 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 { @@ -353,12 +373,18 @@ class JishoAttribution { }); factory JishoAttribution.fromJson(Map json){ - return JishoAttribution( - jmdict: json['jmdict'] as bool, - jmnedict: json['jmnedict'] as bool, - dbpedia: json['dbpedia'] as bool + return JishoAttribution( //TODO: This is broken. Find the potential values of a json result and fix + jmdict: (json['jmdict'] == 'true'), + jmnedict: (json['jmnedict'] == 'true'), + dbpedia: (json['dbpedia'] == 'true') ); } + + Map toJson() => { + 'jmdict': jmdict, + 'jmnedict': jmnedict, + 'dbpedia': dbpedia + }; } class JishoResult { @@ -384,13 +410,23 @@ class JishoResult { return JishoResult( slug: json['slug'] as String, is_common: json['is_common'] as bool, - tags: json['tags'] as List, - jlpt: json['jlpt'] as List, - japanese: json['japanese'] as List, - senses: json['senses'] as List, - attribution: json['attribution'] as JishoAttribution + tags: (json['tags'] as List).map((result) => result as String).toList(), + jlpt: (json['jlpt'] as List).map((result) => result as String).toList(), + japanese: (json['japanese'] as List).map((result) => JishoJapaneseWord.fromJson(result)).toList(), + senses: (json['senses'] as List).map((result) => JishoWordSense.fromJson(result)).toList(), + attribution: JishoAttribution.fromJson(json['attribution']) ); } + + Map toJson() => { + 'slug': slug, + 'is_common': is_common, + 'tags': tags, + 'jlpt': jlpt, + 'japanese': japanese, + 'senses': senses, + 'attribution': attribution + }; } class JishoResultMeta { @@ -403,6 +439,10 @@ class JishoResultMeta { status: json['status'] as int ); } + + Map toJson() => { + 'status': status + }; } class JishoAPIResult { @@ -413,8 +453,13 @@ class JishoAPIResult { factory JishoAPIResult.fromJson(Map json){ return JishoAPIResult( - meta: json['meta'] as JishoResultMeta, - data: json['data'] as List + meta: JishoResultMeta.fromJson(json['meta']), + data: (json['data'] as List).map((result) => JishoResult.fromJson(result)).toList() ); } + + Map toJson() => { + 'meta': meta.toJson(), + 'data': data + }; } \ No newline at end of file diff --git a/lib/src/unofficial_jisho_api_base.dart b/lib/src/unofficial_jisho_api_base.dart index 233af1f..d7c36fb 100644 --- a/lib/src/unofficial_jisho_api_base.dart +++ b/lib/src/unofficial_jisho_api_base.dart @@ -20,7 +20,7 @@ class JishoApi { /// @async Future> searchForPhrase(String phrase) async { 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; }