| 107 | // HTTP content type. |
| 108 | template <typename Message> |
| 109 | Try<Message> deserialize( |
| 110 | ContentType contentType, |
| 111 | const std::string& body) |
| 112 | { |
| 113 | switch (contentType) { |
| 114 | case ContentType::PROTOBUF: { |
| 115 | Message message; |
| 116 | if (!message.ParseFromString(body)) { |
| 117 | return Error("Failed to parse body into a protobuf object"); |
| 118 | } |
| 119 | return message; |
| 120 | } |
| 121 | case ContentType::JSON: { |
| 122 | Try<JSON::Value> value = JSON::parse(body); |
| 123 | if (value.isError()) { |
| 124 | return Error("Failed to parse body into JSON: " + value.error()); |
| 125 | } |
| 126 | |
| 127 | return ::protobuf::parse<Message>(value.get()); |
| 128 | } |
| 129 | case ContentType::RECORDIO: { |
| 130 | return Error("Deserializing a RecordIO stream is not supported"); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | UNREACHABLE(); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | // Returns true if the media type can be used for |