Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README-M4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Milestone 4 – Stream support for **JSONObject**

## What is Added

| Item | Description |
|------|-------------|
| `JSONObject.JSONNode` | Immutable leaf wrapper containing an absolute `path` and its `value`. |
| `Stream<JSONNode> JSONObject.toStream()` | Depth-first, **lazy** flattening of any `JSONObject/JSONArray` into a `Stream`. Only leaf nodes are emitted (low memory footprint). |

Path conventions
* Object keys: `/parent/child`
* Array items: `/array[0]/child`

---

## Run the test class
```bash
mvn -Dtest=org.json.junit.milestone4.tests.JSONObjectStreamTest test
62 changes: 62 additions & 0 deletions src/main/java/org/json/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.util.*;
import java.util.Map.Entry;
import java.util.regex.Pattern;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
* A JSONObject is an unordered collection of name/value pairs. Its external
Expand Down Expand Up @@ -3030,4 +3032,64 @@ private static String removeLeadingZerosOfNumber(String value){
if (negativeFirstChar) {return "-0";}
return "0";
}

// ---------------------------- Milestone 4 ------------------------------

/**
* Represents a node in the JSON object tree, with path and value.
*/
public static class JSONNode {
private final String path;
private final Object value;

public JSONNode(String path, Object value) {
this.path = path;
this.value = value;
}

public String getPath() {
return path;
}

public Object getValue() {
return value;
}

@Override
public String toString() {
return "JSONNode{path='" + path + "', value=" + value + '}';
}
}

/**
* Convert this JSONObject into a stream of JSONNode, allowing chained operations.
* @return Stream of JSONNode objects (each has a full path and a value)
*/
public Stream<JSONNode> toStream() {
return toStream("", this);
}

/**
* Recursive helper method to flatten JSONObject/JSONArray into JSONNode stream.
*/
private Stream<JSONNode> toStream(String path, Object value) {
if (value instanceof JSONObject) {
JSONObject obj = (JSONObject) value;
return obj.keySet().stream()
.flatMap(key -> {
String newPath = path.isEmpty() ? "/" + key : path + "/" + key;
return toStream(newPath, obj.get(key));
});
} else if (value instanceof JSONArray) {
JSONArray array = (JSONArray) value;
return IntStream.range(0, array.length())
.boxed()
.flatMap(i -> {
String newPath = path + "[" + i + "]";
return toStream(newPath, array.get(i));
});
} else {
return Stream.of(new JSONNode(path, value));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package org.json.junit.milestone4.tests;

import org.json.JSONObject;
import org.json.JSONObject.JSONNode;
import org.json.XML;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Optional;
import java.util.Scanner;
import java.util.stream.Collectors;

import static org.junit.Assert.*;

/**
* Unit tests for the JSONObject.toStream() extension (Milestone 4) – JUnit 4 / Java 8.
*/
public class JSONObjectStreamTest {

private static JSONObject catalog;

@BeforeClass
public static void loadXml() throws Exception {
try (InputStream in = JSONObjectStreamTest.class.getResourceAsStream("/books.xml")) {
assertNotNull("books.xml must be on the class-path (src/test/resources)", in);

String xml;
try (Scanner scanner = new Scanner(in, StandardCharsets.UTF_8.name())) {
scanner.useDelimiter("\\A");
xml = scanner.hasNext() ? scanner.next() : "";
}

catalog = XML.toJSONObject(xml);
}
}

@Test
public void extractTitles() {
List<String> titles = catalog.toStream()
.filter(n -> n.getPath().endsWith("/title"))
.map(n -> n.getValue().toString())
.collect(Collectors.toList());

assertEquals(12, titles.size());
assertTrue(titles.contains("XML Developer's Guide"));
assertTrue(titles.contains("Visual Studio 7: A Comprehensive Guide"));
}

@Test
public void findExactPath() {
String wantedPath = "/catalog/book[0]/author";
Optional<JSONNode> node = catalog.toStream()
.filter(n -> n.getPath().equals(wantedPath))
.findFirst();

assertTrue(node.isPresent());
assertEquals("Gambardella, Matthew", node.get().getValue());
}

@Test
public void filterCheapBooks() {
List<JSONNode> cheapPrices = catalog.toStream()
.filter(n -> n.getPath().endsWith("/price"))
.filter(n -> Double.parseDouble(n.getValue().toString()) < 10.0)
.collect(Collectors.toList());

assertEquals(8, cheapPrices.size());
assertTrue(cheapPrices.stream()
.allMatch(n -> Double.parseDouble(n.getValue().toString()) < 10.0));
}
}
120 changes: 120 additions & 0 deletions src/test/resources/books.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
<book id="bk103">
<author>Corets, Eva</author>
<title>Maeve Ascendant</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-11-17</publish_date>
<description>After the collapse of a nanotechnology
society in England, the young survivors lay the
foundation for a new society.</description>
</book>
<book id="bk104">
<author>Corets, Eva</author>
<title>Oberon's Legacy</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2001-03-10</publish_date>
<description>In post-apocalypse England, the mysterious
agent known only as Oberon helps to create a new life
for the inhabitants of London. Sequel to Maeve
Ascendant.</description>
</book>
<book id="bk105">
<author>Corets, Eva</author>
<title>The Sundered Grail</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2001-09-10</publish_date>
<description>The two daughters of Maeve, half-sisters,
battle one another for control of England. Sequel to
Oberon's Legacy.</description>
</book>
<book id="bk106">
<author>Randall, Cynthia</author>
<title>Lover Birds</title>
<genre>Romance</genre>
<price>4.95</price>
<publish_date>2000-09-02</publish_date>
<description>When Carla meets Paul at an ornithology
conference, tempers fly as feathers get ruffled.</description>
</book>
<book id="bk107">
<author>Thurman, Paula</author>
<title>Splish Splash</title>
<genre>Romance</genre>
<price>4.95</price>
<publish_date>2000-11-02</publish_date>
<description>A deep sea diver finds true love twenty
thousand leagues beneath the sea.</description>
</book>
<book id="bk108">
<author>Knorr, Stefan</author>
<title>Creepy Crawlies</title>
<genre>Horror</genre>
<price>4.95</price>
<publish_date>2000-12-06</publish_date>
<description>An anthology of horror stories about roaches,
centipedes, scorpions and other insects.</description>
</book>
<book id="bk109">
<author>Kress, Peter</author>
<title>Paradox Lost</title>
<genre>Science Fiction</genre>
<price>6.95</price>
<publish_date>2000-11-02</publish_date>
<description>After an inadvertant trip through a Heisenberg
Uncertainty Device, James Salway discovers the problems
of being quantum.</description>
</book>
<book id="bk110">
<author>O'Brien, Tim</author>
<title>Microsoft .NET: The Programming Bible</title>
<genre>Computer</genre>
<price>36.95</price>
<publish_date>2000-12-09</publish_date>
<description>Microsoft's .NET initiative is explored in
detail in this deep programmer's reference.</description>
</book>
<book id="bk111">
<author>O'Brien, Tim</author>
<title>MSXML3: A Comprehensive Guide</title>
<genre>Computer</genre>
<price>36.95</price>
<publish_date>2000-12-01</publish_date>
<description>The Microsoft MSXML3 parser is covered in
detail, with attention to XML DOM interfaces, XSLT processing,
SAX and more.</description>
</book>
<book id="bk112">
<author>Galos, Mike</author>
<title>Visual Studio 7: A Comprehensive Guide</title>
<genre>Computer</genre>
<price>49.95</price>
<publish_date>2001-04-16</publish_date>
<description>Microsoft Visual Studio 7 is explored in depth,
looking at how Visual Basic, Visual C++, C#, and ASP+ are
integrated into a comprehensive development
environment.</description>
</book>
</catalog>
Loading