GPOUtils { public static Map<String, Set<Object>> deserializeToMap(FieldsDescriptor fieldsDescriptor, JSONObject dpou) { Map<String, Set<Object>> keyToValues = Maps.newHashMap(); for (String key : fieldsDescriptor.getFields().getFields()) { if (!dpou.has(key)) { throw new IllegalArgumentException("The given key " + key + " is not contained in the given JSON"); } Set<Object> keyValues; Object keyValue; try { keyValue = dpou.get(key); } catch (JSONException ex) { throw new IllegalStateException("This should never happen", ex); } if (keyValue instanceof JSONArray) { JSONArray ja = (JSONArray)keyValue; keyValues = Sets.newHashSetWithExpectedSize(ja.length()); Type type = fieldsDescriptor.getType(key); for (int index = 0; index < ja.length(); index++) { keyValues.add(getFieldFromJSON(type, ja, index)); } } else if (keyValue instanceof JSONObject) { throw new UnsupportedOperationException("Cannot extract objects from JSONObjects"); } else { keyValues = Sets.newHashSetWithExpectedSize(1); keyValues.add(getFieldFromJSON(fieldsDescriptor, key, dpou)); } keyToValues.put(key, keyValues); } return keyToValues; } private  GPOUtils(); @SuppressWarnings("unchecked") static Map<String, Type> buildTypeMap(JSONObject jo); static GPOMutable deserialize(FieldsDescriptor fieldsDescriptor, JSONObject dpou); static Map<String, Set<Object>> deserializeToMap(FieldsDescriptor fieldsDescriptor, JSONObject dpou); static void setFieldFromJSON(GPOMutable gpo, Type type, String field, JSONArray jo, int index); static void setFieldFromJSON(GPOMutable gpo, String field, JSONObject jo); static Object getFieldFromJSON(FieldsDescriptor fd, String field, JSONObject jo); static Object getFieldFromJSON(Type type, JSONArray ja, int index); static JSONObject serializeJSONObject(GPOMutable gpo, Fields fields, ResultFormatter resultFormatter); static JSONObject serializeJSONObject(GPOMutable gpo, ResultFormatter resultFormatter); static int serializedLength(GPOMutable gpo); static byte[] serialize(GPOMutable gpo, GPOByteArrayList byteArrayList); static byte[] serialize(GPOMutable gpo, Fields excludedFields); static GPOMutable deserialize(FieldsDescriptor fd, byte[] serializedGPO, MutableInt offset); static GPOMutable deserialize(FieldsDescriptor fieldsDescriptor, Fields excludedFields, byte[] serializedGPO,
      int offset); static String deserializeString(byte[] buffer, MutableInt offset); static void serializeString(String val, byte[] buffer, MutableInt offset); static byte[] serializeString(String val); static long deserializeLong(byte[] buffer, MutableInt offset); static void serializeLong(long val, byte[] buffer, MutableInt offset); static byte[] serializeLong(long val); static long deserializeLong(byte[] buffer); static double deserializeDouble(byte[] buffer, MutableInt offset); static void serializeDouble(double valD, byte[] buffer, MutableInt offset); static byte[] serializeDouble(double valD); static int deserializeInt(byte[] buffer, MutableInt offset); static int deserializeInt(byte[] buffer); static void serializeInt(int val, byte[] buffer, MutableInt offset); static byte[] serializeInt(int val); static float deserializeFloat(byte[] buffer, MutableInt offset); static void serializeFloat(float valf, byte[] buffer, MutableInt offset); static byte[] serializeFloat(float valf); static short deserializeShort(byte[] buffer, MutableInt offset); static void serializeShort(short val, byte[] buffer, MutableInt offset); static byte[] serializeShort(short val); static byte deserializeByte(byte[] buffer, MutableInt offset); static void serializeByte(byte val, byte[] buffer, MutableInt offset); static byte[] serializeByte(byte val); static boolean deserializeBoolean(byte[] buffer, MutableInt offset); static void serializeBoolean(boolean val, byte[] buffer, MutableInt offset); static byte[] serializeBoolean(boolean val); static char deserializeChar(byte[] buffer, MutableInt offset); static void serializeChar(char val, byte[] buffer, MutableInt offset); static byte[] serializeChar(char val); @SuppressWarnings("unchecked") static T[] createGetters(List<String> fields, Map<String, String> valueToExpression,
      Class<?> clazz, Class<?> getterClazz, Class<?> getterMethodClazz); static Getter<Object, String>[] createGettersString(List<String> fields, Map<String, String> valueToExpression,
      Class<?> clazz); static Getter<Object, Object>[] createGettersObject(List<String> fields, Map<String, String> valueToExpression,
      Class<?> clazz); static GPOGetters buildGPOGetters(Map<String, String> fieldToGetter, FieldsDescriptor fieldsDescriptor,
      Class<?> clazz); static void copyPOJOToGPO(GPOMutable mutable, GPOGetters getters, Object object); static void indirectCopy(GPOMutable dest, GPOMutable src, IndexSubset indexSubset); static int hashcode(GPOMutable gpo); static int indirectHashcode(GPOMutable gpo, IndexSubset indexSubset); static boolean equals(GPOMutable dest, GPOMutable src); static boolean subsetEquals(GPOMutable dest, GPOMutable src, IndexSubset indexSubset); static boolean indirectEquals(GPOMutable dest, GPOMutable src, IndexSubset indexSubset); static void zeroFillNumeric(GPOMutable value); static IndexSubset computeSubIndices(FieldsDescriptor child, FieldsDescriptor parent); static Map<String, Object> getDestringedData(FieldsDescriptor fd, Map<String, String> stringMap); static Map<String, Object> convertToMapIntersection(GPOMutable gpo, Fields fields); static boolean insideRange(Type type, int val); static boolean numericTypeIntOrSmaller(Type type);  }
@Test @SuppressWarnings("AssertEqualsBetweenInconvertibleTypes") public void testDeserializeToMap() throws Exception { Map<String, Type> fieldToType = Maps.newHashMap(); fieldToType.put("name", Type.STRING); fieldToType.put("longvals", Type.LONG); fieldToType.put("stringvals", Type.STRING); FieldsDescriptor fd = new FieldsDescriptor(fieldToType); String json = SchemaUtils.jarResourceFileToString("deserializeToMapTest.json"); Map<String, Set<Object>> resultMap = GPOUtils.deserializeToMap(fd, new JSONObject(json)); Set<Object> names = resultMap.get("name"); Set<Object> longvals = resultMap.get("longvals"); Set<Object> stringvals = resultMap.get("stringvals"); Assert.assertEquals(Sets.newHashSet("tim"), names); Assert.assertEquals(Sets.newHashSet(1L, 2L, 3L), longvals); Assert.assertEquals(Sets.newHashSet("a", "b", "c"), stringvals); }