Class MapperUtil

java.lang.Object
io.github.eggy03.ferrumx.windows.mapping.MapperUtil

public class MapperUtil extends Object
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.

Intended usage: This utility is primarily designed to support the internal service classes in 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 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 null
      objectClass - 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 an Optional object 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 null
      objectClass - the class of the object; must not be null
      Returns:
      an Optional containing the deserialized object, or empty if null
      Throws:
      com.google.gson.JsonSyntaxException - if the JSON is malformed