Skip to content

Commit 99dea41

Browse files
committed
Added Async toJSONObject method and one test case.
1 parent a1a3fcc commit 99dea41

2 files changed

Lines changed: 90 additions & 1 deletion

File tree

src/main/java/org/json/XML.java

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ of this software and associated documentation files (the "Software"), to deal
4242
* @version 2016-08-10
4343
*/
4444
@SuppressWarnings("boxing")
45-
public class XML {
45+
public class XML{
4646

4747
/** The Character '&'. */
4848
public static final Character AMP = '&';
@@ -78,6 +78,11 @@ public class XML {
7878

7979
public static final String TYPE_ATTR = "xsi:type";
8080

81+
/** [Milestone 5]
82+
* Need to define some global function variables that can be accessed by run function.
83+
*/
84+
//public static Function command, errorHandle;
85+
8186
/**
8287
* Creates an iterator for navigating Code Points in a string instead of
8388
* characters. Once Java7 support is dropped, this can be replaced with
@@ -1072,6 +1077,45 @@ public static Stream<String> toStream(JSONObject obj)
10721077
return (listOfTags).stream();
10731078
}
10741079

1080+
public static void toJSONObject(Reader r, Function<JSONObject, Object> command, boolean diff)
1081+
{
1082+
//Reader r, Function commander, Function errorHandler
1083+
//Take the inputs and store them in global variables for later use in run function
1084+
//command = commander;
1085+
//errorHandle = errorHandler;
1086+
String outstr = "outside the thread rj";
1087+
JSONObject myJSON = toJSONObject(r);
1088+
1089+
Runnable runnable = () -> {
1090+
System.out.println("Inside : " + outstr);
1091+
System.out.println(myJSON.toString());
1092+
command.apply(myJSON);
1093+
};
1094+
1095+
System.out.println("Creating Thread...");
1096+
Thread thread = new Thread(runnable);
1097+
1098+
System.out.println("Starting Thread...");
1099+
thread.start();
1100+
1101+
1102+
//(new Thread(new XML())).start();
1103+
}
1104+
1105+
/*public void run() {
1106+
System.out.println("Hello from a thread!");
1107+
try
1108+
{
1109+
//Convert reader to JSON Object and send off.i
1110+
command.apply();
1111+
1112+
} catch (Exception e)
1113+
{
1114+
System.out.println();
1115+
}
1116+
}*/
1117+
1118+
10751119
/**
10761120
* Convert a JSONObject into a well-formed, element-normal XML string.
10771121
*

src/test/java/org/json/junit/XMLTest_SWE262.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,4 +378,49 @@ public void testStream2()
378378
}
379379
}
380380

381+
@Test
382+
public void async()
383+
{
384+
String expectedStr = "";
385+
//Generate output.txt
386+
try
387+
{
388+
FileReader filereader = new FileReader("src/test/resources/Catalog.xml");
389+
FileWriter filewriter = new FileWriter("output.json");
390+
XML.toJSONObject(filereader, (JSONObject joo) -> joo.write(filewriter), true);
391+
//XML.toJSONObject();
392+
expectedStr = XML.toJSONObject(filereader).toString();
393+
394+
395+
//Give thread adequate time to finish
396+
try{
397+
Thread.sleep(1000);
398+
399+
}catch(InterruptedException e)
400+
{
401+
e.printStackTrace();
402+
}
403+
404+
//filewriter.flush();
405+
filewriter.close();
406+
}catch(IOException e)
407+
{
408+
e.printStackTrace();
409+
}
410+
411+
//Compare the output.txt to the Catalog.xml JSON
412+
try
413+
{
414+
FileReader filereader = new FileReader("output.json");
415+
JSONObject actualJSON = XML.toJSONObject(filereader);
416+
assertEquals(expectedStr, actualJSON.toString());
417+
418+
}catch(IOException e)
419+
{
420+
e.printStackTrace();
421+
}
422+
423+
424+
}
425+
381426
}

0 commit comments

Comments
 (0)