refactor: use JSON serialization for profiles - #29
Conversation
- Add JsonPropertyName attributes to FanCurvePoint, FanProfile for compact JSON - Add ToJson/FromJson methods to FanCurve and FanProfile - Update FanCurveEditor to use ToJson() for saving - Migrate ProfileManager to JSON-based serialization
|
Warning Rate limit exceeded
Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 9 minutes and 49 seconds. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (3)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
There was a problem hiding this comment.
Code Review
This pull request migrates the serialization of fan curves and profiles from custom string-based formats to JSON. While this modernizes the data handling, the review highlights critical concerns regarding backward compatibility, as existing user profiles and exported curves will fail to load without a migration path or fallback logic. Additionally, the Points property in FanCurve should remain an IReadOnlyList to correctly signal that the getter returns a snapshot, and error handling during deserialization should be enhanced to provide better diagnostic information instead of silently swallowing exceptions.
| try | ||
| { | ||
| var data = JsonSerializer.Deserialize<List<FanProfile>>(serialized); | ||
| if (data != null) | ||
| _profiles.AddRange(data); | ||
| } | ||
| catch (JsonException) | ||
| { | ||
| var profile = FanProfile.FromString(entry); | ||
| if (profile != null) | ||
| _profiles.Add(profile); | ||
| Debug.WriteLine("Settings are not valid JSON."); | ||
| } |
There was a problem hiding this comment.
This refactor breaks existing profile storage. The LoadProfiles method will fail to parse the legacy pipe-delimited format (name|curve|processes), causing all existing user profiles to be lost upon upgrading to this version. A migration path is necessary to convert the old string-based settings into the new JSON format to ensure users do not lose their data.
| curve.SetPoints(points); | ||
| return curve; | ||
| } | ||
| public static FanCurve FromString(string data) => FromJson(data); |
There was a problem hiding this comment.
This change breaks backward compatibility for existing fan curve files. The previous format was temperature:speed pairs separated by commas, while the new implementation expects JSON. Existing user-exported curve files will fail to load. You should implement a fallback to the legacy parsing logic if JSON deserialization fails. Additionally, this line is missing proper indentation.
|
|
||
| public IReadOnlyList<FanCurvePoint> Points | ||
| [JsonPropertyName("p")] | ||
| public List<FanCurvePoint> Points |
There was a problem hiding this comment.
The Points property returns a new list instance (ToList()) in its getter, but the property type is now List<FanCurvePoint>. This is misleading because callers might expect that modifying the returned list (e.g., via .Add()) would update the FanCurve state, which it won't due to the cloning logic. Since you've added a setter for JSON support, consider changing the public type back to IReadOnlyList<FanCurvePoint> to correctly convey that the getter provides a snapshot.
public IReadOnlyList<FanCurvePoint> Points| catch | ||
| { | ||
| return new FanCurve(); | ||
| } |
| using System.Linq; | ||
| using System.Text.Json.Serialization; | ||
| using System.Text.Json; | ||
|
|
| public class FanCurve | ||
| { | ||
| private readonly object _lock = new object(); | ||
| private List<FanCurvePoint> _points = new List<FanCurvePoint>(); |
There was a problem hiding this comment.
WARNING: Points property exposes List<FanCurvePoint> instead of IReadOnlyList<FanCurvePoint>. This could allow external modification without proper locking, leading to thread-safety issues. Consider using IReadOnlyList<FanCurvePoint> for the property type and keeping the setter private or protected.
| @@ -85,15 +94,20 @@ public int GetTargetSpeed(int currentTemp) | |||
| return sortedPoints[count - 1].Speed; | |||
| } | |||
|
|
|||
There was a problem hiding this comment.
WARNING: FromJson returns a new empty FanCurve on any deserialization error, which can hide data corruption. Consider returning null or throwing an exception, and let the caller handle the error appropriately.
| catch | ||
| { | ||
| return new FanCurve(); | ||
| } |
There was a problem hiding this comment.
SUGGESTION: The FromString method is now just a wrapper for FromJson. Consider removing it entirely or keeping it for backward compatibility with a migration path (e.g., try parsing old format first).
| @@ -60,12 +60,15 @@ public void LoadProfiles(string serialized) | |||
| _activeProfileName = null; | |||
There was a problem hiding this comment.
WARNING: LoadProfiles only catches JsonException. If other exceptions occur (e.g., ArgumentNullException), they will slip through. Consider catching Exception or at least documenting why only JsonException is caught.
| @@ -20,26 +27,19 @@ public FanProfile(string name, FanCurve curve, IEnumerable<string> triggerProces | |||
| TriggerProcesses = triggerProcesses.ToList(); | |||
| } | |||
|
|
|||
There was a problem hiding this comment.
SUGGESTION: FanProfile.FromJson returns null on error, which is better than returning an empty object. However, consider logging the error or including exception details for debugging.
Code Review SummaryStatus: 5 Issues Found | Recommendation: Address before merge Overview
Issue Details (click to expand)WARNING
SUGGESTION
Other Observations (not in diff)This refactoring completely replaces the old string serialization format with JSON, which is a breaking change. Existing user profiles and settings saved in the old format will no longer work. Consider adding a migration path (e.g., try parsing old format first) or maintaining backward compatibility. Additionally, in
Files Reviewed (5 files)
Reviewed by trinity-large-thinking · 393,636 tokens |
Summary