Fix JSON encoding

This commit is contained in:
Oystein Kristoffer Tveit 2020-06-15 11:14:03 +02:00
parent cbbd60f4c3
commit aa29d73477
3 changed files with 50 additions and 21 deletions

View File

@ -140,10 +140,12 @@ class KanjiResult {
Map<String, dynamic> toJson() {
if (found == false) return {
if (found == false) {
return {
'query': query,
'found': found
};
}
return {
'query': query,
@ -175,6 +177,13 @@ class ExampleSentencePiece {
this.unlifted = unlifted;
this.lifted = lifted;
}
Map<String, dynamic> toJson() {
return {
'unlifted': unlifted,
'lifted': lifted
};
}
}
class ExampleResultData {
@ -183,12 +192,21 @@ class ExampleResultData {
String english;
List<ExampleSentencePiece> pieces;
ExampleResultData({String kanji, String kana, String english, List<ExampleSentencePiece> pieces}){
ExampleResultData({String english, String kanji, String kana, List<ExampleSentencePiece> pieces}){
this.english = english;
this.kanji = kanji;
this.kana = kana;
this.english = english;
this.pieces = pieces;
}
Map<String, dynamic> toJson() {
return {
'english': english,
'kanji': kanji,
'kana': kana,
'pieces': pieces
};
}
}
class ExampleResults {
@ -198,13 +216,23 @@ class ExampleResults {
List<ExampleResultData> results;
String phrase;
ExampleResults({String query, bool found, String uri, List<ExampleResultData> results, String phrase}){
ExampleResults({String query, bool found, List<ExampleResultData> results, String uri, String phrase}){
this.query = query;
this.found = found;
this.uri = uri;
this.results = results;
this.uri = uri;
this.phrase = phrase;
}
Map<String, dynamic> toJson() {
return {
'query': query,
'found': found,
'results': results,
'uri': uri,
'phrase': phrase
};
}
}

View File

@ -3,6 +3,7 @@ import 'package:http/http.dart' as http;
import 'package:html_unescape/html_unescape.dart' as html_entities;
import 'dart:convert';
import 'package:html/parser.dart';
import 'package:html/dom.dart';
final htmlUnescape = html_entities.HtmlUnescape();
@ -236,7 +237,7 @@ String uriForExampleSearch(String phrase) {
return '${SCRAPE_BASE_URI}${Uri.encodeComponent(phrase)}%23sentences';
}
ExampleResultData getKanjiAndKana(div) {
ExampleResultData getKanjiAndKana(Element div) {
final ul = div.querySelector('ul');
final contents = ul.children;
@ -245,10 +246,10 @@ ExampleResultData getKanjiAndKana(div) {
var kana = '';
for (var i = 0; i < contents.length; i += 1) {
final content = contents[i];
if (content.tagName == 'li') {
if (content.localName == 'li') {
final li = content;
final furigana = li.querySelector('.furigana').text;
final unlifted = li.querySelector('.unlinked').text;
final furigana = li.querySelector('.furigana')?.text;
final unlifted = li.querySelector('.unlinked')?.text;
if (furigana != null) {
kanji += unlifted;
@ -284,21 +285,21 @@ ExampleResultData getKanjiAndKana(div) {
);
}
List<ExampleSentencePiece> getPieces(sentenceElement) {
List<ExampleSentencePiece> getPieces(Element sentenceElement) {
final pieceElements = sentenceElement.querySelectorAll('li.clearfix');
final pieces = [];
final List<ExampleSentencePiece> pieces = [];
for (var pieceIndex = 0; pieceIndex < pieceElements.length; pieceIndex += 1) {
final pieceElement = pieceElements[pieceIndex];
pieces.add(ExampleSentencePiece(
lifted: pieceElement.querySelector('.furigana').text,
unlifted: pieceElement.querySelector('.unlinked').text,
lifted: pieceElement.querySelector('.furigana')?.text,
unlifted: pieceElement.querySelector('.unlinked')?.text,
));
}
return pieces;
}
ExampleResultData parseExampleDiv(div) {
ExampleResultData parseExampleDiv(Element div) {
final result = getKanjiAndKana(div);
result.english = div.querySelector('.english').text;
result.pieces = getPieces(div);
@ -310,7 +311,7 @@ ExampleResults parseExamplePageData(String pageHtml, String phrase) {
final document = parse(pageHtml);
final divs = document.querySelectorAll('.sentence_content');
final results = divs.map((div) => parseExampleDiv(div));
final results = divs.map((div) => parseExampleDiv(div)).toList();
return ExampleResults(
query: phrase,
@ -325,7 +326,7 @@ ExampleResults parseExamplePageData(String pageHtml, String phrase) {
/* PHRASE SCRAPE FUNCTIONS START */
List<String> getTags(document) {
List<String> getTags(Document document) {
final tags = [];
final tagElements = document.querySelectorAll('.concept_light-tag');
@ -337,7 +338,7 @@ List<String> getTags(document) {
return tags;
}
PhrasePageScrapeResult getMeaningsOtherFormsAndNotes(document) {
PhrasePageScrapeResult getMeaningsOtherFormsAndNotes(Document document) {
final returnValues = PhrasePageScrapeResult( otherForms: [], notes: [] );
// const meaningsWrapper = $('#page_container > div > div > article > div > div.concept_light-meanings.medium-9.columns > div');

View File

@ -20,7 +20,7 @@ void runTestCases(List<String> testCaseFiles, Function apiFunction) async {
final testCase = jsonDecode(file);
await test('Test ${testCount}', () async {
final result = await apiFunction(testCase['query']);
expect(result, testCase['expectedResult']);
expect(jsonEncode(result), jsonEncode(testCase['expectedResult']));
});
}
}