1. import java.io.*;
  2. import java.nio.file.*;
  3. import java.util.List;
  4. import java.util.ArrayList;
  5. import org.json.JSONArray;
  6. import org.json.JSONObject;
  7. public class RecordSerializer {
  8. /**
  9. * Serializes a list of records to a JSON file based on a configuration.
  10. * @param records The list of records to serialize.
  11. * @param configPath The path to the configuration file.
  12. * @param outputFilePath The path to the output JSON file.
  13. * @throws IOException If an I/O error occurs.
  14. */
  15. public static void serializeRecords(List<Record> records, String configPath, String outputFilePath) throws IOException {
  16. // Load configuration
  17. Configuration config = loadConfiguration(configPath);
  18. // Validate configuration
  19. if (config == null || config.getRecordFieldSelector() == null || config.getOutputFormat() == null) {
  20. throw new IllegalArgumentException("Invalid configuration file.");
  21. }
  22. // Prepare data for JSON serialization
  23. List<Record> filteredRecords = filterRecords(records, config.getRecordFieldSelector());
  24. // Convert records to JSON array
  25. JSONArray jsonArray = convertRecordsToJsonArray(filteredRecords);
  26. // Write JSON array to file
  27. try (FileWriter fileWriter = new FileWriter(outputFilePath)) {
  28. fileWriter.write(jsonArray.toString(2)); // Use 2 spaces for indentation
  29. }
  30. }
  31. /**
  32. * Loads the configuration from a JSON file.
  33. * @param configPath The path to the configuration file.
  34. * @return The configuration object.
  35. * @throws IOException If an I/O error occurs.
  36. */
  37. private static Configuration loadConfiguration(String configPath) throws IOException {
  38. try (FileReader fileReader = new FileReader(configPath)) {
  39. JSONObject configJson = new JSONObject(fileReader);
  40. return new Configuration(configJson);
  41. }
  42. }
  43. /**
  44. * Filters records based on the specified field selector in the configuration.
  45. * @param records The list of records.
  46. * @param fieldSelector The field selector from the configuration.
  47. * @return The filtered list of records.
  48. */
  49. private static List<Record> filterRecords(List<Record> records, String fieldSelector) {
  50. List<Record> filteredRecords = new ArrayList<>();
  51. for (Record record : records) {
  52. if (record.getData().containsKey(fieldSelector)) {
  53. filteredRecords.add(record);
  54. }
  55. }
  56. return filteredRecords;
  57. }
  58. /**
  59. * Converts a list of records to a JSON array.
  60. * @param records The list of records.
  61. * @return A JSONArray representing the records.
  62. */
  63. private static JSONArray convertRecordsToJsonArray(List<Record> records) {
  64. JSONArray jsonArray = new JSONArray();
  65. for (Record record : records) {
  66. JSONObject recordJson = new JSONObject(record.getData());
  67. jsonArray.put(recordJson);
  68. }
  69. return jsonArray;
  70. }
  71. // Define a simple Record class for demonstration
  72. public static class Record {
  73. private JSONObject data;
  74. public Record(JSONObject data) {
  75. this.data = data;
  76. }
  77. public JSONObject getData() {
  78. return data;
  79. }
  80. }
  81. // Define a simple Configuration class
  82. public static class Configuration {
  83. private JSONObject json;
  84. public Configuration(JSONObject json) {
  85. this.json = json;
  86. }
  87. public String getRecordFieldSelector() {
  88. return json.getString("recordFieldSelector");
  89. }
  90. public String getOutputFormat() {
  91. return json.getString("outputFormat");
  92. }
  93. }
  94. public static void main(String[] args) throws IOException {
  95. // Example usage:
  96. List<Record> records = new ArrayList<>();
  97. JSONObject record1Data = new JSONObject();
  98. record1Data.put("id", 1);
  99. record1Data.put("name", "Alice");
  100. record1Data.put("age", 30);
  101. records.add(new Record(record1Data));
  102. JSONObject record2Data = new JSONObject();
  103. record2Data.put("id", 2);
  104. record2Data.put("name", "Bob");
  105. record2Data.put("city", "New York");
  106. records.add(new Record(record2Data));
  107. JSONObject record3Data =

Add your comment