From d566821f311a33676aa7a0dc46835036d2d63025 Mon Sep 17 00:00:00 2001 From: Tyler Nickerson Date: Wed, 19 May 2021 15:25:24 -0700 Subject: [PATCH] feat(java): added toJSON method to Entry --- java/main/java/org/odict/Dictionary.java | 57 ++++++++++---------- java/main/java/org/odict/models/Entry.java | 6 +++ java/test/java/org/odict/TestDictionary.java | 1 + 3 files changed, 35 insertions(+), 29 deletions(-) diff --git a/java/main/java/org/odict/Dictionary.java b/java/main/java/org/odict/Dictionary.java index 06c543f5..9f0f1ebe 100644 --- a/java/main/java/org/odict/Dictionary.java +++ b/java/main/java/org/odict/Dictionary.java @@ -1,6 +1,5 @@ package org.odict; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import cz.adamh.utils.NativeUtils; import java.io.*; @@ -56,11 +55,11 @@ public Dictionary(String path, Boolean index) throws IOException { } public Entry lookup(String term) { -System.out.println(Arrays.toString(term.getBytes(StandardCharsets.UTF_8))); + System.out.println(Arrays.toString(term.getBytes(StandardCharsets.UTF_8))); schema.Entry entry = this.dict.entriesByKey(term.toLowerCase()); if (entry == null) { - return null; + return null; } return new Entry(entry); @@ -83,45 +82,45 @@ public String search(String query) { } private schema.Dictionary read(String filePath) throws IOException { - try (FileInputStream fis = new FileInputStream(filePath)) { - try (BufferedInputStream stream = new BufferedInputStream(fis)) { - // Read in signature and validate it - byte[] signature = new byte[5]; + try (FileInputStream fis = new FileInputStream(filePath)) { + try (BufferedInputStream stream = new BufferedInputStream(fis)) { + // Read in signature and validate it + byte[] signature = new byte[5]; - stream.read(signature, 0, 5); + stream.read(signature, 0, 5); - // Validate file signature - if (!new String(signature).equals("ODICT")) { - throw new Error("Invalid ODict file signature"); - } + // Validate file signature + if (!new String(signature).equals("ODICT")) { + throw new Error("Invalid ODict file signature"); + } - // Read in version number - byte[] version_b = new byte[2]; + // Read in version number + byte[] version_b = new byte[2]; - stream.read(version_b); + stream.read(version_b); - short version = ByteBuffer.wrap(version_b).order(ByteOrder.LITTLE_ENDIAN).getShort(); + short version = ByteBuffer.wrap(version_b).order(ByteOrder.LITTLE_ENDIAN).getShort(); - // Read in length of compressed data - byte[] compressed_size_b = new byte[8]; + // Read in length of compressed data + byte[] compressed_size_b = new byte[8]; - stream.read(compressed_size_b); + stream.read(compressed_size_b); - long compressed_size = ByteBuffer.wrap(compressed_size_b).order(ByteOrder.LITTLE_ENDIAN).getLong(); + long compressed_size = ByteBuffer.wrap(compressed_size_b).order(ByteOrder.LITTLE_ENDIAN).getLong(); - // Read in compressed data - byte[] compressed = new byte[(int) compressed_size]; + // Read in compressed data + byte[] compressed = new byte[(int) compressed_size]; - stream.read(compressed); + stream.read(compressed); - // Decompress data - byte[] uncompressed = Snappy.uncompress(compressed); + // Decompress data + byte[] uncompressed = Snappy.uncompress(compressed); - this.version = version; + this.version = version; - // Convert to dictionary and return - return schema.Dictionary.getRootAsDictionary(ByteBuffer.wrap(uncompressed)); - } + // Convert to dictionary and return + return schema.Dictionary.getRootAsDictionary(ByteBuffer.wrap(uncompressed)); } + } } } \ No newline at end of file diff --git a/java/main/java/org/odict/models/Entry.java b/java/main/java/org/odict/models/Entry.java index c56225b3..1cd5bae7 100644 --- a/java/main/java/org/odict/models/Entry.java +++ b/java/main/java/org/odict/models/Entry.java @@ -1,6 +1,8 @@ package org.odict.models; import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.core.JsonProcessingException; import java.util.ArrayList; import java.util.List; @@ -29,4 +31,8 @@ public String getTerm() { public List getEtymologies() { return etymologies; } + + public String toJSON() throws JsonProcessingException { + return new ObjectMapper().writeValueAsString(this); + } } diff --git a/java/test/java/org/odict/TestDictionary.java b/java/test/java/org/odict/TestDictionary.java index 90c8b511..ad7038ec 100644 --- a/java/test/java/org/odict/TestDictionary.java +++ b/java/test/java/org/odict/TestDictionary.java @@ -18,6 +18,7 @@ public void testLookup() throws Exception { Entry entry = dict.lookup("run"); assertNotNull(entry); + assertNotEquals(entry.toJSON(), "{}"); } @Test