List of usage examples for org.json JSONObject opt
public Object opt(String key)
From source file:com.github.koraktor.steamcondenser.community.GameItem.java
/** * Creates a new instance of a GameItem with the given data * * @param inventory The inventory this item is contained in * @param itemData The data specifying this item * @throws WebApiException on Web API errors *//*from w w w. jav a 2 s. co m*/ public GameItem(GameInventory inventory, JSONObject itemData) throws SteamCondenserException { this.inventory = inventory; try { this.defindex = itemData.getInt("defindex"); this.backpackPosition = (int) itemData.getLong("inventory") & 0xffff; this.count = itemData.getInt("quantity"); this.craftable = !itemData.optBoolean("flag_cannot_craft"); this.id = itemData.getInt("id"); this.itemClass = this.getSchemaData().getString("item_class"); this.itemSet = this.inventory.getItemSchema().getItemSets() .get(this.getSchemaData().optString("item_set")); this.level = itemData.getInt("level"); this.name = this.getSchemaData().getString("item_name"); this.preliminary = (itemData.getLong("inventory") & 0x40000000) != 0; this.originalId = itemData.getInt("original_id"); this.quality = this.inventory.getItemSchema().getQualities().get(itemData.getInt("quality")); this.tradeable = !itemData.optBoolean("flag_cannot_trade"); this.type = this.getSchemaData().getString("item_type_name"); if (itemData.has("origin")) { this.origin = this.inventory.getItemSchema().getOrigins().get(itemData.getInt("origin")); } JSONArray attributesData = this.getSchemaData().optJSONArray("attributes"); if (attributesData == null) { attributesData = new JSONArray(); } if (itemData.has("attributes")) { JSONArray itemAttributes = itemData.getJSONArray("attributes"); for (int i = 0; i < itemAttributes.length(); i++) { attributesData.put(itemAttributes.get(i)); } } this.attributes = new ArrayList<JSONObject>(); for (int i = 0; i < attributesData.length(); i++) { JSONObject attributeData = attributesData.getJSONObject(i); Object attributeKey = attributeData.opt("defindex"); if (attributeKey == null) { attributeKey = attributeData.opt("name"); } if (attributeKey != null) { JSONObject schemaAttributeData = inventory.getItemSchema().getAttributes().get(attributeKey); for (String key : JSONObject.getNames(schemaAttributeData)) { attributeData.put(key, schemaAttributeData.get(key)); } this.attributes.add(attributeData); } } } catch (JSONException e) { throw new WebApiException("Could not parse JSON data.", e); } }
From source file:org.jdesktop.http.async.JsonHttpRequest.java
private void stuffIntoMap(JSONObject obj, Map<String, Object> map) { if (obj == null) { return;/* w w w . ja va 2 s . co m*/ } Iterator itr = obj.keys(); while (itr.hasNext()) { String key = (String) itr.next(); Object value = obj.opt(key); if (value instanceof JSONArray) { JSONArray a = (JSONArray) value; Object[] array = new Object[a.length()]; stuffIntoArray(a, array); map.put(key, array); } else if (value instanceof JSONObject) { Map<String, Object> submap = new HashMap<String, Object>(); stuffIntoMap((JSONObject) value, submap); map.put(key, submap); } else { map.put(key, value); } } }
From source file:com.reecedunn.espeak.test.VoiceSettingsTest.java
public void testNoPreferences() { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); SharedPreferences.Editor editor = prefs.edit(); editor.clear();//from w w w.j a v a 2 s .com 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(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("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 testDefaultGenderMale() { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); SharedPreferences.Editor editor = prefs.edit(); editor.clear();// www. ja va 2 s . c o m editor.putString("default_gender", Integer.toString(SpeechSynthesis.GENDER_MALE)); 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(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("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 testDefaultGenderFemale() { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); SharedPreferences.Editor editor = prefs.edit(); editor.clear();/* w w w . j a v a2 s.c o m*/ editor.putString("default_gender", Integer.toString(SpeechSynthesis.GENDER_FEMALE)); editor.commit(); SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback); VoiceSettings settings = new VoiceSettings(prefs, synth); assertThat(settings.getVoiceVariant().toString(), is("female")); 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("female")); 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 defaultRateTest(int prefValue, int settingValue, SpeechSynthesis synth) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); SharedPreferences.Editor editor = prefs.edit(); editor.clear();/*from w w w. j ava2s. c o m*/ editor.putString("default_rate", Integer.toString(prefValue)); editor.commit(); VoiceSettings settings = new VoiceSettings(prefs, synth); assertThat(settings.getVoiceVariant().toString(), is("male")); assertThat(settings.getRate(), is(settingValue)); 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(settingValue)); 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 defaultPitchTest(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 om editor.putString("default_pitch", 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(settingValue)); 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(settingValue)); 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 testEspeakVariant() { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); SharedPreferences.Editor editor = prefs.edit(); editor.clear();//ww w . j a v a 2 s . c om editor.putString("espeak_variant", "klatt2-old"); editor.commit(); SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback); VoiceSettings settings = new VoiceSettings(prefs, synth); assertThat(settings.getVoiceVariant().toString(), is("klatt2-old")); 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("klatt2-old")); 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 espeakRateTest(int prefValue, int settingValue, SpeechSynthesis synth) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); SharedPreferences.Editor editor = prefs.edit(); editor.clear();//from w w w. ja va 2s . c o m editor.putString("espeak_rate", Integer.toString(prefValue)); editor.commit(); VoiceSettings settings = new VoiceSettings(prefs, synth); assertThat(settings.getVoiceVariant().toString(), is("male")); assertThat(settings.getRate(), is(settingValue)); 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(settingValue)); 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 espeakPitchTest(int prefValue, int settingValue, SpeechSynthesis synth) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); SharedPreferences.Editor editor = prefs.edit(); editor.clear();/*from ww w .j a v a 2s . c om*/ editor.putString("espeak_pitch", 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(settingValue)); 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(settingValue)); 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. } }