My favorites
▼
|
Sign in
totoe
JSON and XML parser for GWT with json/xpath and namespace support
Project Home
Downloads
Wiki
Issues
Source
READ-ONLY: This project has been
archived
. For more information see
this post
.
Search
Search within:
All issues
Open issues
New issues
Issues to verify
for
Advanced search
Search tips
Subscriptions
Issue
4
attachment: JsonPath.java
(3.4 KB)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package name.pehl.totoe.json.client;
import com.google.gwt.core.client.JavaScriptException;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.json.client.JSONException;
import com.google.gwt.json.client.JSONNull;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONValue;
/**
* Java wrapper for <a
* href="http://www.sitepen.com/blog/2008/03/17/jsonpath-support/" >JSONPath</a>
* expressions.
*
* @see http://code.google.com/p/jsonpath/wiki/ExprSyntax
* @see http://code.google.com/p/jsonpath/wiki/Javascript
* @author $LastChangedBy:$
* @version $LastChangedRevision:$
*/
public final class JsonPath
{
/**
* Private constructor to ensure that the class acts as a true utility class
* i.e. it isn't instantiable and extensible.
*/
private JsonPath()
{
}
/**
* Selects JSON data using this <a
* href="http://www.sitepen.com/blog/2008/03/17/jsonpath-support/"
* >JSONPath</a> library.
*
* @param json
* The json object to select the data from
* @param path
* An JSONPath expression to select some data.
* @return The data selected by the path or {@link JSONNull} if no data was
* found or the path was null.
* @throws JSONException
* If the path expression could not be evaluated.
* @see http://code.google.com/p/jsonpath/wiki/ExprSyntax
* @see http://code.google.com/p/jsonpath/wiki/Javascript
*/
public static JSONValue select(JSONObject json, String path) throws JSONException
{
if (path == null || json == null)
{
return JSONNull.getInstance();
}
try
{
JSONValue value = selectImpl(json.getJavaScriptObject(), path);
return value != null ? value : JSONNull.getInstance();
}
catch (JavaScriptException e)
{
throw new JSONException(e);
}
}
private static native JSONValue selectImpl(JavaScriptObject json, String path)
/*-{
try
{
var data = $wnd.jsonPath(json, path, {evalType:"RESULT", safeEval:true});
if (typeof(data) == "undefined")
{
return null;
}
else if (data instanceof Array || data instanceof $wnd.Array)
{
return @com.google.gwt.json.client.JSONArray::new(Lcom/google/gwt/core/client/JavaScriptObject;)(data);
}
else if (typeof(data) == "boolean")
{
return @com.google.gwt.json.client.JSONBoolean::getInstance(Z)(data);
}
else if (typeof(data) == "number")
{
return @com.google.gwt.json.client.JSONNumber::new(D)(data);
}
else if (typeof(data) == "string")
{
return @com.google.gwt.json.client.JSONString::new(Ljava/lang/String;)(data);
}
else
{
if (!data)
{
return null;
}
// Try as object
return @com.google.gwt.json.client.JSONObject::new(Lcom/google/gwt/core/client/JavaScriptObject;)(data);
}
}
catch (e)
{
throw new Error(e);
}
}-*/;
}
Powered by
Google Project Hosting