near / devx

This is the home of NEAR collective developer experience plans and roadmap.
11 stars 0 forks source link

[Mr. Pink] Function outputs are not valid JSON #241

Closed mikedotexe closed 3 years ago

mikedotexe commented 3 years ago

Function outputs are serialized as an escaped JSON string, as a result of using serde_json::to_string to serialize function output.

Consuming the data is unnecessarily complicated. One needs to clean the data, remove extra double-quotes ", and escape characters \ to transform it to a valid JSON byte array.

Example cleanup code:

// cleanNEAROracleRequestRaw transforms NEAROracleRequest.Result to a valid JSON.
// Currently the NEARQueryResult.Result for "get_requests" fn returns an escaped JSON string
// serialized as a byte array. We are unable to unmarshal this data directly and needs to be cleaned first.
// This is the result of Rust contracts serializing fn results using serde_json::to_string instead of serde_json::to_vec.
func cleanNEAROracleRequestRaw(data []byte) []byte {
    str := string(data)
    // remove escape dashes
    strNoDash := strings.Replace(str, "\\", "", -1)
    // remove first and last doublequote
    strNoDashNoQuotes := strNoDash[1 : len(strNoDash)-1]
    return []byte(strNoDashNoQuotes)
}

Consider using serde_json::to_vec which should hopefully avoid this problem.

mikedotexe commented 3 years ago

Pull request created on their end. Here's the branch I made for this: https://github.com/nearprotocol/near-protocol-contracts/tree/hotfix/12-improve-json-for-reqs