本文整理汇总了Java中sagex.remote.json.JSONObject类的典型用法代码示例。如果您正苦于以下问题:Java JSONObject类的具体用法?Java JSONObject怎么用?Java JSONObject使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JSONObject类属于sagex.remote.json包,在下文中一共展示了JSONObject类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testOnSystemMessage
import sagex.remote.json.JSONObject; //导入依赖的package包/类
@Test
public void testOnSystemMessage() throws Exception {
LogProvider.useSystemOut();
LogProvider.getLogger(PushBulletPlugin.class).debug("Logging Initialized");
// setup the event message
PushBulletPlugin plugin = spy(new PushBulletPlugin(mock(SageTVPluginRegistry.class)));
// just in case we are using a rest test
// doReturn(getApiKey()).when(plugin).getApiKey();
// normally we just prevent the pushbullet call
doNothing().when(plugin).sendMessage(any(JSONObject.class));
// use the test send command
plugin.onTestSend();
// verify that the message was sent
verify(plugin, times(1)).sendMessage(any(JSONObject.class));
}
示例2: encodeError
import sagex.remote.json.JSONObject; //导入依赖的package包/类
public String encodeError(Exception e) {
try {
JSONObject jo = new JSONObject();
jo.put("sagexVersion", sagex.api.Version.GetVersion());
jo.put("error", e.getMessage());
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
pw.flush();
jo.put("exception", sw.toString());
StringWriter sw2 = new StringWriter();
jo.write(sw2);
return sw2.toString();
} catch (Exception ex) {
return "{error: " + e.getMessage() + "}";
}
}
示例3: getState
import sagex.remote.json.JSONObject; //导入依赖的package包/类
/**
* Returns the current state of the view.
*
* @param folder
* @return
* @throws IOException
*/
public String getState(ViewFolder folder) throws IOException {
JSONObject viewState = new JSONObject();
try {
ViewFactory fact = folder.getViewFactory();
viewState.put("view", fact.getName());
JSONArray presentations = new JSONArray();
if (fact.getViewPresentations().size() > 0) {
List<ViewPresentation> list = new ArrayList<ViewPresentation>(fact.getViewPresentations());
for (int i = 0; i < list.size(); i++) {
presentations.put(serializePresentation(list.get(i)));
}
}
viewState.put("presentations", presentations);
} catch (JSONException e) {
throw new IOException("Failed to store ");
}
return viewState.toString();
}
示例4: setState
import sagex.remote.json.JSONObject; //导入依赖的package包/类
/**
* Applies the given state, as created with getState to the view.
*
* @param state
* @param folder
*/
public void setState(String state, ViewFolder folder) {
try {
JSONObject jo = new JSONObject(state);
ViewFactory f = folder.getViewFactory();
JSONArray presArr = jo.optJSONArray("presentations");
if (presArr != null) {
for (int i = 0; i < presArr.length(); i++) {
applyPresentation(i, presArr.getJSONObject(i), f);
}
}
folder.setChanged();
} catch (Exception e) {
log.warn("Failed to apply state: " + state);
}
}
示例5: applyPresentationState
import sagex.remote.json.JSONObject; //导入依赖的package包/类
private void applyPresentationState(String name, JSONArray set, List<? extends BaseConfigurable> config) {
if (set == null || set.length() == 0 || config == null || config.size() == 0 || set.length() != config.size()) {
log.warn("Unable to apply '" + name + "' state since state and current configuration does not match; set "
+ ((set == null) ? "is null" : "size " + set.length()) + "; config "
+ ((config == null) ? "is null" : "size " + config.size()));
return;
}
// log.info("name: " + name + " size: " + set.length());
for (int i = 0; i < set.length(); i++) {
BaseConfigurable bc = config.get(i);
JSONObject state = set.optJSONObject(i);
if (state != null) {
// log.info("Applying State: " + name + " for " + state);
applyOptions(state.optJSONArray("options"), bc);
}
}
}
示例6: serializeConfigurable
import sagex.remote.json.JSONObject; //导入依赖的package包/类
private JSONObject serializeConfigurable(BaseConfigurable conf) throws JSONException {
JSONObject jo = new JSONObject();
jo.put("name", ((HasName) conf).getName());
JSONArray optArr = new JSONArray();
jo.put("options", optArr);
for (String s : conf.getOptionNames()) {
if ("name".equals(s))
continue;
ConfigurableOption co = conf.getOption(s);
if (!StringUtils.isEmpty(co.value().getValue())) {
JSONObject opt = new JSONObject();
opt.put("name", co.getName());
opt.put("value", co.value().getValue());
optArr.put(opt);
}
}
return jo;
}
示例7: get
import sagex.remote.json.JSONObject; //导入依赖的package包/类
public static <T> T get(String key, JSONObject jo) {
if (key==null) return null;
if (jo==null) return null;
try {
String keyParts[] = key.split("\\s*\\.\\s*");
if (keyParts.length == 1) {
return (T) jo.get(key);
} else {
for (int i = 0; i < keyParts.length - 1; i++) {
jo = jo.getJSONObject(keyParts[i]);
}
return (T) jo.get(keyParts[keyParts.length - 1]);
}
} catch (Throwable t) {
t.printStackTrace();
return null;
}
}
示例8: updateQueryFromJSON
import sagex.remote.json.JSONObject; //导入依赖的package包/类
public void updateQueryFromJSON(SearchQuery query, String data) throws Exception {
JSONObject jo = new JSONObject(data);
for (Iterator i = jo.keys(); i.hasNext(); ) {
String k = (String) i.next();
String v = jo.getString(k);
if ("MediaType".equalsIgnoreCase(k)) {
query.setMediaType(MediaType.toMediaType(v));
} else {
SearchQuery.Field f = mappedFields.get(k.toLowerCase());
if (f == null) {
throw new JSONException("Invalid Field: " + k);
}
log.debug("Setting Query Field via json args: " + f + " = " + v);
query.set(f, v);
}
}
}
示例9: testVIEWSWithRange
import sagex.remote.json.JSONObject; //导入依赖的package包/类
@Test
public void testVIEWSWithRange() throws Exception {
VirtualMediaFolder f = new VirtualMediaFolder(null, "virtfolder", "folder", "Root Folder");
for (int i = 0; i < 10; i++) {
VirtualMediaFile file = new VirtualMediaFile("File " + (i + 1));
f.addMediaResource(file);
}
assertEquals(10, f.getChildren().size());
RemoteContext.get().reset();
RemoteContext.get().setData("start", 1);
RemoteContext.get().setData("end", 5);
if (((Integer) RemoteContext.get().getData("end")) > 0) {
RemoteContext.get().setData("useranges", true);
}
JSONObject jo = new JSONObject(RemoteAPI.createGson(true).toJson(f));
System.out.println(jo.toString(3));
assertEquals(jo.optJSONArray("children").length(), 4);
assertEquals(jo.optJSONArray("children").optJSONObject(0).optString("title"), "File 2");
assertEquals(jo.optJSONArray("children").optJSONObject(3).optString("title"), "File 5");
}
示例10: testUTF8Characters
import sagex.remote.json.JSONObject; //导入依赖的package包/类
@Test
public void testUTF8Characters() throws Exception {
String title = "“ ” ‘ ’ – — … ‐ ‒ ° © ® ™ • ½ ¼ ¾ ⅓ ⅔ † ‡ µ ¢ £ € « » ♠ ♣ ♥ ♦ ¿ �";
VirtualMediaFile vmf = new VirtualMediaFile(title);
ByteArrayOutputStream output = new ByteArrayOutputStream();
PrintWriter pw = new PrintWriter(output);
Command cmd = mock(Command.class);
IOContext ctx = mock(IOContext.class);
when(cmd.getIOContext()).thenReturn(ctx);
when(ctx.getOutputStream()).thenReturn(output);
when(ctx.getWriter()).thenReturn(pw);
RemoteAPI api = new RemoteAPI();
api.encode(cmd, vmf);
InputStream input = new ByteArrayInputStream(output.toByteArray());
// NOTE: Applications consuming the stream MUST utf-8 decode it as well
JSONObject jo = new JSONObject(IOUtils.toString(input, "UTF-8"));
assertEquals(title, jo.getJSONObject("reply").getString("title"));
}
示例11: sendMessage
import sagex.remote.json.JSONObject; //导入依赖的package包/类
void sendMessage(JSONObject pb) throws IOException {
RequestBuilder builder = new RequestBuilder(PUSHBULLET_URL);
builder.setContentType("application/json");
builder.addHeader("Authorization", "Bearer " + getApiKey());
builder.setBody(pb.toString());
builder.postRequest();
}
示例12: dumpSageApi
import sagex.remote.json.JSONObject; //导入依赖的package包/类
private void dumpSageApi(JSONObject o, PrintWriter pw) throws Exception {
pw.println("<tr><td colspan=2><h2>"+o.getString("name")+"</h2></td></tr>");
pw.println("<tr><td class=l>Comment</td><td><pre>" + formatComment(o.getString("comment")) + "</pre></td></tr>");
pw.println("<tr><td class=l>Args</td><td><ul>");
JSONArray arr = o.getJSONArray("args");
for (int i=0;i<arr.length();i++) {
JSONObject arg = arr.getJSONObject(i);
pw.println("<li><pre>" + (i+1) + "; " + arg.getString("varName") + ":" + arg.getString("dataType") + "</pre></li>");
}
pw.println();
pw.println("</ul></td></tr>");
pw.println("<tr><td class=l>Return</td><td>" + o.getString("returnType") + "</td></tr>");
pw.println("<tr><td colspan=2><hr/></td></tr>");
}
示例13: parseScale
import sagex.remote.json.JSONObject; //导入依赖的package包/类
private static int[] parseScale(JSONObject json) {
if (json==null) return null;
String name = json.optString("name");
if (name==null||!name.equals("scale")) return null;
int x = json.optInt("width", -1);
int y = json.optInt("height", -1);
return new int[] {x,y};
}
示例14: beginArray
import sagex.remote.json.JSONObject; //导入依赖的package包/类
public void beginArray(String name, int size) {
arr = new JSONArray();
if (cur instanceof JSONObject) {
try {
((JSONObject)cur).put(name, arr);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if (cur instanceof JSONArray) {
((JSONArray)cur).put(arr);
}
objs.push(arr);
cur=arr;
}
示例15: beginObject
import sagex.remote.json.JSONObject; //导入依赖的package包/类
public void beginObject(String objectName) {
JSONObject o = new JSONObject();
if (cur instanceof JSONObject) {
try {
((JSONObject)cur).put(objectName, o);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if (cur instanceof JSONArray) {
((JSONArray)cur).put(o);
}
objs.push(o);
cur = o;
}