List of usage examples for org.json JSONObject opt
public Object opt(String key)
From source file:com.reecedunn.espeak.test.VoiceSettingsTest.java
public void espeakPitchRangeTest(int prefValue, int settingValue, SpeechSynthesis synth) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); SharedPreferences.Editor editor = prefs.edit(); editor.clear();/*w w w .j av a 2s . c o m*/ editor.putString("espeak_pitch_range", Integer.toString(prefValue)); editor.commit(); VoiceSettings settings = new VoiceSettings(prefs, synth); assertThat(settings.getVoiceVariant().toString(), is("male")); assertThat(settings.getRate(), is(synth.Rate.getDefaultValue())); assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue())); assertThat(settings.getPitchRange(), is(settingValue)); assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue())); assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE)); assertThat(settings.getPunctuationCharacters(), is(nullValue())); try { JSONObject json = settings.toJSON(); assertThat(json.opt(VoiceSettings.PRESET_VARIANT), is(instanceOf(String.class))); assertThat((String) json.opt(VoiceSettings.PRESET_VARIANT), is("male")); assertThat(json.opt(VoiceSettings.PRESET_RATE), is(instanceOf(Integer.class))); assertThat((Integer) json.opt(VoiceSettings.PRESET_RATE), is(synth.Rate.getDefaultValue())); assertThat(json.opt(VoiceSettings.PRESET_PITCH), is(instanceOf(Integer.class))); assertThat((Integer) json.opt(VoiceSettings.PRESET_PITCH), is(synth.Pitch.getDefaultValue())); assertThat(json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(instanceOf(Integer.class))); assertThat((Integer) json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(settingValue)); assertThat(json.opt(VoiceSettings.PRESET_VOLUME), is(instanceOf(Integer.class))); assertThat((Integer) json.opt(VoiceSettings.PRESET_VOLUME), is(synth.Volume.getDefaultValue())); assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(instanceOf(String.class))); assertThat((String) json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is("none")); assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(nullValue())); } catch (JSONException e) { assertThat(e.toString(), is(nullValue())); // This will be false; used to report exception. } }
From source file:com.reecedunn.espeak.test.VoiceSettingsTest.java
public void espeakVolumeTest(int prefValue, int settingValue, SpeechSynthesis synth) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); SharedPreferences.Editor editor = prefs.edit(); editor.clear();/*from w ww. jav a 2 s . c o m*/ editor.putString("espeak_volume", Integer.toString(prefValue)); editor.commit(); VoiceSettings settings = new VoiceSettings(prefs, synth); assertThat(settings.getVoiceVariant().toString(), is("male")); assertThat(settings.getRate(), is(synth.Rate.getDefaultValue())); assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue())); assertThat(settings.getPitchRange(), is(synth.PitchRange.getDefaultValue())); assertThat(settings.getVolume(), is(settingValue)); assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE)); assertThat(settings.getPunctuationCharacters(), is(nullValue())); try { JSONObject json = settings.toJSON(); assertThat(json.opt(VoiceSettings.PRESET_VARIANT), is(instanceOf(String.class))); assertThat((String) json.opt(VoiceSettings.PRESET_VARIANT), is("male")); assertThat(json.opt(VoiceSettings.PRESET_RATE), is(instanceOf(Integer.class))); assertThat((Integer) json.opt(VoiceSettings.PRESET_RATE), is(synth.Rate.getDefaultValue())); assertThat(json.opt(VoiceSettings.PRESET_PITCH), is(instanceOf(Integer.class))); assertThat((Integer) json.opt(VoiceSettings.PRESET_PITCH), is(synth.Pitch.getDefaultValue())); assertThat(json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(instanceOf(Integer.class))); assertThat((Integer) json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(synth.PitchRange.getDefaultValue())); assertThat(json.opt(VoiceSettings.PRESET_VOLUME), is(instanceOf(Integer.class))); assertThat((Integer) json.opt(VoiceSettings.PRESET_VOLUME), is(settingValue)); assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(instanceOf(String.class))); assertThat((String) json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is("none")); assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(nullValue())); } catch (JSONException e) { assertThat(e.toString(), is(nullValue())); // This will be false; used to report exception. } }
From source file:com.reecedunn.espeak.test.VoiceSettingsTest.java
public void espeakPunctuationLevelTest(int prefValue, int settingValue, String jsonValue, SpeechSynthesis synth) {/*from www . j a v a2 s .c o m*/ SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); SharedPreferences.Editor editor = prefs.edit(); editor.clear(); editor.putString("espeak_punctuation_level", Integer.toString(prefValue)); editor.commit(); VoiceSettings settings = new VoiceSettings(prefs, synth); assertThat(settings.getVoiceVariant().toString(), is("male")); assertThat(settings.getRate(), is(synth.Rate.getDefaultValue())); assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue())); assertThat(settings.getPitchRange(), is(synth.PitchRange.getDefaultValue())); assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue())); assertThat(settings.getPunctuationLevel(), is(settingValue)); assertThat(settings.getPunctuationCharacters(), is(nullValue())); try { JSONObject json = settings.toJSON(); assertThat(json.opt(VoiceSettings.PRESET_VARIANT), is(instanceOf(String.class))); assertThat((String) json.opt(VoiceSettings.PRESET_VARIANT), is("male")); assertThat(json.opt(VoiceSettings.PRESET_RATE), is(instanceOf(Integer.class))); assertThat((Integer) json.opt(VoiceSettings.PRESET_RATE), is(synth.Rate.getDefaultValue())); assertThat(json.opt(VoiceSettings.PRESET_PITCH), is(instanceOf(Integer.class))); assertThat((Integer) json.opt(VoiceSettings.PRESET_PITCH), is(synth.Pitch.getDefaultValue())); assertThat(json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(instanceOf(Integer.class))); assertThat((Integer) json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(synth.PitchRange.getDefaultValue())); assertThat(json.opt(VoiceSettings.PRESET_VOLUME), is(instanceOf(Integer.class))); assertThat((Integer) json.opt(VoiceSettings.PRESET_VOLUME), is(synth.Volume.getDefaultValue())); assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(instanceOf(String.class))); assertThat((String) json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(jsonValue)); assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(nullValue())); } catch (JSONException e) { assertThat(e.toString(), is(nullValue())); // This will be false; used to report exception. } }
From source file:com.reecedunn.espeak.test.VoiceSettingsTest.java
public void testEspeakPunctuationCharacters() { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); SharedPreferences.Editor editor = prefs.edit(); editor.clear();//w w w . j a v a 2 s .co m editor.putString("espeak_punctuation_characters", ".?!"); editor.commit(); SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback); VoiceSettings settings = new VoiceSettings(prefs, synth); assertThat(settings.getVoiceVariant().toString(), is("male")); assertThat(settings.getRate(), is(synth.Rate.getDefaultValue())); assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue())); assertThat(settings.getPitchRange(), is(synth.PitchRange.getDefaultValue())); assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue())); assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE)); assertThat(settings.getPunctuationCharacters(), is(".?!")); try { JSONObject json = settings.toJSON(); assertThat(json.opt(VoiceSettings.PRESET_VARIANT), is(instanceOf(String.class))); assertThat((String) json.opt(VoiceSettings.PRESET_VARIANT), is("male")); assertThat(json.opt(VoiceSettings.PRESET_RATE), is(instanceOf(Integer.class))); assertThat((Integer) json.opt(VoiceSettings.PRESET_RATE), is(synth.Rate.getDefaultValue())); assertThat(json.opt(VoiceSettings.PRESET_PITCH), is(instanceOf(Integer.class))); assertThat((Integer) json.opt(VoiceSettings.PRESET_PITCH), is(synth.Pitch.getDefaultValue())); assertThat(json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(instanceOf(Integer.class))); assertThat((Integer) json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(synth.PitchRange.getDefaultValue())); assertThat(json.opt(VoiceSettings.PRESET_VOLUME), is(instanceOf(Integer.class))); assertThat((Integer) json.opt(VoiceSettings.PRESET_VOLUME), is(synth.Volume.getDefaultValue())); assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(instanceOf(String.class))); assertThat((String) json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is("none")); assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(instanceOf(String.class))); assertThat((String) json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(".?!")); } catch (JSONException e) { assertThat(e.toString(), is(nullValue())); // This will be false; used to report exception. } }
From source file:com.reecedunn.espeak.test.VoiceSettingsTest.java
public void testEspeakVariantWithDefaultGenderFemale() { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); SharedPreferences.Editor editor = prefs.edit(); editor.clear();//from w w w .ja v a 2 s.co m editor.putString("default_gender", Integer.toString(SpeechSynthesis.GENDER_FEMALE)); editor.putString("espeak_variant", "klatt4"); editor.commit(); SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback); VoiceSettings settings = new VoiceSettings(prefs, synth); assertThat(settings.getVoiceVariant().toString(), is("klatt4")); assertThat(settings.getRate(), is(synth.Rate.getDefaultValue())); assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue())); assertThat(settings.getPitchRange(), is(synth.PitchRange.getDefaultValue())); assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue())); assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE)); assertThat(settings.getPunctuationCharacters(), is(nullValue())); try { JSONObject json = settings.toJSON(); assertThat(json.opt(VoiceSettings.PRESET_VARIANT), is(instanceOf(String.class))); assertThat((String) json.opt(VoiceSettings.PRESET_VARIANT), is("klatt4")); assertThat(json.opt(VoiceSettings.PRESET_RATE), is(instanceOf(Integer.class))); assertThat((Integer) json.opt(VoiceSettings.PRESET_RATE), is(synth.Rate.getDefaultValue())); assertThat(json.opt(VoiceSettings.PRESET_PITCH), is(instanceOf(Integer.class))); assertThat((Integer) json.opt(VoiceSettings.PRESET_PITCH), is(synth.Pitch.getDefaultValue())); assertThat(json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(instanceOf(Integer.class))); assertThat((Integer) json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(synth.PitchRange.getDefaultValue())); assertThat(json.opt(VoiceSettings.PRESET_VOLUME), is(instanceOf(Integer.class))); assertThat((Integer) json.opt(VoiceSettings.PRESET_VOLUME), is(synth.Volume.getDefaultValue())); assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(instanceOf(String.class))); assertThat((String) json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is("none")); assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(nullValue())); } catch (JSONException e) { assertThat(e.toString(), is(nullValue())); // This will be false; used to report exception. } }
From source file:com.reecedunn.espeak.test.VoiceSettingsTest.java
public void testEspeakRateWithDefaultRate() { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); SharedPreferences.Editor editor = prefs.edit(); editor.clear();//from w w w. j a va 2 s . co m editor.putString("default_rate", Integer.toString(50)); editor.putString("espeak_rate", Integer.toString(200)); editor.commit(); SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback); VoiceSettings settings = new VoiceSettings(prefs, synth); assertThat(settings.getVoiceVariant().toString(), is("male")); assertThat(settings.getRate(), is(200)); assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue())); assertThat(settings.getPitchRange(), is(synth.PitchRange.getDefaultValue())); assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue())); assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE)); assertThat(settings.getPunctuationCharacters(), is(nullValue())); try { JSONObject json = settings.toJSON(); assertThat(json.opt(VoiceSettings.PRESET_VARIANT), is(instanceOf(String.class))); assertThat((String) json.opt(VoiceSettings.PRESET_VARIANT), is("male")); assertThat(json.opt(VoiceSettings.PRESET_RATE), is(instanceOf(Integer.class))); assertThat((Integer) json.opt(VoiceSettings.PRESET_RATE), is(200)); assertThat(json.opt(VoiceSettings.PRESET_PITCH), is(instanceOf(Integer.class))); assertThat((Integer) json.opt(VoiceSettings.PRESET_PITCH), is(synth.Pitch.getDefaultValue())); assertThat(json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(instanceOf(Integer.class))); assertThat((Integer) json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(synth.PitchRange.getDefaultValue())); assertThat(json.opt(VoiceSettings.PRESET_VOLUME), is(instanceOf(Integer.class))); assertThat((Integer) json.opt(VoiceSettings.PRESET_VOLUME), is(synth.Volume.getDefaultValue())); assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(instanceOf(String.class))); assertThat((String) json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is("none")); assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(nullValue())); } catch (JSONException e) { assertThat(e.toString(), is(nullValue())); // This will be false; used to report exception. } }
From source file:com.reecedunn.espeak.test.VoiceSettingsTest.java
public void testEspeakPitchWithDefaultPitch() { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); SharedPreferences.Editor editor = prefs.edit(); editor.clear();// w w w.ja v a2s . com editor.putString("default_pitch", Integer.toString(50)); editor.putString("espeak_pitch", Integer.toString(75)); editor.commit(); SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback); VoiceSettings settings = new VoiceSettings(prefs, synth); assertThat(settings.getVoiceVariant().toString(), is("male")); assertThat(settings.getRate(), is(synth.Rate.getDefaultValue())); assertThat(settings.getPitch(), is(75)); assertThat(settings.getPitchRange(), is(synth.PitchRange.getDefaultValue())); assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue())); assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE)); assertThat(settings.getPunctuationCharacters(), is(nullValue())); try { JSONObject json = settings.toJSON(); assertThat(json.opt(VoiceSettings.PRESET_VARIANT), is(instanceOf(String.class))); assertThat((String) json.opt(VoiceSettings.PRESET_VARIANT), is("male")); assertThat(json.opt(VoiceSettings.PRESET_RATE), is(instanceOf(Integer.class))); assertThat((Integer) json.opt(VoiceSettings.PRESET_RATE), is(synth.Rate.getDefaultValue())); assertThat(json.opt(VoiceSettings.PRESET_PITCH), is(instanceOf(Integer.class))); assertThat((Integer) json.opt(VoiceSettings.PRESET_PITCH), is(75)); assertThat(json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(instanceOf(Integer.class))); assertThat((Integer) json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(synth.PitchRange.getDefaultValue())); assertThat(json.opt(VoiceSettings.PRESET_VOLUME), is(instanceOf(Integer.class))); assertThat((Integer) json.opt(VoiceSettings.PRESET_VOLUME), is(synth.Volume.getDefaultValue())); assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(instanceOf(String.class))); assertThat((String) json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is("none")); assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(nullValue())); } catch (JSONException e) { assertThat(e.toString(), is(nullValue())); // This will be false; used to report exception. } }
From source file:com.adf.bean.AbsBean.java
public boolean fromJson(JSONObject json) { List<String> cols = jsonColumns(); for (int i = 0; i < cols.size(); i++) { String col = cols.get(i); Object val = json.opt(col); if (val != null) { if (val instanceof JSONArray) { JSONArray arr = (JSONArray) val; try { setArrayColumn(col, arr); } catch (NoSuchFieldException e) { } catch (IllegalAccessException e) { } catch (IllegalArgumentException e) { }/*from w ww .j a va 2 s.c o m*/ } else { try { setValueByOject(col, val); } catch (NoSuchFieldException e) { } catch (IllegalAccessException e) { } catch (IllegalArgumentException e) { } } } } return true; }
From source file:m2.android.archetype.example.FacebookSdk.internal.Utility.java
public static Object getStringPropertyAsJSON(JSONObject jsonObject, String key, String nonJSONPropertyKey) throws JSONException { Object value = jsonObject.opt(key); if (value != null && value instanceof String) { JSONTokener tokener = new JSONTokener((String) value); value = tokener.nextValue();//from w w w.j a v a2 s .co m } if (value != null && !(value instanceof JSONObject || value instanceof JSONArray)) { if (nonJSONPropertyKey != null) { // Facebook sometimes gives us back a non-JSON value such as // literal "true" or "false" as a result. // If we got something like that, we present it to the caller as // a GraphObject with a single // property. We only do this if the caller wants that behavior. jsonObject = new JSONObject(); jsonObject.putOpt(nonJSONPropertyKey, value); return jsonObject; } else { throw new FacebookException("Got an unexpected non-JSON object."); } } return value; }
From source file:com.panchosoft.appspy.models.iTunesProxy.java
public iTunesApp[] buscarApp(String appName, int limit) { // Hacemos una bsqueda en iTunes String datos_json = buscarDatosAppJSON(appName, 15); if (datos_json == null || datos_json.length() == 0) { return null; }//from w w w .j ava 2 s. c o m try { JSONObject ob = new JSONObject(datos_json); if (ob == null) { return null; } int totalResultados = ob.getInt("resultCount"); if (totalResultados == 0) { return null; } iTunesApp[] appsencontradas = new iTunesApp[totalResultados]; for (int c = 0; c < totalResultados; c++) { JSONArray resultados = ob.getJSONArray("results"); if (resultados == null) { return null; } JSONObject appdata = resultados.getJSONObject(c); if (appdata == null) { return null; } // Creamos el objeto iTunesApp iTunesApp app = new iTunesApp(); app.setArtistId(appdata.opt("artistId").toString()); app.setArtistName(appdata.optString("artistName")); app.setArtistViewUrl(appdata.optString("artistViewUrl")); app.setArtworkUrl100(appdata.optString("artworkUrl100")); app.setArtworkUrl60(appdata.optString("artworkUrl60")); app.setContentAdvisoryRating(appdata.optString("advisorRating")); app.setDescription(appdata.optString("description")); app.setFileSizeBytes(appdata.optString("fileSizeBytes")); // Generos JSONArray generos = appdata.getJSONArray("genreIds"); String[] generosApp = new String[generos.length()]; for (int i = 0; i < generos.length(); i++) { generosApp[i] = generos.getString(i); } app.setGenreIds(generosApp); // Generos nombres generos = appdata.getJSONArray("genres"); generosApp = new String[generos.length()]; for (int i = 0; i < generos.length(); i++) { generosApp[i] = generos.getString(i); } app.setGenres(generosApp); // Lenguajes JSONArray lenguajes = appdata.getJSONArray("genres"); String[] lenguajesApp = new String[lenguajes.length()]; for (int i = 0; i < lenguajes.length(); i++) { lenguajesApp[i] = lenguajes.getString(i); } app.setLanguageCodesISO2(lenguajesApp); app.setPrice(appdata.optString("price")); app.setPrimaryGenreId(appdata.optString("primaryGenreId")); app.setPrimaryGenreName(appdata.optString("primaryGenreName")); app.setReleaseDate(appdata.optString("releaseDate")); // Capturas JSONArray capturas = appdata.getJSONArray("screenshotUrls"); String[] capturasApp = new String[capturas.length()]; for (int i = 0; i < capturas.length(); i++) { capturasApp[i] = capturas.getString(i); } app.setScreenshotUrls(capturasApp); app.setSellerName(appdata.optString("sellerName")); app.setSellerUrl(appdata.optString("sellerUrl")); // Dispositivos soportados JSONArray dispositivos = appdata.getJSONArray("supportedDevices"); String[] dispositivosApp = new String[dispositivos.length()]; for (int i = 0; i < dispositivos.length(); i++) { dispositivosApp[i] = dispositivos.getString(i); } app.setSupportedDevices(dispositivosApp); app.setTrackCensoredName(appdata.optString("trackCensoredName")); app.setTrackName(appdata.optString("trackName")); app.setTrackViewUrl(appdata.optString("trackViewUrl")); app.setVersion(appdata.optString("version")); appsencontradas[c] = app; } return appsencontradas; } catch (JSONException ex) { Logger.getLogger("com.panchosoft.itunes").log(Level.WARNING, "Error: " + ex.getMessage()); } return null; }