Skip to content
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

[CodeHealth] Simplifying json parsing - pt.4 #27884

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

cdesouza-chromium
Copy link
Collaborator

This PR simplifies JSON reading in several places by:

- Using `base::JSONReader::ReadDict`
- Using `base::JSONReader::ReadList`
- Using `base::test::ParseJson*`

This changes aim to make the checks around the parsing and its expectations simpler to read.

Resolves

Submitter Checklist:

  • I confirm that no security/privacy review is needed and no other type of reviews are needed, or that I have requested them
  • There is a ticket for my issue
  • Used Github auto-closing keywords in the PR description above
  • Wrote a good PR/commit description
  • Squashed any review feedback or "fixup" commits before merge, so that history is a record of what happened in the repo, not your PR
  • Added appropriate labels (QA/Yes or QA/No; release-notes/include or release-notes/exclude; OS/...) to the associated issue
  • Checked the PR locally:
    • npm run test -- brave_browser_tests, npm run test -- brave_unit_tests wiki
    • npm run presubmit wiki, npm run gn_check, npm run tslint
  • Ran git rebase master (if needed)

Reviewer Checklist:

  • A security review is not needed, or a link to one is included in the PR description
  • New files have MPL-2.0 license header
  • Adequate test coverage exists to prevent regressions
  • Major classes, functions and non-trivial code blocks are well-commented
  • Changes in component dependencies are properly reflected in gn
  • Code follows the style guide
  • Test plan is specified in PR before merging

After-merge Checklist:

Test Plan:

Copy link
Contributor

@iefremov iefremov left a comment

Choose a reason for hiding this comment

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

components/skus/renderer lgtm

Comment on lines 256 to 259
std::string json = R"([])";
auto result = simple_hash_client_->ParseNFTsFromSimpleHash(
base::test::ParseJsonDict(json), true, false);
ASSERT_FALSE(result);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we don't need to test non-dict inputs as it is validated by caller now

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done. Thanks.

Copy link
Collaborator

@supermassive supermassive left a comment

Choose a reason for hiding this comment

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

wallet core lgtm

This PR simplifies JSON reading in several places by:

    - Using `base::JSONReader::ReadDict`
    - Using `base::JSONReader::ReadList`
    - Using `base::test::ParseJson*`

This changes aim to make the checks around the parsing and its
expectations simpler to read.
@cdesouza-chromium cdesouza-chromium force-pushed the simplify-json-reader-use-pt5 branch from 69fda71 to 28c5b7f Compare March 3, 2025 11:55
Copy link
Contributor

github-actions bot commented Mar 3, 2025

[puLL-Merge] - brave/brave-core@27884

Description

This PR updates the codebase to use the new Value API in Chromium's base library, specifically transitioning from the older JSONReader::Read() to the newer base::test::ParseJson() and JSONReader::ReadDict() methods. The changes are focused on modernizing JSON parsing across several Brave components and improving type safety.

Changes

Changes

  1. components/brave_wallet/

    • Replaced base::JSONReader::Read with base::test::ParseJson in test files
    • Updated CompareJSON function to use newer APIs
    • Modified function signatures to accept base::Value::Dict directly
    • Updated parameter types for parsing functions to work with dictionary values directly
  2. components/de_amp/

    • Updated navigation chain handling to use List APIs directly
    • Switched to base::JSONReader::ReadDict for JSON parsing
  3. Other Components

    • Updated JSON parsing in ntp_background_images, psst, request_otr, skus and web_discovery components
    • Removed redundant dictionary type checking
    • Simplified error handling paths by using newer APIs
sequenceDiagram
    participant App
    participant JSONParser
    participant Dict
    
    App->>JSONParser: ReadDict(json_string)
    alt Invalid JSON
        JSONParser-->>App: Return nullopt
    else Valid JSON
        JSONParser->>Dict: Create Dictionary
        Dict-->>App: Return Dict
    end
    
    App->>Dict: Access Values
    Dict-->>App: Return Typed Values
Loading

Details

The key changes involve:

  1. Replacing base::JSONReader::Read() with:

    • base::test::ParseJson() for testing
    • base::JSONReader::ReadDict() for production code
    • base::JSONReader::ReadList() where lists are expected
  2. Modifying function signatures to work with base::Value::Dict directly rather than raw base::Value

  3. Removing redundant type checking since the new APIs enforce types at the API level

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

Successfully merging this pull request may close these issues.

4 participants