Search in sources :

Example 26 with JSONTokener

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;
}
Also used : JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) InputStream(java.io.InputStream) IOException(java.io.IOException) LevelEditorEntity(net.drewke.tdme.tools.shared.model.LevelEditorEntity)

Example 27 with JSONTokener

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;
}
Also used : JSONTokener(org.json.JSONTokener) OkHttpClient(okhttp3.OkHttpClient) JSONObject(org.json.JSONObject) Request(okhttp3.Request) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject)

Example 28 with JSONTokener

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;
}
Also used : JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JSONException(org.json.JSONException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 29 with JSONTokener

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);
}
Also used : JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 30 with JSONTokener

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;
}
Also used : JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JSONArray(org.json.JSONArray) XmlPullParser(org.xmlpull.v1.XmlPullParser) File(java.io.File) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Aggregations

JSONTokener (org.json.JSONTokener)63 JSONObject (org.json.JSONObject)60 JSONException (org.json.JSONException)32 JSONArray (org.json.JSONArray)23 IOException (java.io.IOException)19 ArrayList (java.util.ArrayList)12 InputStream (java.io.InputStream)10 GraphObject (com.facebook.model.GraphObject)8 FileInputStream (java.io.FileInputStream)8 File (java.io.File)6 HashMap (java.util.HashMap)6 HttpEntity (org.apache.http.HttpEntity)5 HttpResponse (org.apache.http.HttpResponse)5 BufferedReader (java.io.BufferedReader)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 InputStreamReader (java.io.InputStreamReader)4 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)4 HttpPost (org.apache.http.client.methods.HttpPost)4 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)4 FacebookException (com.facebook.FacebookException)3