Skip to content

Commit 51fc340

Browse files
hansonrhansonr
authored andcommitted
more JSON support as swingjs/json with JSUtil.parseJSON$O
1 parent e277a21 commit 51fc340

File tree

5 files changed

+823
-16
lines changed

5 files changed

+823
-16
lines changed

sources/net.sf.j2s.java.core/src/javajs/util/AjaxURLConnection.java

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.io.InputStream;
66
import java.net.HttpURLConnection;
77
import java.net.URL;
8-
import java.net.URLConnection;
98
import java.util.Hashtable;
109
import java.util.Map;
1110

@@ -20,11 +19,14 @@ public class AjaxURLConnection extends HttpURLConnection {
2019

2120
protected AjaxURLConnection(URL url) {
2221
super(url);
22+
ajax = /** @j2sNative url.ajax || */ null;
2323
}
2424

2525
byte[] bytesOut;
2626
String postOut = "";
2727

28+
private Object ajax;
29+
2830
/**
2931
*
3032
* doAjax() is where the synchronous call to AJAX is to happen. or at least
@@ -48,7 +50,14 @@ protected AjaxURLConnection(URL url) {
4850
@SuppressWarnings("null")
4951
private Object doAjax(boolean isBinary) {
5052
J2SObjectInterface J2S = /** @j2sNative self.J2S || */ null;
51-
Object info = (/** @j2sNative {isBinary: isBinary } || */null);
53+
Object info = null;
54+
/** @j2sNative
55+
*
56+
* info = this.ajax || {};
57+
* if (!info.dataType) {
58+
* info.isBinary = !!isBinary;
59+
* }
60+
*/
5261
Object result = J2S.doAjax(url.toString(), postOut, bytesOut, info);
5362
boolean isEmpty = false;
5463
// the problem is that jsmol.php is still returning crlf even if output is 0 bytes
@@ -91,7 +100,7 @@ private InputStream getInputStreamAndResponse(URL url, boolean allowNWError) {
91100
BufferedInputStream is = getAttachedStreamData(url, false);
92101
if (is != null || getUseCaches() && (is = getCachedStream(url, allowNWError)) != null)
93102
return is;
94-
is = attachStreamData(url, doAjax(true));
103+
is = attachStreamData(url, doAjax(ajax == null));
95104
if (getUseCaches() && is != null)
96105
setCachedStream(url);
97106
isNetworkError(is);
@@ -104,17 +113,35 @@ private BufferedInputStream getCachedStream(URL url, boolean allowNWError) {
104113
Object data = urlCache.get(url.toString());
105114
if (data == null)
106115
return null;
107-
BufferedInputStream bis = Rdr.toBIS(data);
116+
boolean isAjax = /** @j2sNative url.ajax || */false;
117+
BufferedInputStream bis = getBIS(data, isAjax);
108118
return (allowNWError || !isNetworkError(bis) ? bis : null);
109119
}
110120

121+
private static BufferedInputStream getBIS(Object data, boolean isAjax) {
122+
if (data == null)
123+
return null;
124+
if (!isAjax)
125+
return Rdr.toBIS(data);
126+
BufferedInputStream bis = Rdr.toBIS("");
127+
/**
128+
* @j2sNative
129+
*
130+
* bis._ajaxData = data;
131+
*/
132+
return bis;
133+
}
134+
111135
private void setCachedStream(URL url) {
112136
Object data = url._streamData;
113137
if (data != null)
114138
urlCache.put(url.toString(), data);
115139
}
116140

141+
@SuppressWarnings("unused")
117142
private boolean isNetworkError(BufferedInputStream is) {
143+
if (/** @j2sNative is._ajaxData || */ false)
144+
return false;
118145
is.mark(15);
119146
byte[] bytes = new byte[13];
120147
try {
@@ -138,29 +165,36 @@ private boolean isNetworkError(BufferedInputStream is) {
138165
* the first time in Java is usually just to see if it exists.
139166
*
140167
* @param url
141-
* @return String, SB, or byte[]
168+
* @return String, SB, or byte[], or JSON dat
142169
*/
170+
@SuppressWarnings("unused")
143171
public static BufferedInputStream getAttachedStreamData(URL url, boolean andDelete) {
144172

145173
Object data = null;
174+
boolean isAjax = false;
146175
/**
147176
* @j2sNative
148-
*
149177
* data = url._streamData;
150178
* if (andDelete) url._streamData = null;
179+
* isAjax = (data && url.ajax && url.ajax.dataType == "json")
151180
*/
152-
return (data == null ? null : Rdr.toBIS(data));
181+
return getBIS(data, isAjax);
153182
}
154183

184+
/**
185+
*
186+
* @param url
187+
* @param o
188+
* @return InputStream or possibly a wrapper for an empty string, but also with JSON data.
189+
*/
155190
public static BufferedInputStream attachStreamData(URL url, Object o) {
156-
157-
/**
158-
* @j2sNative
159-
*
160-
* url._streamData = o;
161-
*/
162-
163-
return (o == null ? null : Rdr.toBIS(o));
191+
/**
192+
* @j2sNative
193+
*
194+
* url._streamData = o;
195+
*/
196+
197+
return getBIS(o, /** @j2sNative url.ajax || */false);
164198
}
165199

166200
/**

sources/net.sf.j2s.java.core/src/swingjs/JSUtil.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package swingjs;
22

3-
import java.awt.Container;
43
import java.awt.JSComponent;
54
import java.awt.Toolkit;
65
import java.io.BufferedInputStream;
6+
import java.io.BufferedReader;
77
import java.io.File;
88
import java.io.IOException;
99
import java.io.InputStream;
@@ -21,6 +21,7 @@
2121
import swingjs.api.Interface;
2222
import swingjs.api.js.J2SInterface;
2323
import swingjs.api.js.JQuery;
24+
import swingjs.json.JSON;
2425

2526
public class JSUtil {
2627

@@ -314,6 +315,10 @@ public static JQuery getJQuery() {
314315

315316
public static JQuery jQuery = getJQuery();
316317

318+
public static Object parseJSONRaw(String json) {
319+
return getJQuery().parseJSON(json);
320+
}
321+
317322
public static String getStackTrace(int n) {
318323
return /** @j2sNative Clazz._getStackTrace(n) || */null;
319324
}
@@ -540,4 +545,21 @@ public static String prompt(String msg, String defaultRet) {
540545
}
541546
}
542547

548+
public static void setAjax(Object url) {
549+
setAjax("url", url, "dataType", "json", "async", Boolean.FALSE);
550+
}
551+
552+
public static Object setAjax(Object... params) {
553+
return params.length == 1 ? JSON.setAjax((URL) params[0]) : JSON.setAjax(params);
554+
}
555+
556+
public static Object parseJSON(Object o) {
557+
return JSON.parse(o);
558+
}
559+
560+
public static BufferedReader getJSONReader(Object is) {
561+
return JSON.getJSONReader((InputStream) is);
562+
}
563+
543564
}
565+

0 commit comments

Comments
 (0)