JsonGenerator extends Generator { @Override public void generate(ProgramTypeRegistry registry) throws IOException, GeneratorException { ProgramType doc = registry.getProgramType(); if (doc.hasIncludes()) { doc = doc.mutate() .setIncludes(doc.getIncludes() .stream() .map(path -> path.replaceAll("(\\.thrift)$", ".json")) .collect(Collectors.toList())) .build(); } OutputStream out = getFileManager().create(null, doc.getProgramName() + ".json"); try { serializer.serialize(out, doc); out.write('\n'); } catch (SerializerException e) { throw new GeneratorException("Unable to serialize document.", e); } getFileManager().finalize(out); } JsonGenerator(FileManager fileManager); @Override void generate(ProgramTypeRegistry registry);  }
@Test public void testGenerate() throws GeneratorException, IOException { ResourceUtils.copyResourceTo("/net/morimekta/providence/generator/format/json/test.thrift", tmp.getRoot()); ResourceUtils.copyResourceTo("/net/morimekta/providence/generator/format/json/included.thrift", tmp.getRoot()); File file = new File(tmp.getRoot(), "test.thrift"); generator.generate(typeLoader.load(file)); File test = new File(out, "test.json"); assertTrue(test.exists()); assertEquals("{\n" + " \"program_name\": \"test\",\n" + " \"includes\": [\n" + " \"included.json\"\n" + " ],\n" + " \"namespaces\": {\n" + " \"java\": \"net.morimekta.test.json.test\"\n" + " },\n" + " \"decl\": [\n" + " {\n" + " \"decl_message\": {\n" + " \"name\": \"Test\",\n" + " \"fields\": [\n" + " {\n" + " \"id\": 1,\n" + " \"requirement\": \"REQUIRED\",\n" + " \"type\": \"i32\",\n" + " \"name\": \"test\",\n" + " \"start_pos\": {\n" + " \"line_no\": 6,\n" + " \"line_pos\": 5\n" + " },\n" + " \"end_pos\": {\n" + " \"line_no\": 6,\n" + " \"line_pos\": 25\n" + " }\n" + " },\n" + " {\n" + " \"id\": 15,\n" + " \"requirement\": \"OPTIONAL\",\n" + " \"type\": \"i32\",\n" + " \"name\": \"another\",\n" + " \"start_pos\": {\n" + " \"line_no\": 7,\n" + " \"line_pos\": 5\n" + " },\n" + " \"end_pos\": {\n" + " \"line_no\": 7,\n" + " \"line_pos\": 29\n" + " }\n" + " },\n" + " {\n" + " \"id\": 2,\n" + " \"type\": \"included.Included\",\n" + " \"name\": \"included\",\n" + " \"start_pos\": {\n" + " \"line_no\": 8,\n" + " \"line_pos\": 5\n" + " },\n" + " \"end_pos\": {\n" + " \"line_no\": 8,\n" + " \"line_pos\": 34\n" + " }\n" + " }\n" + " ],\n" + " \"start_pos\": {\n" + " \"line_no\": 5,\n" + " \"line_pos\": 1\n" + " },\n" + " \"end_pos\": {\n" + " \"line_no\": 9,\n" + " \"line_pos\": 2\n" + " }\n" + " }\n" + " }\n" + " ]\n" + "}\n", IOUtils.readString(new FileInputStream(test))); }