-
-
Notifications
You must be signed in to change notification settings - Fork 677
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix leading zero handling in toJsonType method #4721
Conversation
- Added a check in the `toJsonType` method to preserve leading zeros in strings. - This ensures that strings starting with a leading zero are treated as strings rather than numbers, preventing unwanted data conversion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems you haven't yet signed a CLA. Please do so here.
Once you do that we will be able to review and accept this pull request.
Thanks!
After a brief check in the debug APK, it seems to be working so far. At least a PIN code with a leading zero works without any issues. I will conduct further tests tomorrow. I kindly ask for your additional testing as well. |
After extensive testing, I was unable to identify any issues. Everything seems to be working as expected without any problems. |
I wonder if it's worth making this more specific? For example |
Added a check to identify and parse Double values, allowing for correct handling of decimal numbers (e.g., 0.5). Introduced logic to preserve leading zeros in strings that start with "0" and are longer than one character, ensuring these values are treated as strings (e.g., 02 remains as 02). Adjusted the parsing order to first check for Double values, followed by handling of Int and Boolean, returning the string if none of these types match.
- Changed the parsing order to first check for integers and then doubles, addressing the issue where integers like "123" were incorrectly interpreted as doubles. - Updated comments to clarify the behavior of leading zeros and decimal handling. - Added validation to check if a string starting with '0' followed by a decimal point contains valid digits, returning it as a Double if valid. - Preserved leading zeros by returning the string representation for inputs like "02". - Ensured correct parsing behavior for different input formats, returning strings for invalid cases.
@jpelgrom You made a good point about handling inputs like "0.5" and "0." I’ve updated the logic to parse valid decimal numbers starting with '0' as Double, except for '0' itself (which should be an Int), while keeping strings for cases like "02." As a beginner, I encountered some difficulties, so I appreciate your understanding. I've also verified the logic in the playground: Kotlin Playground Link. Thanks for your input! |
Your own test highlights an issue: "02.5", // should return as Double
I believe you should be able to fix this by adding (It also says |
I'm actually uncertain whether As for |
IMO we should preserve and send exactly whatever a user saves. If they want to send "00000.00010" then let them. I think that is the general expectation. |
Yes, I think so too. Of course, one option could be to ensure it's treated as a string by wrapping the input in quotes. However, I don't think that would be intuitive for most users. |
Summary
Screenshots
Link to pull request in Documentation repository
Documentation: home-assistant/companion.home-assistant#
Any other notes
toJsonType
method to preserve leading zeros in strings.