Skip to content

Commit

Permalink
feat: string to json (#238)
Browse files Browse the repository at this point in the history
* feat: string to json

Solves #237

* docs: update strings references
  • Loading branch information
gnkz authored Nov 29, 2023
1 parent c98fbbf commit f67740f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/src/references/Strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,7 @@ Parses a bytes32 string.

Parses a boolean string.

### **parseJson(string value) → (JsonResult)**

Parses a JSON string.

9 changes: 8 additions & 1 deletion src/_internal/Strings.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.13 <0.9.0;

import "./Vulcan.sol";
import {vulcan} from "./Vulcan.sol";
import {json, JsonResult} from "./Json.sol";
import {fmt} from "./Fmt.sol";

library strings {
Expand Down Expand Up @@ -92,4 +93,10 @@ library strings {
function parseBool(string memory value) internal pure returns (bool) {
return vulcan.hevm.parseBool(value);
}

/// @dev Parses a JSON string.
/// @param value The string to parse
function parseJson(string memory value) internal returns (JsonResult) {
return json.create(value);
}
}
6 changes: 6 additions & 0 deletions test/modules/Strings.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,10 @@ contract StringsTest is Test {

expect(value.parseBool()).toBeFalse();
}

function testItCanParseJson() external {
string memory value = '{"foo": "bar"}';

expect(value.parseJson().expect("Invalid JSON").getString(".foo")).toEqual("bar");
}
}

0 comments on commit f67740f

Please sign in to comment.