Skip to content

Commit ae61d99

Browse files
committed
add file read
1 parent 02c8415 commit ae61d99

File tree

4 files changed

+106
-9
lines changed

4 files changed

+106
-9
lines changed

src/ws/http/test/TestCase.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,21 @@ public static void main(String[] args) throws IOException {
2424
public void testJson()
2525
{
2626
// logger.info( JSON.toJSON(null) );
27-
logger.info( JSON.NULL.toString() );
28-
String json = "{\"account_id\":\"121\",\"channel\":\"ycb\",\"domain\":\"183.131.145.124\",\"port\":\"80\",\"request_id\":\"e54af3f3-d915-4524-8805-3108d78a2220\",\"request_time\":\"1473833813956\",\"timestamp\":1474251393,\"sign\":\"6905d6e800b8bfed60ca02efbe404abb\"}";
29-
JsonValue jo = JSON.parseJSON(json);
30-
logger.info( jo.toString() );
31-
logger.info( JSON.toJSON(jo) );
27+
// logger.info( JSON.NULL.toString() );
28+
// String json = "{\"account_id\":\"121\",\"channel\":\"ycb\",\"domain\":\"183.131.145.124\",\"port\":\"80\",\"request_id\":\"e54af3f3-d915-4524-8805-3108d78a2220\",\"request_time\":\"1473833813956\",\"timestamp\":1474251393,\"sign\":\"6905d6e800b8bfed60ca02efbe404abb\"}";
29+
// JsonValue jo = JSON.parseJSON(json);
30+
// logger.info( jo.toString() );
31+
// logger.info( JSON.toJSON(jo) );
3232
// System.out.println(jo.getStringValue("channel"));
3333
// logger.info(jo.getStringValue("channel"));
34-
logger.info( JSON.object().add("list", JSON.array().add(JSON.object().add("id", 123).add("age", 456))).toString() );
34+
// logger.info( JSON.object().add("list", JSON.array().add(JSON.object().add("id", 123).add("age", 456))).toString() );
3535

3636
// Object jo = JSON.parseJSON("[{\"a\":\"21\"}]");
3737

3838
// System.out.println(jo instanceof JSONObject);
3939

40+
// JsonValue jo = JSON.parseJSONFile("C:/tmptt/postmanv1.json", false);
41+
// logger.info(jo.asObject().get("id").toString());
4042
}
4143

4244
public void testGet() throws IOException

src/ws/http/tools/File.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package ws.http.tools;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.io.InputStreamReader;
7+
import java.net.MalformedURLException;
8+
import java.net.URL;
9+
import java.nio.file.Files;
10+
import java.nio.file.Paths;
11+
import java.nio.file.StandardOpenOption;
12+
13+
public class File {
14+
15+
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
16+
17+
public static final String readFromPath(String path) {
18+
try {
19+
return read(Files.newInputStream(Paths.get(path), StandardOpenOption.READ));
20+
} catch (IOException e) {
21+
e.printStackTrace();
22+
}
23+
return null;
24+
}
25+
26+
public static final String readFromURL(String url) {
27+
try {
28+
return read(new URL(url).openStream());
29+
} catch (MalformedURLException e) {
30+
e.printStackTrace();
31+
} catch (IOException e) {
32+
e.printStackTrace();
33+
}
34+
return null;
35+
}
36+
37+
private static final String read(InputStream is) {
38+
try {
39+
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
40+
StringBuilder builder = new StringBuilder();
41+
String line = null;
42+
while ((line = reader.readLine()) != null) builder.append(line + LINE_SEPARATOR);
43+
reader.close();
44+
System.gc();
45+
return builder.toString().trim();
46+
} catch (IOException e) {
47+
e.printStackTrace();
48+
}
49+
return null;
50+
}
51+
52+
}

src/ws/http/tools/json/JSON.java

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import java.io.IOException;
2525
import java.io.Reader;
2626

27+
import ws.http.tools.File;
28+
2729
/**
2830
* This class serves as the entry point to the minimal-json API.
2931
* <p>
@@ -32,7 +34,7 @@
3234
* </p>
3335
*
3436
* <pre>
35-
* JsonObject object = Json.parse(string).asObject();
37+
* JsonObject object = JSON.parse(string).asObject();
3638
* </pre>
3739
* <p>
3840
* To <strong>create</strong> a JSON data structure to be serialized, use the
@@ -42,7 +44,7 @@
4244
* </p>
4345
*
4446
* <pre>
45-
* String string = Json.object().add("foo", 23).add("bar", true).toString();
47+
* String string = JSON.object().add("foo", 23).add("bar", true).toString();
4648
* </pre>
4749
* <p>
4850
* To create a JSON array from a given Java array, you can use one of the
@@ -51,7 +53,7 @@
5153
*
5254
* <pre>
5355
* String[] names = ...
54-
* JsonArray array = Json.array(names);
56+
* JsonArray array = JSON.array(names);
5557
* </pre>
5658
*/
5759
public final class JSON {
@@ -331,6 +333,37 @@ public static JsonValue parseJSON(Reader reader) throws IOException {
331333
return handler.getValue();
332334
}
333335

336+
/**
337+
* Parses the given input path as JSON
338+
*
339+
* @param path
340+
* @param isUrl
341+
* @return a value that represents the parsed JSON
342+
*/
343+
public static JsonValue parseJSONFile(String path, boolean isUrl) {
344+
if (path == null) {
345+
throw new NullPointerException("path is null");
346+
}
347+
if ( isUrl )
348+
{
349+
return parseJSON(File.readFromURL(path));
350+
}
351+
352+
return parseJSON(File.readFromPath(path));
353+
}
354+
355+
/**
356+
* querying JSONNode objects with varieties of expressions
357+
*
358+
* @param json
359+
* @param queryMl
360+
* @return a value that represents the parsed JSON
361+
*/
362+
public static JsonValue query(JsonValue json, String queryMl)
363+
{
364+
return Query.find(json, queryMl);
365+
}
366+
334367
/**
335368
* Convert an object into a JSON.
336369
*

src/ws/http/tools/json/Query.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package ws.http.tools.json;
2+
3+
public class Query {
4+
5+
public static JsonValue find(JsonValue json, String queryMl)
6+
{
7+
return json;
8+
}
9+
10+
}

0 commit comments

Comments
 (0)