/** * sanitize all the JSONObject.NULL converting them to nulls. * @param s * @return * @throws JSONException */ private Object safeGet(String s) throws JSONException { if(s == null) return null; Object obj = jsonObject.get(s); if(JSONObject.NULL.equals(obj)) { return null; } else { return obj; } }
@Override public Object getMapValueElement(Object data, Object key) { if (JsonObjectInspectorUtils.checkObject(data) == null) { return null; } JSONObject jObj = (JSONObject) data; try { Object obj = jObj.get(key.toString()); if(JSONObject.NULL == obj) { return null; } else { return obj; } } catch (JSONException ex) { // key does not exists -> like null return null; } } }
/** * Get the double value associated with a key. * @param key A key string. * @return The numeric value. * @throws JSONException if the key is not found or * if the value is not a Number object and cannot be converted to a number. */ public double getDouble(String key) throws JSONException { Object object = get(key); try { return object instanceof Number ? ((Number)object).doubleValue() : Double.parseDouble((String)object); } catch (NumberFormatException e) { throw new JSONException("JSONObject[" + quote(key) + "] is not a number."); } }
/** * Get the int value associated with a key. * * @param key A key string. * @return The integer value. * @throws JSONException if the key is not found or if the value cannot * be converted to an integer. */ public int getInt(String key) throws JSONException { Object object = get(key); try { return object instanceof Number ? ((Number)object).intValue() : Integer.parseInt((String)object); } catch (NumberFormatException e) { throw new JSONException("JSONObject[" + quote(key) + "] is not an int."); } }
/** * Get the long value associated with a key. * * @param key A key string. * @return The long value. * @throws JSONException if the key is not found or if the value cannot * be converted to a long. */ public long getLong(String key) throws JSONException { Object object = get(key); try { return object instanceof Number ? ((Number)object).longValue() : Long.parseLong((String)object); } catch (Exception e) { throw new JSONException("JSONObject[" + quote(key) + "] is not a long."); } }
/** * Get the JSONObject value associated with a key. * * @param key A key string. * @return A JSONObject which is the value. * @throws JSONException if the key is not found or * if the value is not a JSONObject. */ public JSONObject getJSONObject(String key) throws JSONException { Object object = get(key); if (object instanceof JSONObject) { return (JSONObject)object; } throw new JSONException("JSONObject[" + quote(key) + "] is not a JSONObject."); }
/** * Get the JSONArray value associated with a key. * * @param key A key string. * @return A JSONArray which is the value. * @throws JSONException if the key is not found or * if the value is not a JSONArray. */ public JSONArray getJSONArray(String key) throws JSONException { Object object = get(key); if (object instanceof JSONArray) { return (JSONArray)object; } throw new JSONException("JSONObject[" + quote(key) + "] is not a JSONArray."); }
/** * Get the string associated with a key. * * @param key A key string. * @return A string which is the value. * @throws JSONException if there is no string value for the key. */ public String getString(String key) throws JSONException { Object object = get(key); if (object instanceof String) { return (String)object; } throw new JSONException("JSONObject[" + quote(key) + "] not a 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 public void deserializeStrangeString() throws Exception { JsonSerDe instance = new JsonSerDe(); initialize(instance); System.out.println("deserialize"); Writable w = new Text("{\"one\":\"\\a\\v\"}"); JSONObject result = (JSONObject) instance.deserialize(w); assertEquals("",result.get("one").toString().trim()); } }
/** * 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"); }
@Test public void testTimestampDeSerializeWithPlusHours() throws Exception { // Test that timestamp object can be deserialized Writable w = new Text("{\"one\":true,\"five\":\"2017-08-17T10:46:04+0300\"}"); JSONObject result = (JSONObject) instance.deserialize(w); StructObjectInspector soi = (StructObjectInspector) instance.getObjectInspector(); JavaStringTimestampObjectInspector jstOi = (JavaStringTimestampObjectInspector) soi.getStructFieldRef("five").getFieldObjectInspector(); assertEquals( Timestamp.valueOf("2017-08-17 07:46:04.0"), jstOi.getPrimitiveJavaObject(result.get("five"))); }
@Test public void testUTCTimestampDeSerialize() throws Exception { // Test that timestamp object can be deserialized Writable w = new Text("{\"one\":true,\"five\":\"2013-03-27T23:18:40Z\"}"); JSONObject result = (JSONObject) instance.deserialize(w); StructObjectInspector soi = (StructObjectInspector) instance.getObjectInspector(); JavaStringTimestampObjectInspector jstOi = (JavaStringTimestampObjectInspector) soi.getStructFieldRef("five").getFieldObjectInspector(); assertEquals(Timestamp.valueOf("2013-03-27 23:18:40.0"), jstOi.getPrimitiveJavaObject(result.get("five"))); }
@Test public void testTimestampDeSerialize() throws Exception { // Test that timestamp object can be deserialized Writable w = new Text("{\"one\":true,\"five\":\"2013-03-27 23:18:40\"}"); JSONObject result = (JSONObject) instance.deserialize(w); StructObjectInspector soi = (StructObjectInspector) instance.getObjectInspector(); JavaStringTimestampObjectInspector jstOi = (JavaStringTimestampObjectInspector) soi.getStructFieldRef("five").getFieldObjectInspector(); assertEquals(Timestamp.valueOf("2013-03-27 23:18:40.0"), jstOi.getPrimitiveJavaObject(result.get("five"))); }
@Test public void testTimestampDeSerializeWithNanoseconds() throws Exception { // Test that timestamp object can be deserialized Writable w = new Text("{\"one\":true,\"five\":\"2013-03-27 23:18:40.123456\"}"); JSONObject result = (JSONObject) instance.deserialize(w); StructObjectInspector soi = (StructObjectInspector) instance.getObjectInspector(); JavaStringTimestampObjectInspector jstOi = (JavaStringTimestampObjectInspector) soi.getStructFieldRef("five").getFieldObjectInspector(); assertEquals( Timestamp.valueOf("2013-03-27 23:18:40.123456"), jstOi.getPrimitiveJavaObject(result.get("five"))); }
@Test public void testTimestampDeSerializeNumericTimestampWithNanoseconds() throws Exception { // Test that timestamp object can be deserialized Writable w = new Text("{\"one\":true,\"five\":1367801925.123}"); // JSONObject result = (JSONObject) instance.deserialize(w); StructObjectInspector soi = (StructObjectInspector) instance.getObjectInspector(); JavaStringTimestampObjectInspector jstOi = (JavaStringTimestampObjectInspector) soi.getStructFieldRef("five").getFieldObjectInspector(); assertEquals(getDate("2013-05-05 17:58:45.123"), jstOi.getPrimitiveJavaObject(result.get("five")) ); }
@Test public void testTimestampDeSerializeNumericTimestampWithMilliseconds() throws Exception { // Test that timestamp object can be deserialized Writable w = new Text("{\"one\":true,\"five\":1367801925123}"); // JSONObject result = (JSONObject) instance.deserialize(w); StructObjectInspector soi = (StructObjectInspector) instance.getObjectInspector(); JavaStringTimestampObjectInspector jstOi = (JavaStringTimestampObjectInspector) soi.getStructFieldRef("five").getFieldObjectInspector(); assertEquals(getDate("2013-05-05 17:58:45.123"), jstOi.getPrimitiveJavaObject(result.get("five")) ); }
@Test public void testTimestampDeSerializeNumericTimestamp() throws Exception { // Test that timestamp object can be deserialized Writable w = new Text("{\"one\":true,\"five\":1367801925}"); JSONObject result = (JSONObject) instance.deserialize(w); StructObjectInspector soi = (StructObjectInspector) instance.getObjectInspector(); JavaStringTimestampObjectInspector jstOi = (JavaStringTimestampObjectInspector) soi.getStructFieldRef("five").getFieldObjectInspector(); assertEquals(getDate("2013-05-05 17:58:45.0" ), jstOi.getPrimitiveJavaObject(result.get("five")) ); }