Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
projet-part2
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Mamadu-lamarana Bah
projet-part2
Commits
817bf11d
Commit
817bf11d
authored
1 year ago
by
Mamadu-lamarana Bah
Browse files
Options
Downloads
Patches
Plain Diff
reduction complexité cyclomatique dans UtilDeserialisationJson
parent
597f6435
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
gson/metrics/src/main/java/com/google/gson/metrics/UtilDeserialisationJson.java
+33
-19
33 additions, 19 deletions
...java/com/google/gson/metrics/UtilDeserialisationJson.java
with
33 additions
and
19 deletions
gson/metrics/src/main/java/com/google/gson/metrics/UtilDeserialisationJson.java
+
33
−
19
View file @
817bf11d
...
@@ -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
());
}
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment