use of org.json.JSONTokener in project tdme by andreasdr.
the class ModelMetaDataFileImport method doImport.
/**
* Imports a model meta data file from file
* @param id or LevelEditorEntity.ID_NONE
* @param path name
* @param file name
*/
public static LevelEditorEntity doImport(int id, String pathName, String fileName) throws Exception {
fileName = fileName.replace(File.separatorChar == '/' ? '\\' : '/', File.separatorChar);
JSONObject jEntityRoot = null;
InputStream is = null;
try {
jEntityRoot = new JSONObject(new JSONTokener(FileSystem.getInstance().getContent(pathName, fileName)));
} catch (IOException ioe) {
throw ioe;
} finally {
if (is != null)
try {
is.close();
} catch (IOException ioei) {
}
}
// do the work
LevelEditorEntity levelEditorEntity = doImportFromJSON(id, pathName, jEntityRoot);
levelEditorEntity.setEntityFileName(pathName + "/" + fileName);
return levelEditorEntity;
}
use of org.json.JSONTokener in project muzei by romannurik.
the class FeaturedArtSource method fetchJsonObject.
private JSONObject fetchJsonObject(final String url) throws IOException, JSONException {
OkHttpClient client = new OkHttpClient.Builder().build();
Request request = new Request.Builder().url(url).build();
String json = client.newCall(request).execute().body().string();
JSONTokener tokener = new JSONTokener(json);
Object val = tokener.nextValue();
if (!(val instanceof JSONObject)) {
throw new JSONException("Expected JSON object.");
}
return (JSONObject) val;
}
use of org.json.JSONTokener in project spanner-jdbc by olavloite.
the class CloudSpannerConnection method getServiceAccountProjectId.
public static String getServiceAccountProjectId(String credentialsPath) {
String project = null;
if (credentialsPath != null) {
try (InputStream credentialsStream = new FileInputStream(credentialsPath)) {
JSONObject json = new JSONObject(new JSONTokener(credentialsStream));
project = json.getString("project_id");
} catch (IOException | JSONException ex) {
// ignore
}
}
return project;
}
use of org.json.JSONTokener in project opennms by OpenNMS.
the class SnmpAgentConfig method parseProtocolConfigurationString.
public static SnmpAgentConfig parseProtocolConfigurationString(String protocolConfigString) {
if (protocolConfigString == null) {
throw new IllegalArgumentException("Protocol configuration string for SnmpAgentConfig must not be null.");
}
final JSONObject protocolConfig = new JSONObject(new JSONTokener(protocolConfigString)).optJSONObject("snmp");
if (protocolConfig == null) {
throw new IllegalStateException("Invalid protocol configuration string for SnmpAgentConfig: Expected it to start with snmp object" + protocolConfigString);
}
Map<String, String> attributes = new HashMap<>();
@SuppressWarnings("unchecked") Iterator<String> keysItr = protocolConfig.keys();
while (keysItr.hasNext()) {
String key = keysItr.next();
attributes.put(key, protocolConfig.isNull(key) ? null : protocolConfig.getString(key));
}
return SnmpAgentConfig.fromMap(attributes);
}
use of org.json.JSONTokener in project SmartAndroidSource by jaychou2012.
the class AbstractAjaxCallback method transform.
@SuppressWarnings("unchecked")
protected T transform(String url, byte[] data, AjaxStatus status) {
if (type == null) {
return null;
}
File file = status.getFile();
if (data != null) {
if (type.equals(Bitmap.class)) {
return (T) BitmapFactory.decodeByteArray(data, 0, data.length);
}
if (type.equals(JSONObject.class)) {
JSONObject result = null;
String str = null;
try {
str = new String(data, encoding);
result = (JSONObject) new JSONTokener(str).nextValue();
} catch (Exception e) {
AQUtility.debug(e);
AQUtility.debug(str);
}
return (T) result;
}
if (type.equals(JSONArray.class)) {
JSONArray result = null;
try {
String str = new String(data, encoding);
result = (JSONArray) new JSONTokener(str).nextValue();
} catch (Exception e) {
AQUtility.debug(e);
}
return (T) result;
}
if (type.equals(String.class)) {
String result = null;
if (status.getSource() == AjaxStatus.NETWORK) {
AQUtility.debug("network");
result = correctEncoding(data, encoding, status);
} else {
AQUtility.debug("file");
try {
result = new String(data, encoding);
} catch (Exception e) {
AQUtility.debug(e);
}
}
return (T) result;
}
if (type.equals(byte[].class)) {
return (T) data;
}
if (transformer != null) {
return transformer.transform(url, type, encoding, data, status);
}
if (st != null) {
return st.transform(url, type, encoding, data, status);
}
} else if (file != null) {
if (type.equals(File.class)) {
return (T) file;
}
if (type.equals(XmlDom.class)) {
XmlDom result = null;
try {
FileInputStream fis = new FileInputStream(file);
result = new XmlDom(fis);
status.closeLater(fis);
} catch (Exception e) {
AQUtility.report(e);
return null;
}
return (T) result;
}
if (type.equals(XmlPullParser.class)) {
XmlPullParser parser = Xml.newPullParser();
try {
FileInputStream fis = new FileInputStream(file);
parser.setInput(fis, encoding);
status.closeLater(fis);
} catch (Exception e) {
AQUtility.report(e);
return null;
}
return (T) parser;
}
if (type.equals(InputStream.class)) {
try {
FileInputStream fis = new FileInputStream(file);
status.closeLater(fis);
return (T) fis;
} catch (Exception e) {
AQUtility.report(e);
return null;
}
}
}
return null;
}
Aggregations