JsonSerializer implements EntityDocumentDumpProcessor { public static String getJsonString(ItemDocument itemDocument) { return jacksonObjectToString(itemDocument); } JsonSerializer(OutputStream outputStream); @Override void open(); @Override void processItemDocument(ItemDocument itemDocument); @Override void processPropertyDocument(PropertyDocument propertyDocument); @Override void processLexemeDocument(LexemeDocument lexemeDocument); @Override void processMediaInfoDocument(MediaInfoDocument mediaInfoDocument); @Override void close(); int getEntityDocumentCount(); static String getJsonString(ItemDocument itemDocument); static String getJsonString(PropertyDocument propertyDocument); static String getJsonString(MediaInfoDocument mediaInfoDocument); static String getJsonString(Statement statement);  }
@Test public void testItemDocumentToJson() { ItemDocument id = Datamodel.makeItemDocument( Datamodel.makeWikidataItemIdValue("Q42"), Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), Collections.emptyMap()); String json = "{\"type\":\"item\",\"id\":\"Q42\",\"labels\":{},\"descriptions\":{},\"aliases\":{},\"claims\":{},\"sitelinks\":{}}"; JsonComparator.compareJsonStrings(json, JsonSerializer.getJsonString(id)); }
@Test public void testPropertyDocumentToJson() { PropertyDocument pd = Datamodel.makePropertyDocument( Datamodel.makeWikidataPropertyIdValue("P1"), Collections. emptyList(), Collections. emptyList(), Collections. emptyList(), Collections. emptyList(), Datamodel.makeDatatypeIdValue(DatatypeIdValue.DT_ITEM)); String json = "{\"id\":\"P1\",\"aliases\":{},\"labels\":{},\"descriptions\":{},\"claims\":{},\"type\":\"property\", \"datatype\":\"wikibase-item\"}"; JsonComparator.compareJsonStrings(json, JsonSerializer.getJsonString(pd)); }
@Test public void testStatementToJson() { Statement s = Datamodel.makeStatement(ItemIdValue.NULL, Datamodel.makeNoValueSnak(Datamodel.makeWikidataPropertyIdValue("P1")), Collections.emptyList(), Collections.emptyList(), StatementRank.NORMAL, "MyId"); String json = "{\"rank\":\"normal\",\"id\":\"MyId\",\"mainsnak\":{\"property\":\"P1\",\"snaktype\":\"novalue\"},\"type\":\"statement\"}"; JsonComparator.compareJsonStrings(json, JsonSerializer.getJsonString(s)); }