Skip to content

fix: preserve float literal type in JSON config values#572

Open
18202781743 wants to merge 1 commit into
linuxdeepin:masterfrom
18202781743:master
Open

fix: preserve float literal type in JSON config values#572
18202781743 wants to merge 1 commit into
linuxdeepin:masterfrom
18202781743:master

Conversation

@18202781743

Copy link
Copy Markdown
Contributor
  1. QJsonValue::toVariant() converts all JSON numbers to double
    internally, but when converting to QVariant, it degrades floating-point
    numbers without fractional parts (e.g., 1.0) to integers (qlonglong),
    losing the original float intent
  2. Added JsonParseResult struct to keep raw JSON bytes alongside parsed
    document for float-literal detection
  3. Added isOriginalValueFloat() helper that checks raw JSON text for '.'
    or 'e'/'E' characters in number literals
  4. Added jsonValueToVariant() helper that converts QJsonValue to
    QVariant with forced double type when the raw literal was a float
  5. Updated loadJsonFile() to return JsonParseResult with raw bytes
  6. Applied float type preservation in both meta file loading and
    override loading paths
  7. Updated unit test to verify that default values with float literals
    (like "1.0") remain as QMetaType::Double, and integer literals (like
    "1") remain as QMetaType::LongLong

Log: Fixed issue where JSON config values with float literals (e.g.,
1.0) were incorrectly degraded to integer types

Influence:

  1. Test config values with float literals (1.0, 2.5, 1e10) to ensure
    they remain double type
  2. Test config values with integer literals (1, 100) to ensure they
    remain integer type
  3. Verify override files with float values are correctly applied
  4. Test backward compatibility with existing config files
  5. Verify dde-dconfig set command accepts float values correctly
  6. Test edge cases: negative floats, scientific notation, values without
    decimal part

fix: 修复 JSON 配置值中浮点数字面量的类型保留问题

  1. QJsonValue::toVariant() 将所有 JSON 数字转换为 double 类型,但
    在转换为 QVariant 时,会将没有小数部分的浮点数(如 1.0)退化为整数
    (qlonglong),丢失原始的浮点意图
  2. 添加了 JsonParseResult 结构体,用于在解析文档时保留原始 JSON 字节数
    据,以便进行浮点数字面量检测
  3. 添加了 isOriginalValueFloat() 辅助函数,通过检查原始 JSON 文本中数字
    字面量是否包含 '.' 或 'e'/'E' 字符来判断是否为浮点数
  4. 添加了 jsonValueToVariant() 辅助函数,在将 QJsonValue 转换为 QVariant
    时,如果原始字面量为浮点数则强制保留 double 类型
  5. 更新了 loadJsonFile() 函数使其返回 JsonParseResult(包含原始字节
    数据)
  6. 在元文件加载和覆盖文件加载路径中都应用了浮点类型保留逻辑
  7. 更新了单元测试,验证包含浮点字面量的默认值(如 "1.0")保持为
    QMetaType::Double,整数字面量(如 "1")保持为 QMetaType::LongLong

Log: 修复了 JSON 配置值中浮点数字面量(如 1.0)被错误退化为整数类型的
问题

Influence:

  1. 测试包含浮点字面量的配置值(1.0, 2.5, 1e10)确保保持 double 类型
  2. 测试包含整数字面量的配置值(1, 100)确保保持整数类型
  3. 验证包含浮点值的覆盖文件是否正确应用
  4. 测试与现有配置文件的向后兼容性
  5. 验证 dde-dconfig set 命令是否正确接受浮点值
  6. 测试边界情况:负浮点数、科学计数法、不包含小数部分的值

1. QJsonValue::toVariant() converts all JSON numbers to double
internally, but when converting to QVariant, it degrades floating-point
numbers without fractional parts (e.g., 1.0) to integers (qlonglong),
losing the original float intent
2. Added JsonParseResult struct to keep raw JSON bytes alongside parsed
document for float-literal detection
3. Added isOriginalValueFloat() helper that checks raw JSON text for '.'
or 'e'/'E' characters in number literals
4. Added jsonValueToVariant() helper that converts QJsonValue to
QVariant with forced double type when the raw literal was a float
5. Updated loadJsonFile() to return JsonParseResult with raw bytes
6. Applied float type preservation in both meta file loading and
override loading paths
7. Updated unit test to verify that default values with float literals
(like "1.0") remain as QMetaType::Double, and integer literals (like
"1") remain as QMetaType::LongLong

Log: Fixed issue where JSON config values with float literals (e.g.,
1.0) were incorrectly degraded to integer types

Influence:
1. Test config values with float literals (1.0, 2.5, 1e10) to ensure
they remain double type
2. Test config values with integer literals (1, 100) to ensure they
remain integer type
3. Verify override files with float values are correctly applied
4. Test backward compatibility with existing config files
5. Verify dde-dconfig set command accepts float values correctly
6. Test edge cases: negative floats, scientific notation, values without
decimal part

fix: 修复 JSON 配置值中浮点数字面量的类型保留问题

1. QJsonValue::toVariant() 将所有 JSON 数字转换为 double 类型,但
在转换为 QVariant 时,会将没有小数部分的浮点数(如 1.0)退化为整数
(qlonglong),丢失原始的浮点意图
2. 添加了 JsonParseResult 结构体,用于在解析文档时保留原始 JSON 字节数
据,以便进行浮点数字面量检测
3. 添加了 isOriginalValueFloat() 辅助函数,通过检查原始 JSON 文本中数字
字面量是否包含 '.' 或 'e'/'E' 字符来判断是否为浮点数
4. 添加了 jsonValueToVariant() 辅助函数,在将 QJsonValue 转换为 QVariant
时,如果原始字面量为浮点数则强制保留 double 类型
5. 更新了 loadJsonFile() 函数使其返回 JsonParseResult(包含原始字节
数据)
6. 在元文件加载和覆盖文件加载路径中都应用了浮点类型保留逻辑
7. 更新了单元测试,验证包含浮点字面量的默认值(如 "1.0")保持为
QMetaType::Double,整数字面量(如 "1")保持为 QMetaType::LongLong

Log: 修复了 JSON 配置值中浮点数字面量(如 1.0)被错误退化为整数类型的
问题

Influence:
1. 测试包含浮点字面量的配置值(1.0, 2.5, 1e10)确保保持 double 类型
2. 测试包含整数字面量的配置值(1, 100)确保保持整数类型
3. 验证包含浮点值的覆盖文件是否正确应用
4. 测试与现有配置文件的向后兼容性
5. 验证 dde-dconfig set 命令是否正确接受浮点值
6. 测试边界情况:负浮点数、科学计数法、不包含小数部分的值
@18202781743 18202781743 requested review from BLumia and mhduiy July 10, 2026 10:37

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @18202781743, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@deepin-ci-robot

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: 18202781743

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

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