/** * Construct a JSONObject from a ResourceBundle. * * @param baseName The ResourceBundle base name. * @param locale The Locale to load the ResourceBundle for. * @throws JSONException If any JSONExceptions are detected. */ public JSONObject(String baseName, Locale locale) throws JSONException { this(); ResourceBundle bundle = ResourceBundle.getBundle(baseName, locale, Thread.currentThread().getContextClassLoader()); // Iterate through the keys in the bundle. Enumeration keys = bundle.getKeys(); while (keys.hasMoreElements()) { Object key = keys.nextElement(); if (key instanceof String) { // Go through the path, ensuring that there is a nested JSONObject for each // segment except the last. Add the value using the last segment's name into // the deepest nested JSONObject. String[] path = ((String) key).split("\\."); int last = path.length - 1; JSONObject target = this; for (int i = 0; i < last; i += 1) { String segment = path[i]; JSONObject nextTarget = target.optJSONObject(segment); if (nextTarget == null) { nextTarget = new JSONObject(); target.put(segment.toLowerCase(), nextTarget); } target = nextTarget; } target.put(path[last].toLowerCase(), bundle.getString((String) key)); } } }
/** * Serializing means getting every field, and setting the appropriate JSONObject field. Actual * serialization is done at the end when the whole JSON object is built * * @param serializer * @param obj * @param structObjectInspector */ private JSONObject serializeStruct( Object obj, StructObjectInspector soi, List<String> columnNames) { // do nothing for null struct if (null == obj) { return null; } JSONObject result = new JSONObject(); List<? extends StructField> fields = soi.getAllStructFieldRefs(); for (int i = 0; i < fields.size(); i++) { StructField sf = fields.get(i); Object data = soi.getStructFieldData(obj, sf); if (null != data) { try { // we want to serialize columns with their proper HIVE name, // not the _col2 kind of name usually generated upstream result.put( getSerializedFieldName(columnNames, i, sf), serializeField(data, sf.getFieldObjectInspector())); } catch (JSONException ex) { LOG.warn("Problem serializing", ex); throw new RuntimeException(ex); } } } return result; }
/** * Get an array of field names from a JSONObject. * * @return An array of field names, or null if there are no names. */ public static String[] getNames(JSONObject jo) { int length = jo.length(); if (length == 0) { return null; } Iterator iterator = jo.keys(); String[] names = new String[length]; int i = 0; while (iterator.hasNext()) { names[i] = (String) iterator.next(); i += 1; } return names; }
/** * Write the contents of the JSONObject as JSON text to a writer. For compactness, no whitespace * is added. * * <p>Warning: This method assumes that the data structure is acyclical. * * @return The writer. * @throws JSONException */ public Writer write(Writer writer) throws JSONException { try { boolean commanate = false; Iterator keys = this.keys(); writer.write('{'); while (keys.hasNext()) { if (commanate) { writer.write(','); } Object key = keys.next(); writer.write(quote(key.toString())); writer.write(':'); Object value = this.map.get(key); if (value instanceof JSONObject) { ((JSONObject) value).write(writer); } else if (value instanceof JSONArray) { ((JSONArray) value).write(writer); } else { writer.write(valueToString(value)); } commanate = true; } writer.write('}'); return writer; } catch (IOException exception) { throw new JSONException(exception); } }
/** * Hive will call this to serialize an object. Returns a writable object of the same class * returned by <a href="#getSerializedClass">getSerializedClass</a> * * @param obj The object to serialize * @param objInspector The ObjectInspector that knows about the object's structure * @return a serialized object in form of a Writable. Must be the same type returned by <a * href="#getSerializedClass">getSerializedClass</a> * @throws SerDeException */ @Override public Writable serialize(Object obj, ObjectInspector objInspector) throws SerDeException { // make sure it is a struct record if (objInspector.getCategory() != Category.STRUCT) { throw new SerDeException( getClass().toString() + " can only serialize struct types, but we got: " + objInspector.getTypeName()); } JSONObject serializer = serializeStruct(obj, (StructObjectInspector) objInspector, columnNames); Text t = new Text(serializer.toString()); serializedDataSize = t.getBytes().length; return t; }
/** * Construct a JSONObject from a subset of another JSONObject. An array of strings is used to * identify the keys that should be copied. Missing keys are ignored. * * @param jo A JSONObject. * @param names An array of strings. * @throws JSONException * @exception JSONException If a value is a non-finite number or if a name is duplicated. */ public JSONObject(JSONObject jo, String[] names) { this(); for (int i = 0; i < names.length; i += 1) { try { putOnce(names[i].toLowerCase(), jo.opt(names[i])); } catch (Exception ignore) { } } }
/** * Serializes a Hive map<> using a JSONObject. * * @param obj the object to serialize * @param moi the object's inspector * @return */ private JSONObject serializeMap(Object obj, MapObjectInspector moi) { if (obj == null) { return null; } JSONObject jo = new JSONObject(); Map m = moi.getMap(obj); for (Object k : m.keySet()) { try { jo.put( serializeField(k, moi.getMapKeyObjectInspector()).toString(), serializeField(m.get(k), moi.getMapValueObjectInspector())); } catch (JSONException ex) { LOG.warn("Problem serializing map"); } } return jo; }