Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
G
Grasscutter
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
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
ziqian zhang
Grasscutter
Commits
c6323e97
Commit
c6323e97
authored
3 years ago
by
AnimeGitB
Committed by
Luke H-W
3 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Funnel all gson calls into helper functions
Add deprecated getGsonFactory for plugin compat until 3.0
parent
76fcbb47
No related merge requests found
Changes
21
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/emu/grasscutter/utils/Utils.java
+53
-3
53 additions, 3 deletions
src/main/java/emu/grasscutter/utils/Utils.java
with
53 additions
and
3 deletions
src/main/java/emu/grasscutter/utils/Utils.java
+
53
−
3
View file @
c6323e97
...
@@ -20,12 +20,25 @@ import it.unimi.dsi.fastutil.ints.IntList;
...
@@ -20,12 +20,25 @@ import it.unimi.dsi.fastutil.ints.IntList;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
com.google.gson.Gson
;
import
com.google.gson.GsonBuilder
;
import
com.google.gson.JsonElement
;
import
com.google.gson.JsonSyntaxException
;
import
com.google.gson.reflect.TypeToken
;
import
javax.annotation.Nullable
;
import
javax.annotation.Nullable
;
import
static
emu
.
grasscutter
.
utils
.
Language
.
translate
;
import
static
emu
.
grasscutter
.
utils
.
Language
.
translate
;
@SuppressWarnings
({
"UnusedReturnValue"
,
"BooleanMethodIsAlwaysInverted"
})
@SuppressWarnings
({
"UnusedReturnValue"
,
"BooleanMethodIsAlwaysInverted"
})
public
final
class
Utils
{
public
final
class
Utils
{
private
static
final
Gson
gson
=
new
GsonBuilder
().
setPrettyPrinting
().
create
();
@Deprecated
(
forRemoval
=
true
)
public
static
Gson
getGsonFactory
()
{
return
gson
;
}
public
static
final
Random
random
=
new
Random
();
public
static
final
Random
random
=
new
Random
();
public
static
int
randomRange
(
int
min
,
int
max
)
{
public
static
int
randomRange
(
int
min
,
int
max
)
{
...
@@ -158,8 +171,7 @@ public final class Utils {
...
@@ -158,8 +171,7 @@ public final class Utils {
* @param object The object to log.
* @param object The object to log.
*/
*/
public
static
void
logObject
(
Object
object
)
{
public
static
void
logObject
(
Object
object
)
{
String
asJson
=
Grasscutter
.
getGsonFactory
().
toJson
(
object
);
Grasscutter
.
getLogger
().
info
(
jsonEncode
(
object
));
Grasscutter
.
getLogger
().
info
(
asJson
);
}
}
/**
/**
...
@@ -357,6 +369,44 @@ public final class Utils {
...
@@ -357,6 +369,44 @@ public final class Utils {
return
Base64
.
getDecoder
().
decode
(
toDecode
);
return
Base64
.
getDecoder
().
decode
(
toDecode
);
}
}
/*
* Encode an object to a JSON string
*/
public
static
String
jsonEncode
(
Object
object
)
{
return
gson
.
toJson
(
object
);
}
public
static
<
T
>
T
jsonDecode
(
JsonElement
jsonElement
,
Class
<
T
>
classType
)
throws
JsonSyntaxException
{
return
gson
.
fromJson
(
jsonElement
,
classType
);
}
public
static
<
T
>
T
loadJsonToClass
(
InputStreamReader
fileReader
,
Class
<
T
>
classType
)
throws
IOException
{
return
gson
.
fromJson
(
fileReader
,
classType
);
}
public
static
<
T
>
T
loadJsonToClass
(
String
filename
,
Class
<
T
>
classType
)
throws
IOException
{
try
(
InputStreamReader
fileReader
=
new
InputStreamReader
(
new
FileInputStream
(
Utils
.
toFilePath
(
filename
)),
StandardCharsets
.
UTF_8
))
{
return
loadJsonToClass
(
fileReader
,
classType
);
}
}
public
static
<
T
>
List
<
T
>
loadJsonToList
(
InputStreamReader
fileReader
,
Class
<
T
>
classType
)
throws
IOException
{
return
gson
.
fromJson
(
fileReader
,
TypeToken
.
getParameterized
(
List
.
class
,
classType
).
getType
());
}
public
static
<
T
>
List
<
T
>
loadJsonToList
(
String
filename
,
Class
<
T
>
classType
)
throws
IOException
{
try
(
InputStreamReader
fileReader
=
new
InputStreamReader
(
new
FileInputStream
(
Utils
.
toFilePath
(
filename
)),
StandardCharsets
.
UTF_8
))
{
return
loadJsonToList
(
fileReader
,
classType
);
}
}
public
static
<
T1
,
T2
>
Map
<
T1
,
T2
>
loadJsonToMap
(
InputStreamReader
fileReader
,
Class
<
T1
>
keyType
,
Class
<
T2
>
valueType
)
throws
IOException
{
return
gson
.
fromJson
(
fileReader
,
TypeToken
.
getParameterized
(
Map
.
class
,
keyType
,
valueType
).
getType
());
}
public
static
<
T1
,
T2
>
Map
<
T1
,
T2
>
loadJsonToMap
(
String
filename
,
Class
<
T1
>
keyType
,
Class
<
T2
>
valueType
)
throws
IOException
{
try
(
InputStreamReader
fileReader
=
new
InputStreamReader
(
new
FileInputStream
(
Utils
.
toFilePath
(
filename
)),
StandardCharsets
.
UTF_8
))
{
return
loadJsonToMap
(
fileReader
,
keyType
,
valueType
);
}
}
/**
/**
* Safely JSON decodes a given string.
* Safely JSON decodes a given string.
* @param jsonData The JSON-encoded data.
* @param jsonData The JSON-encoded data.
...
@@ -364,7 +414,7 @@ public final class Utils {
...
@@ -364,7 +414,7 @@ public final class Utils {
*/
*/
public
static
<
T
>
T
jsonDecode
(
String
jsonData
,
Class
<
T
>
classType
)
{
public
static
<
T
>
T
jsonDecode
(
String
jsonData
,
Class
<
T
>
classType
)
{
try
{
try
{
return
Grasscutter
.
getGsonFactory
()
.
fromJson
(
jsonData
,
classType
);
return
gson
.
fromJson
(
jsonData
,
classType
);
}
catch
(
Exception
ignored
)
{
}
catch
(
Exception
ignored
)
{
return
null
;
return
null
;
}
}
...
...
This diff is collapsed.
Click to expand it.
Prev
1
2
Next
Preview
0%
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