Class MapperUtil
java.lang.Object
io.github.eggy03.ferrumx.windows.mapping.MapperUtil
Utility class for mapping JSON strings to Java objects.
Provides methods to convert JSON responses (typically from PowerShell commands)
into either a List of objects or a single Optional object.
This class uses Gson for JSON deserialization.
org.ferrumx.windows.service for mapping PowerShell JSON output
to domain entities. Consumers of the library should not need to invoke these methods directly,
unless they are parsing a custom POJO not already handled by the existing service classes.
Usage example
List<Battery> batteries = MapperUtil.mapToList(jsonString, Battery.class);
Optional<Processor> processor = MapperUtil.mapToObject(jsonString, Processor.class);
- Since:
- 2.0.0
- Author:
- Egg-03
-
Method Summary
Modifier and TypeMethodDescriptionstatic <S> @NotNull List<S> Converts a JSON string into a list of objects of the specified type.static <S> @NotNull Optional<S> mapToObject(@NonNull String json, @NonNull Class<S> objectClass) Converts a JSON string into anOptionalobject of the specified type.
-
Method Details
-
mapToList
@NotNull public static <S> @NotNull List<S> mapToList(@NonNull @NonNull String json, @NonNull @NonNull Class<S> objectClass) Converts a JSON string into a list of objects of the specified type.If the JSON represents a single object, it is returned as a singleton list. If the JSON is null or empty, returns an empty list.
- Type Parameters:
S- the type of the object- Parameters:
json- the JSON string to parse; must not be nullobjectClass- the class of the objects in the list; must not be null- Returns:
- a non-null list of objects deserialized from JSON
- Throws:
com.google.gson.JsonSyntaxException- if the JSON is malformed
-
mapToObject
@NotNull public static <S> @NotNull Optional<S> mapToObject(@NonNull @NonNull String json, @NonNull @NonNull Class<S> objectClass) Converts a JSON string into anOptionalobject of the specified type.Returns
Optional.empty()if the JSON is null or cannot be parsed into an object.- Type Parameters:
S- the type of the object- Parameters:
json- the JSON string to parse; must not be nullobjectClass- the class of the object; must not be null- Returns:
- an
Optionalcontaining the deserialized object, or empty if null - Throws:
com.google.gson.JsonSyntaxException- if the JSON is malformed
-