Skip to content

Commit

Permalink
feat: use serializeJson cheatcode (#196)
Browse files Browse the repository at this point in the history
gnkz authored Sep 12, 2023
1 parent 392d99e commit 6a90b1b
Showing 2 changed files with 7 additions and 12 deletions.
15 changes: 5 additions & 10 deletions src/_modules/Json.sol
Original file line number Diff line number Diff line change
@@ -21,10 +21,6 @@ library JsonError {
return Invalid.encodeError("Invalid json");
}

function Immutable() internal pure returns (Error) {
return Immutable.encodeError("Json object is immutable");
}

function toJsonResult(Error self) internal pure returns (JsonResult) {
return JsonResult.wrap(Pointer.unwrap(self.toPointer()));
}
@@ -238,12 +234,15 @@ library json {

/// @dev Creates a new JsonObject struct.
/// @return The JsonObject struct.
function create(string memory obj) internal pure returns (JsonResult) {
function create(string memory obj) internal returns (JsonResult) {
if (!isValid(obj)) {
return JsonError.Invalid().toJsonResult();
}

return Ok(JsonObject({id: "", serialized: obj}));
JsonObject memory jsonObj = create();
jsonObj.serialized = vulcan.hevm.serializeJson(jsonObj.id, obj);

return Ok(jsonObj);
}

/// @dev Serializes and sets the key and value for the provided json object.
@@ -441,10 +440,6 @@ library json {
vulcan.hevm.writeJson(obj.serialized, path, key);
}

function isImmutable(JsonObject memory obj) internal pure returns (bool) {
return bytes(obj.id).length == 0;
}

function _incrementId() private returns (uint256 count) {
bytes32 slot = keccak256("vulcan.json.id.counter");

4 changes: 2 additions & 2 deletions src/_modules/experimental/Request.sol
Original file line number Diff line number Diff line change
@@ -280,7 +280,7 @@ library LibRequestBuilder {
return self.header("Content-Type", "application/json").body(obj.serialized);
}

function json(RequestBuilder memory self, string memory serialized) internal pure returns (RequestBuilder memory) {
function json(RequestBuilder memory self, string memory serialized) internal returns (RequestBuilder memory) {
if (self.request.isError()) {
return self;
}
@@ -336,7 +336,7 @@ library LibRequest {
}

library LibResponse {
function json(Response memory self) internal pure returns (JsonResult) {
function json(Response memory self) internal returns (JsonResult) {
// create() will validate the json
return jsonModule.create(string(self.body));
}

0 comments on commit 6a90b1b

Please sign in to comment.