Skip to content

[WIP] Update ACAT to load JSON configurations instead of XML#170

Closed
Copilot wants to merge 1 commit intomasterfrom
copilot/update-json-configuration-loader
Closed

[WIP] Update ACAT to load JSON configurations instead of XML#170
Copilot wants to merge 1 commit intomasterfrom
copilot/update-json-configuration-loader

Conversation

Copy link
Contributor

Copilot AI commented Feb 5, 2026

Thanks for assigning this issue to me. I'm starting to work on it and will keep this PR's description up to date as I form a plan and make progress.

Original prompt

This section details on the original issue you should resolve

<issue_title>[9] Update ACAT to Load JSON Configurations</issue_title>
<issue_description>Estimate: 2 days
Sprint: Week 4
Assignee: [Developer]


Description

Update ACAT's configuration loading code to read JSON files instead of XML, using the POCOs and validators from Ticket #7.

Context

  • Currently loads XML via custom parsers
  • Need to switch to JSON deserialization
  • Must maintain backward compatibility during transition

Tasks

  • Identify all XML configuration loading code
  • Create JsonConfigurationLoader<T> utility
  • Update each configuration loader to use JSON
  • Add validation on load
  • Handle missing/corrupted files gracefully
  • Add fallback to defaults if config missing
  • Test with real JSON files

Acceptance Criteria

  • ✅ All configuration types load from JSON
  • ✅ Validation runs on load
  • ✅ Invalid config shows user-friendly error
  • ✅ Missing config falls back to defaults
  • ✅ No references to XML loading remain
  • ✅ Application runs with JSON configs
  • ✅ All existing features work

Implementation Example

// JsonConfigurationLoader.cs
public class JsonConfigurationLoader<T> where T : class
{
    private readonly ILogger<JsonConfigurationLoader<T>> _logger;
    private readonly IValidator<T> _validator;
    
    public JsonConfigurationLoader(
        ILogger<JsonConfigurationLoader<T>> logger,
        IValidator<T> validator = null)
    {
        _logger = logger;
        _validator = validator;
    }
    
    public async Task<T> LoadAsync(string filePath)
    {
        try
        {
            _logger.LogInformation("Loading configuration from {FilePath}", filePath);
            
            if (!File.Exists(filePath))
            {
                _logger.LogWarning("Configuration file not found: {FilePath}", filePath);
                return null;
            }
            
            var json = await File.ReadAllTextAsync(filePath);
            var config = JsonSerializer.Deserialize<T>(json, new JsonSerializerOptions
            {
                PropertyNameCaseInsensitive = true,
                AllowTrailingCommas = true,
                ReadCommentHandling = JsonCommentHandling.Skip
            });
            
            if (config == null)
            {
                throw new InvalidOperationException("Failed to deserialize configuration");
            }
            
            // Validate if validator provided
            if (_validator != null)
            {
                var validationResult = await _validator.ValidateAsync(config);
                if (!validationResult.IsValid)
                {
                    var errors = string.Join(", ", validationResult.Errors.Select(e => e.ErrorMessage));
                    throw new ValidationException($"Configuration validation failed: {errors}");
                }
            }
            
            _logger.LogInformation("Configuration loaded and validated successfully");
            return config;
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Failed to load configuration from {FilePath}", filePath);
            throw;
        }
    }
    
    public T LoadOrDefault(string filePath, Func<T> defaultFactory)
    {
        try
        {
            return LoadAsync(filePath).GetAwaiter().GetResult();
        }
        catch
        {
            _logger.LogWarning("Using default configuration");
            return defaultFactory();
        }
    }
}

// Usage in ActuatorManager
public class ActuatorManager
{
    private readonly JsonConfigurationLoader<ActuatorSettings> _configLoader;
    
    public ActuatorManager(JsonConfigurationLoader<ActuatorSettings> configLoader)
    {
        _configLoader = configLoader;
    }
    
    public async Task InitializeAsync()
    {
        var configPath = Path.Combine(
            FileUtils.GetUserConfigDir(), 
            "ActuatorSettings.json");
        
        var settings = await _configLoader.LoadAsync(configPath) 
            ?? ActuatorSettings.CreateDefault();
        
        // Use settings...
    }
}

Migration Path

// Temporary: Support both XML and JSON during transition
public async Task<ActuatorSettings> LoadSettingsAsync()
{
    var jsonPath = "ActuatorSettings.json";
    var xmlPath = "ActuatorSettings.xml";
    
    // Try JSON first
    if (File.Exists(jsonPath))
    {
        return await _jsonLoader.LoadAsync(jsonPath);
    }
    
    // Fallback to XML if JSON doesn't exist
    if (File.Exists(xmlPath))
    {
        _logger.LogWarning("XML config found. Please migrate to JSON using ConfigMigrationTool");
        var xmlSettings = LoadXml(xmlPath); // Old method
        
        // Auto-migrate
  ...

</details>



<!-- START COPILOT CODING AGENT SUFFIX -->

- Fixes intel/acat#160

<!-- START COPILOT CODING AGENT TIPS -->
---Let Copilot coding agent [set things up for you](https://github.com/intel/acat/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot) — coding agent works faster and does higher quality work when set up for your repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants