Skip to content
Snippets Groups Projects
Commit 817bf11d authored by Mamadu-lamarana Bah's avatar Mamadu-lamarana Bah :speech_balloon:
Browse files

reduction complexité cyclomatique dans UtilDeserialisationJson

parent 597f6435
No related branches found
No related tags found
No related merge requests found
...@@ -11,14 +11,14 @@ public class UtilDeserialisationJson { ...@@ -11,14 +11,14 @@ public class UtilDeserialisationJson {
private UtilDeserialisationJson() { private UtilDeserialisationJson() {
throw new AssertionError("La classe UtilDeserialisationJson ne doit pas être instanciée."); throw new AssertionError("La classe UtilDeserialisationJson ne doit pas être instanciée.");
} }
// Désérialise une liste d'objets de type T à partir d'une chaîne JSON. // Désérialise une liste d'objets de type T à partir d'une chaîne JSON.
public static <T> List<T> deserialiserListe(String json, Class<T> clazz) throws Exception { public static <T> List<T> deserialiserListe(String json, Class<T> clazz) throws Exception {
JsonReader jr = new JsonReader(new StringReader(json)); JsonReader jr = new JsonReader(new StringReader(json));
List<T> resultat = new ArrayList<>(); List<T> resultat = new ArrayList<>();
jr.beginArray(); jr.beginArray();
while (jr.hasNext()) { while (jr.hasNext()) {
T element = deserialiserObjet(jr, clazz); resultat.add(deserialiserObjet(jr, clazz));
resultat.add(element);
} }
jr.endArray(); jr.endArray();
return resultat; return resultat;
...@@ -29,26 +29,40 @@ public class UtilDeserialisationJson { ...@@ -29,26 +29,40 @@ public class UtilDeserialisationJson {
T objet = clazz.getDeclaredConstructor().newInstance(); T objet = clazz.getDeclaredConstructor().newInstance();
jr.beginObject(); jr.beginObject();
while (jr.hasNext()) { while (jr.hasNext()) {
String nom = jr.nextName(); String name = jr.nextName();
for (Field champ : clazz.getDeclaredFields()) { Field champ = chercherChamp(clazz, name);
if (champ.getName().equals(nom)) { if (champ != null) {
Class<?> typeChamp = champ.getType(); affecterChamp(jr, objet, champ);
champ.setAccessible(true); // Pour accéder aux champs privés
if (typeChamp.equals(long.class)) {
champ.setLong(objet, jr.nextLong());
} else if (typeChamp.equals(int.class)) {
champ.setInt(objet, jr.nextInt());
} else if (typeChamp.equals(boolean.class)) {
champ.setBoolean(objet, jr.nextBoolean());
} else if (typeChamp.equals(String.class)) {
champ.set(objet, jr.nextString());
} else {
throw new RuntimeException("Type inattendu: " + typeChamp + ", nom: " + nom);
}
}
} }
} }
jr.endObject(); jr.endObject();
return objet; return objet;
} }
// Cherche le champ correspondant dans la classe spécifiée par son nom.
private static Field chercherChamp(Class<?> clazz, String nomChamp) {
for (Field champ : clazz.getDeclaredFields()) {
if (champ.getName().equals(nomChamp)) {
return champ;
}
}
return null;
}
// Affecte la valeur lue depuis le JsonReader au champ de l'objet.
private static void affecterChamp(JsonReader jr, Object objet, Field champ) throws Exception {
Class<?> typeChamp = champ.getType();
champ.setAccessible(true); // Pour accéder aux champs privés
if (typeChamp.equals(long.class)) {
champ.setLong(objet, jr.nextLong());
} else if (typeChamp.equals(int.class)) {
champ.setInt(objet, jr.nextInt());
} else if (typeChamp.equals(boolean.class)) {
champ.setBoolean(objet, jr.nextBoolean());
} else if (typeChamp.equals(String.class)) {
champ.set(objet, jr.nextString());
} else {
throw new RuntimeException("Type inattendu: " + typeChamp + ", nom: " + champ.getName());
}
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment