/
siman_coder
/
JUnit
Обзор
Документация
Войти
/
siman_coder
/
JUnit
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
java/org/example/CsvJsonParser.java
142 строки
5 KB
siman_coder
Create: CsvJsonParser.java, Employee.java, Main.java, CsvJsonParserTest.java, build.gradle, data.csv, data.json, data.xml, data2.json
07 июл 2026, 11:23
Верифицирован
07 июл 2026, 11:23
1a342fc
Код
Авторство
О чём код?
package org.example; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.reflect.TypeToken; import com.opencsv.CSVReader; import com.opencsv.bean.ColumnPositionMappingStrategy; import com.opencsv.bean.CsvToBean; import com.opencsv.bean.CsvToBeanBuilder; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.lang.reflect.Type; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; public class CsvJsonParser { // Парсинг CSV public static List<Employee> parseCSV(String[] columnMapping, String fileName) { List<Employee> employees = null; try (CSVReader csvReader = new CSVReader(new FileReader(fileName))) { ColumnPositionMappingStrategy<Employee> strategy = new ColumnPositionMappingStrategy<>(); strategy.setType(Employee.class); strategy.setColumnMapping(columnMapping); CsvToBean<Employee> csvToBean = new CsvToBeanBuilder<Employee>(csvReader) .withMappingStrategy(strategy) .build(); employees = csvToBean.parse(); } catch (IOException e) { e.printStackTrace(); return null; } return employees; } // Парсинг XML (ручной) public static List<Employee> parseXML(String fileName) { List<Employee> employees = new ArrayList<>(); try { if (!Files.exists(Paths.get(fileName))) { return employees; } String xmlContent = new String(Files.readAllBytes(Paths.get(fileName))); String[] parts = xmlContent.split("</employee>"); for (String part : parts) { if (part.contains("<employee>")) { String idStr = extractTagContent(part, "id"); String firstName = extractTagContent(part, "firstName"); String lastName = extractTagContent(part, "lastName"); String country = extractTagContent(part, "country"); String ageStr = extractTagContent(part, "age"); if (idStr != null && firstName != null && lastName != null && country != null && ageStr != null) { long id = Long.parseLong(idStr.trim()); int age = Integer.parseInt(ageStr.trim()); Employee employee = new Employee(id, firstName, lastName, country, age); employees.add(employee); } } } } catch (Exception e) { e.printStackTrace(); return null; } return employees; } private static String extractTagContent(String xml, String tag) { String openTag = "<" + tag + ">"; String closeTag = "</" + tag + ">"; int startIndex = xml.indexOf(openTag); if (startIndex == -1) { return null; } startIndex += openTag.length(); int endIndex = xml.indexOf(closeTag, startIndex); if (endIndex == -1) { return null; } return xml.substring(startIndex, endIndex).trim(); } // Преобразование списка в JSON public static String listToJson(List<Employee> list) { GsonBuilder builder = new GsonBuilder().setPrettyPrinting(); Gson gson = builder.create(); Type listType = new TypeToken<List<Employee>>() {}.getType(); return gson.toJson(list, listType); } // Парсинг JSON в список public static List<Employee> jsonToList(String fileName) { List<Employee> employees = new ArrayList<>(); try { if (!Files.exists(Paths.get(fileName))) { return employees; } String jsonContent = new String(Files.readAllBytes(Paths.get(fileName))); Gson gson = new Gson(); Type listType = new TypeToken<List<Employee>>() {}.getType(); employees = gson.fromJson(jsonContent, listType); } catch (IOException e) { e.printStackTrace(); return null; } return employees; } // Запись JSON в файл public static boolean writeString(String json, String fileName) { try (FileWriter fileWriter = new FileWriter(fileName)) { fileWriter.write(json); return true; } catch (IOException e) { e.printStackTrace(); return false; } } }