-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding JSON Iterator and removing unnecessary imports
- Loading branch information
1 parent
a08013f
commit 94bc0a7
Showing
4 changed files
with
188 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
jaggr/jaggr-utils/src/main/java/com/caffinc/jaggr/utils/JsonIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package com.caffinc.jaggr.utils; | ||
|
||
import com.google.gson.Gson; | ||
|
||
import java.io.*; | ||
import java.util.HashMap; | ||
import java.util.Iterator; | ||
import java.util.Map; | ||
import java.util.NoSuchElementException; | ||
|
||
/** | ||
* Iterates a JSON file | ||
* | ||
* @author Sriram | ||
* @since 11/27/2016 | ||
*/ | ||
public class JsonIterator implements Iterator<Map<String, Object>>, Closeable { | ||
private final BufferedReader bufferedReader; | ||
private String cachedLine; | ||
private boolean finished = false; | ||
private Gson gson = new Gson(); | ||
|
||
/** | ||
* Constructs an iterator of the lines for a <code>fileName</code>. | ||
* | ||
* @param fileName the <code>fileName</code> to read from | ||
* @throws IOException thrown if there is a problem accessing the file | ||
*/ | ||
public JsonIterator(final String fileName) throws IOException { | ||
this(new BufferedReader(new FileReader(fileName))); | ||
} | ||
|
||
/** | ||
* Constructs an iterator of the lines for a <code>Reader</code>. | ||
* | ||
* @param reader the <code>Reader</code> to read from, not null | ||
* @throws IllegalArgumentException if the reader is null | ||
*/ | ||
public JsonIterator(final Reader reader) throws IllegalArgumentException { | ||
if (reader == null) { | ||
throw new IllegalArgumentException("Reader must not be null"); | ||
} | ||
if (reader instanceof BufferedReader) { | ||
bufferedReader = (BufferedReader) reader; | ||
} else { | ||
bufferedReader = new BufferedReader(reader); | ||
} | ||
} | ||
|
||
/** | ||
* Indicates whether the <code>Reader</code> has more lines. | ||
* If there is an <code>IOException</code> then {@link #close()} will | ||
* be called on this instance. | ||
* | ||
* @return {@code true} if the Reader has more lines | ||
* @throws IllegalStateException if an IO exception occurs | ||
*/ | ||
public boolean hasNext() { | ||
if (cachedLine != null) { | ||
return true; | ||
} else if (finished) { | ||
return false; | ||
} else { | ||
try { | ||
while (true) { | ||
final String line = bufferedReader.readLine(); | ||
if (line == null) { | ||
finished = true; | ||
return false; | ||
} | ||
cachedLine = line; | ||
return true; | ||
} | ||
} catch (final IOException ioe) { | ||
close(); | ||
throw new IllegalStateException(ioe); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Returns the next object in the file or wrapped <code>Reader</code>. | ||
* | ||
* @return the next JSON object from the input | ||
* @throws NoSuchElementException if there is no object to return | ||
*/ | ||
public Map<String, Object> next() { | ||
return nextObject(); | ||
} | ||
|
||
/** | ||
* Returns the next object in the file or wrapped <code>Reader</code>. | ||
* | ||
* @return the next JSON object from the input | ||
* @throws NoSuchElementException if there is no object to return | ||
*/ | ||
public Map<String, Object> nextObject() { | ||
if (!hasNext()) { | ||
throw new NoSuchElementException("No more objects"); | ||
} | ||
final String currentLine = cachedLine; | ||
cachedLine = null; | ||
return gson.fromJson(currentLine, HashMap.class); | ||
} | ||
|
||
/** | ||
* Closes the underlying <code>Reader</code> quietly. | ||
* This method is useful if you only want to process the first few | ||
* lines of a larger file. If you do not close the iterator | ||
* then the <code>Reader</code> remains open. | ||
* This method can safely be called multiple times. | ||
*/ | ||
public void close() { | ||
finished = true; | ||
try { | ||
bufferedReader.close(); | ||
} catch (final IOException ioe) { | ||
// ignore | ||
} | ||
cachedLine = null; | ||
} | ||
|
||
/** | ||
* Unsupported. | ||
* | ||
* @throws UnsupportedOperationException always | ||
*/ | ||
public void remove() { | ||
throw new UnsupportedOperationException("Remove unsupported on JsonIterator"); | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
jaggr/jaggr-utils/src/test/java/com/caffinc/jaggr/utils/JsonIteratorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.caffinc.jaggr.utils; | ||
|
||
import com.google.gson.Gson; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.io.BufferedWriter; | ||
import java.io.FileWriter; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.*; | ||
|
||
/** | ||
* Tests for the JsonIterator | ||
* | ||
* @author Sriram | ||
* @since 11/27/2016 | ||
*/ | ||
public class JsonIteratorTest { | ||
private static final String TEMP_DIR = System.getProperty("java.io.tmpdir"); | ||
private static final Random RANDOM = new Random(); | ||
private static final Gson GSON = new Gson(); | ||
|
||
@Test | ||
public void testJsonFileIterator() throws Exception { | ||
Path tempFilePath = Paths.get(TEMP_DIR, "jsontest" + RANDOM.nextInt() + ".json"); | ||
try { | ||
List<Map<String, Object>> expectedData = new ArrayList<>(); | ||
try (BufferedWriter br = new BufferedWriter(new FileWriter(tempFilePath.toFile())) | ||
) { | ||
for (int i = 0; i < 10; i++) { | ||
Map<String, Object> json = new HashMap<>(); | ||
json.put("_id", (double) i); | ||
json.put("val", RANDOM.nextDouble()); | ||
expectedData.add(json); | ||
br.write(GSON.toJson(json) + "\n"); | ||
} | ||
} | ||
try (JsonIterator jsonIterator = new JsonIterator(tempFilePath.toString())) { | ||
for (Map<String, Object> expected : expectedData) { | ||
Map<String, Object> actual = jsonIterator.next(); | ||
Assert.assertEquals("Value should match value written to file", expected, actual); | ||
} | ||
} | ||
} finally { | ||
Files.delete(tempFilePath); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters