Class JsonUtils

java.lang.Object
frc.robot.lib.BLine.JsonUtils

public class JsonUtils extends Object
Utility class for loading and parsing path data from JSON files.

This class provides methods to load Path objects from JSON files stored in the robot's deploy directory. It supports loading individual paths as well as global constraint configurations.

File Structure

The expected directory structure under the deploy directory is:

 deploy/
   autos/
     config.json          (global constraints configuration)
     paths/
       myPath.json        (individual path files)
       otherPath.json
 

Path JSON Format

Path JSON files contain:

  • path_elements: Array of translation, rotation, and waypoint targets
  • constraints: Optional path-specific velocity/acceleration constraints
  • default_global_constraints: Optional override for global constraints

Usage Examples


 // Load a path from the default autos directory
 Path path = JsonUtils.loadPath("myPath.json");
 
 // Load a path from a custom directory
 Path path = JsonUtils.loadPath(new File("/custom/dir"), "myPath.json");
 
 // Load global constraints only
 Path.DefaultGlobalConstraints globals = JsonUtils.loadGlobalConstraints(JsonUtils.PROJECT_ROOT);
 
See Also:
  • Field Details

    • PROJECT_ROOT

      public static final File PROJECT_ROOT
      The default project root directory for auto routines.

      This is set to the "autos" subdirectory within the robot's deploy directory. Path files should be placed in a "paths" subdirectory within this location.

  • Constructor Details

    • JsonUtils

      public JsonUtils()
  • Method Details

    • loadPath

      public static Path loadPath(File autosDir, String pathFileName)
      Loads a path from a JSON file in the specified autos directory.

      The path file should be located in a "paths" subdirectory within the autos directory. Global constraints are loaded from a "config.json" file in the autos directory.

      Parameters:
      autosDir - The directory containing the autos (with paths/ subdirectory)
      pathFileName - The name of the path file (including .json extension)
      Returns:
      The loaded Path object
      Throws:
      RuntimeException - if the file cannot be read or parsed
    • loadPath

      public static Path loadPath(org.json.simple.JSONObject json, Path.DefaultGlobalConstraints defaultGlobalConstraints)
      Loads a path from a pre-parsed JSON object with specified global constraints.
      Parameters:
      json - The parsed JSON object representing the path
      defaultGlobalConstraints - The default global constraints to use
      Returns:
      The loaded Path object
    • loadPath

      public static Path loadPath(String pathFileName)
      Loads a path from a JSON file in the default project root directory.

      This is equivalent to calling loadPath(PROJECT_ROOT, pathFileName).

      Parameters:
      pathFileName - The name of the path file (including .json extension)
      Returns:
      The loaded Path object
      Throws:
      RuntimeException - if the file cannot be read or parsed
      See Also:
    • loadPathFromJsonString

      public static Path loadPathFromJsonString(String pathJson, Path.DefaultGlobalConstraints defaultGlobalConstraints)
      Loads a path from a JSON string with specified global constraints.

      This method is useful when the JSON data comes from a source other than a file, such as network communication or embedded resources.

      Parameters:
      pathJson - The JSON string representing the path
      defaultGlobalConstraints - The default global constraints to use
      Returns:
      The loaded Path object
      Throws:
      RuntimeException - if the JSON string cannot be parsed
    • parsePathComponents

      public static JsonUtils.ParsedPathComponents parsePathComponents(org.json.simple.JSONObject pathJson, Path.DefaultGlobalConstraints defaultGlobalConstraints)
      Parses a path JSON object into components without constructing a Path.

      This method is useful for performance measurements where you want to separate JSON parsing from Path construction, or when you need to inspect the parsed data before creating a Path.

      Parameters:
      pathJson - The JSON object representing the path
      defaultGlobalConstraints - Optional default global constraints (can be null, in which case constraints will be loaded from config)
      Returns:
      ParsedPathComponents containing elements, constraints, and globals
    • loadGlobalConstraints

      public static Path.DefaultGlobalConstraints loadGlobalConstraints(File autosDir)
      Loads global constraints from a config.json file in the specified directory.

      The config.json file should contain default values for all constraint types:

      • default_max_velocity_meters_per_sec
      • default_max_acceleration_meters_per_sec2
      • default_max_velocity_deg_per_sec
      • default_max_acceleration_deg_per_sec2
      • default_end_translation_tolerance_meters
      • default_end_rotation_tolerance_deg
      • default_intermediate_handoff_radius_meters
      Parameters:
      autosDir - The directory containing config.json
      Returns:
      DefaultGlobalConstraints loaded from the config file
      Throws:
      RuntimeException - if the config file cannot be read or parsed