Decodes the JSON envelope returned by the Java PlatformFunction.callSync. Schema (shared with iOS / Harmony): {"status":"Success","data": } {"status":"Error","error":"..."} {"status":"Pending","requestId":"..."} `data` may be a string or a JSON value; in the latter case it is re-serialized.
| 18 | // {"status":"Pending","requestId":"..."} |
| 19 | // `data` may be a string or a JSON value; in the latter case it is re-serialized. |
| 20 | static FunctionCallResult parseFunctionCallEnvelope(const std::string& json) { |
| 21 | FunctionCallResult r; |
| 22 | r.status = FunctionCallStatus::Error; |
| 23 | |
| 24 | nlohmann::json envelope = nlohmann::json::parse(json, nullptr, false); |
| 25 | if (envelope.is_discarded() || !envelope.is_object()) { |
| 26 | r.error = "Invalid envelope JSON: " + json; |
| 27 | return r; |
| 28 | } |
| 29 | |
| 30 | auto statusIt = envelope.find("status"); |
| 31 | if (statusIt == envelope.end() || !statusIt->is_string()) { |
| 32 | r.error = "Missing 'status' field in envelope: " + json; |
| 33 | return r; |
| 34 | } |
| 35 | |
| 36 | const std::string status = statusIt->get<std::string>(); |
| 37 | if (status == "Success") { |
| 38 | r.status = FunctionCallStatus::Success; |
| 39 | auto it = envelope.find("data"); |
| 40 | if (it != envelope.end() && !it->is_null()) { |
| 41 | // Always dump as JSON so downstream `nlohmann::json::parse` can re-parse it. |
| 42 | // For a string value this produces "\"...\"" (the JSON string literal), |
| 43 | // not the raw text — required because fromPlatformResult parses data as JSON. |
| 44 | r.data = it->dump(); |
| 45 | } |
| 46 | } else if (status == "Error") { |
| 47 | r.status = FunctionCallStatus::Error; |
| 48 | auto it = envelope.find("error"); |
| 49 | r.error = (it != envelope.end() && it->is_string()) |
| 50 | ? it->get<std::string>() |
| 51 | : "Unknown error"; |
| 52 | } else if (status == "Pending") { |
| 53 | r.status = FunctionCallStatus::Pending; |
| 54 | } else { |
| 55 | r.error = "Unknown status: " + status; |
| 56 | } |
| 57 | return r; |
| 58 | } |
| 59 | |
| 60 | } // namespace |
| 61 |