Driver { protected static void writeLabelBindingsJSON(Writer writer, ARFFModel arffModel) throws IOException { List<Entry<String, Integer>> attributes = Lists.newArrayList(); attributes.addAll(arffModel.getLabelBindings().entrySet()); Collections.sort(attributes, new Comparator<Map.Entry<String, Integer>>() { @Override public int compare(Entry<String, Integer> t, Entry<String, Integer> t1) { return t.getValue().compareTo(t1.getValue()); } }); List<Map<String, Object>> jsonObjects = Lists.newLinkedList(); for (int i = 0; i < attributes.size(); i++) { Entry<String, Integer> modelRepresentation = attributes.get(i); Map<String, Object> jsonRepresentation = Maps.newHashMap(); jsonObjects.add(jsonRepresentation); jsonRepresentation.put("label", i < (attributes.size() - 1) ? String.valueOf(false) : String.valueOf(true)); String attribute = modelRepresentation.getKey(); jsonRepresentation.put("attribute", attribute); Map<String, Integer> nominalValues = arffModel.getNominalMap().get(attribute); if (nominalValues != null) { String[] values = nominalValues.keySet().toArray(new String[1]); jsonRepresentation.put("values", values); jsonRepresentation.put("type", "categorical"); } else { jsonRepresentation.put("type", "numerical"); } } writer.write(OBJECT_MAPPER.writeValueAsString(jsonObjects)); } private  Driver(); static void main(String[] args);  }
@Test public void dictionaryJSON() throws IOException { ARFFModel model = new MapBackedARFFModel(); ARFFVectorIterableTest.getVectors("sample-dense.arff", model); StringWriter writer = new StringWriter(); Driver.writeLabelBindingsJSON(writer, model); assertEquals(Resources.toString(Resources.getResource("expected-arff-schema.json"), Charsets.UTF_8), writer.toString()); }