/** * Get the string associated with an index. * @param index The index must be between 0 and length() - 1. * @return A string value. * @throws JSONException If there is no string value for the index. */ public String getString(int index) throws JSONException { Object object = get(index); if (object instanceof String) { return (String)object; } throw new JSONException("JSONArray[" + index + "] not a string."); }
sb.append('>'); do { object = ja.get(i); i += 1; if (object != null) {
/** * Get the JSONObject associated with an index. * @param index subscript * @return A JSONObject value. * @throws JSONException If there is no value for the index or if the * value is not a JSONObject */ public JSONObject getJSONObject(int index) throws JSONException { Object object = get(index); if (object instanceof JSONObject) { return (JSONObject)object; } throw new JSONException("JSONArray[" + index + "] is not a JSONObject."); }
/** * Get the JSONArray associated with an index. * @param index The index must be between 0 and length() - 1. * @return A JSONArray value. * @throws JSONException If there is no value for the index. or if the * value is not a JSONArray */ public JSONArray getJSONArray(int index) throws JSONException { Object object = get(index); if (object instanceof JSONArray) { return (JSONArray)object; } throw new JSONException("JSONArray[" + index + "] is not a JSONArray."); }
@Override public Object getListElement(Object data, int index) { if (data == null) { return null; } JSONArray array = getJSONArray(data); if(array==null) return null; try { Object obj = array.get(index); if (JSONObject.NULL == obj) { return null; } else { return obj; } } catch (JSONException ex) { return null; } }
/** * Get the double value associated with an index. * * @param index The index must be between 0 and length() - 1. * @return The value. * @throws JSONException If the key is not found or if the value cannot * be converted to a number. */ public double getDouble(int index) throws JSONException { Object object = get(index); try { return object instanceof Number ? ((Number)object).doubleValue() : Double.parseDouble((String)object); } catch (Exception e) { throw new JSONException("JSONArray[" + index + "] is not a number."); } }
/** * Get the int value associated with an index. * * @param index The index must be between 0 and length() - 1. * @return The value. * @throws JSONException If the key is not found or if the value is not a number. */ public int getInt(int index) throws JSONException { Object object = get(index); try { return object instanceof Number ? ((Number)object).intValue() : Integer.parseInt((String)object); } catch (Exception e) { throw new JSONException("JSONArray[" + index + "] is not a number."); } }
/** * Get the long value associated with an index. * * @param index The index must be between 0 and length() - 1. * @return The value. * @throws JSONException If the key is not found or if the value cannot * be converted to a number. */ public long getLong(int index) throws JSONException { Object object = get(index); try { return object instanceof Number ? ((Number)object).longValue() : Long.parseLong((String)object); } catch (Exception e) { throw new JSONException("JSONArray[" + index + "] is not a number."); } }
/** * Get the boolean value associated with an index. * The string values "true" and "false" are converted to boolean. * * @param index The index must be between 0 and length() - 1. * @return The truth. * @throws JSONException If there is no value for the index or if the * value is not convertible to boolean. */ public boolean getBoolean(int index) throws JSONException { Object object = get(index); if (object.equals(Boolean.FALSE) || (object instanceof String && ((String)object).equalsIgnoreCase("false"))) { return false; } else if (object.equals(Boolean.TRUE) || (object instanceof String && ((String)object).equalsIgnoreCase("true"))) { return true; } throw new JSONException("JSONArray[" + index + "] is not a boolean."); }
length = ja.length(); for (i = 0; i < length; i += 1) { object = ja.get(i); if (object != null) { if (object instanceof String) {
@Test public void testDeserialize2() throws Exception { JsonSerDe instance = new JsonSerDe(); initialize(instance); Writable w = new Text("{\"one\":true,\"three\":[\"red\",\"yellow\",[\"blue\",\"azure\",\"cobalt\",\"teal\"],\"orange\"],\"two\":19.5,\"four\":\"poop\"}"); JSONObject result = (JSONObject) instance.deserialize(w); assertEquals("poop", result.get("four")); assertTrue(result.get("three") instanceof JSONArray); assertTrue(((JSONArray) result.get("three")).get(0) instanceof String); assertEquals("red", ((JSONArray) result.get("three")).get(0)); }
@Test public void testDeserializePartialFieldSet() throws Exception { Writable w = new Text("{\"missing\":\"whocares\",\"one\":true,\"three\":[\"red\",\"yellow\",[\"blue\",\"azure\",\"cobalt\",\"teal\"],\"orange\"],\"two\":19.5,\"four\":\"poop\"}"); JsonSerDe instance = new JsonSerDe(); initialize(instance); JSONObject result = (JSONObject) instance.deserialize(w); assertEquals(result.get("four"),"poop"); assertTrue( result.get("three") instanceof JSONArray); assertTrue( ((JSONArray)result.get("three")).get(0) instanceof String ); assertEquals( ((JSONArray)result.get("three")).get(0),"red"); }
/** * Test of deserialize method, but passing an array. */ @Test public void testDeserialize() throws Exception { JsonSerDe instance = new JsonSerDe(); initialize(instance); System.out.println("deserialize"); Writable w = new Text("{\"one\":true,\"three\":[\"red\",\"yellow\",\"orange\"],\"two\":19.5,\"four\":\"poop\"}"); JSONObject result = (JSONObject) instance.deserialize(w); assertEquals("poop",result.get("four")); assertTrue(result.get("three") instanceof JSONArray); assertTrue( ((JSONArray)result.get("three")).get(0) instanceof String ); assertEquals("red", ((JSONArray)result.get("three")).get(0)); }
@Test public void testDeserialize2Initializations() throws Exception { JsonSerDe instance = new JsonSerDe(); initialize(instance); Writable w = new Text("{\"one\":true,\"three\":[\"red\",\"yellow\",[\"blue\",\"azure\",\"cobalt\",\"teal\"],\"orange\"],\"two\":19.5,\"four\":\"poop\"}"); JSONObject result = (JSONObject) instance.deserialize(w); assertEquals(result.get("four"), "poop"); assertTrue(result.get("three") instanceof JSONArray); assertTrue(((JSONArray) result.get("three")).get(0) instanceof String); assertEquals(((JSONArray) result.get("three")).get(0), "red"); // second initialization, new column initialize2(instance); result = (JSONObject) instance.deserialize(w); assertEquals(result.get("four"), "poop"); assertTrue(result.get("three") instanceof JSONArray); assertTrue(((JSONArray) result.get("three")).get(0) instanceof String); assertEquals(((JSONArray) result.get("three")).get(0), "red"); }