|
| 1 | +package com.webfuzzing.commons.faults; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | +import java.nio.file.Files; |
| 8 | +import java.nio.file.Paths; |
| 9 | +import java.util.Arrays; |
| 10 | +import java.util.Comparator; |
| 11 | +import java.util.List; |
| 12 | +import java.util.stream.Collectors; |
| 13 | + |
| 14 | +public class FaultsToJson { |
| 15 | + |
| 16 | + public static final String JSON_OUTPUT_FILEPATH = "src/main/resources/wfc/faults/fault_categories.json"; |
| 17 | + |
| 18 | + public static void main(String[] args) throws Exception { |
| 19 | + |
| 20 | + String json = getJsonFromClass(); |
| 21 | + |
| 22 | + Files.write(Paths.get(JSON_OUTPUT_FILEPATH), json.getBytes()); |
| 23 | + } |
| 24 | + |
| 25 | + public static String getJsonFromClass(){ |
| 26 | + ObjectMapper mapper = new ObjectMapper(); |
| 27 | + |
| 28 | + List<DefinedFaultCategory> faults = Arrays.stream(DefinedFaultCategory.values()) |
| 29 | + .sorted(Comparator.comparingInt(DefinedFaultCategory::getCode)) |
| 30 | + .collect(Collectors.toList()); |
| 31 | + |
| 32 | + String json = null; |
| 33 | + try { |
| 34 | + json = mapper |
| 35 | + .writerWithDefaultPrettyPrinter() |
| 36 | + .writeValueAsString(faults); |
| 37 | + } catch (JsonProcessingException e) { |
| 38 | + throw new RuntimeException(e); |
| 39 | + } |
| 40 | + |
| 41 | + return json; |
| 42 | + } |
| 43 | + |
| 44 | + public static String getJsonFromFile(){ |
| 45 | + |
| 46 | + String json = null; |
| 47 | + try { |
| 48 | + json = new String(Files.readAllBytes(Paths.get(JSON_OUTPUT_FILEPATH))); |
| 49 | + } catch (IOException e) { |
| 50 | + throw new RuntimeException(e); |
| 51 | + } |
| 52 | + return json; |
| 53 | + } |
| 54 | +} |
0 commit comments