JsonRecords { public static List<MapRecord> parseAsMap(File jsonFile) throws FileNotFoundException, IOException, JsonSyntaxException { return parseAsMap(jsonFile, new CountingDocIdGenerator()); }  static List<MapRecord> parseAsMap(File jsonFile); static List<MultimapRecord> parseAsMultimap(File jsonFile); static List<MapRecord> parseAsMap(File jsonFile, DocIdGenerator generator); static List<MultimapRecord> parseAsMultimap(File jsonFile, DocIdGenerator generator); static List<MapRecord> parseAsMap(String json); static List<MultimapRecord> parseAsMultimap(String json); static List<MapRecord> parseAsMap(String json, DocIdGenerator generator); static List<MultimapRecord> parseAsMultimap(String json, DocIdGenerator generator);  }
@Test public void simpleJsonTest() { String json = "[{'foo1':'bar1', 'foo2':'bar2'}, {'foo3':'bar3', 'foo4':'bar4'}]"; List<MapRecord> records = JsonRecords.parseAsMap(json); Assert.assertEquals(2, records.size()); Assert.assertEquals( new MapRecord(ImmutableMap.<Column,RecordValue<?>> of(Column.create("foo1"), RecordValue.create("bar1"), Column.create("foo2"), RecordValue.create("bar2")), "0", Defaults.EMPTY_VIS), records.get(0)); Assert.assertEquals( new MapRecord(ImmutableMap.<Column,RecordValue<?>> of(Column.create("foo3"), RecordValue.create("bar3"), Column.create("foo4"), RecordValue.create("bar4")), "1", Defaults.EMPTY_VIS), records.get(1)); }
@Test(expected = JsonParseException.class) public void invalidJsonMap() { String json = "[{'foo1"; JsonRecords.parseAsMap(json); }
@Test(expected = JsonParseException.class) public void nonSupportedJsonMap() { String json = "{'foo1':'bar1'}"; JsonRecords.parseAsMap(json); }
@Test public void mixedNumericJson() { String json = "[ {'foo1':'bar1', 'foo2':2} ]"; List<MapRecord> records = JsonRecords.parseAsMap(json); Assert.assertEquals(1, records.size()); MapRecord record = records.get(0); Column c = Column.create("foo1"); Assert.assertTrue("Map does not contain key: " + record, record.containsKey(c)); Assert.assertEquals(StringRecordValue.class, record.get(c).getClass()); Assert.assertEquals("bar1", record.get(c).value()); c = Column.create("foo2"); Assert.assertTrue("Map does not contain key: " + record, record.containsKey(c)); Assert.assertEquals(IntegerRecordValue.class, record.get(c).getClass()); Assert.assertEquals(2, record.get(c).value()); }
@Test public void longJson() { String json = "[ {'foo2':" + Long.MAX_VALUE + "} ]"; List<MapRecord> records = JsonRecords.parseAsMap(json); Assert.assertEquals(1, records.size()); MapRecord record = records.get(0); Column c = Column.create("foo2"); Assert.assertTrue("Map does not contain key: " + record, record.containsKey(c)); Assert.assertEquals(LongRecordValue.class, record.get(c).getClass()); Assert.assertEquals(Long.MAX_VALUE, record.get(c).value()); }
@Test public void duplicateKeysInMap() { String json = "[ {'foo':1, 'foo':2} ]"; MapRecord record = JsonRecords.parseAsMap(json).get(0); Assert.assertEquals(2, record.get(Column.create("foo")).value()); }