| 1 |
#include "LibLsp/JsonRpc/message.h" |
| 2 |
#include <rapidjson/stringbuffer.h> |
| 3 |
#include <rapidjson/writer.h> |
| 4 |
#include "LibLsp/JsonRpc/serializer.h" |
| 5 |
#include "LibLsp/JsonRpc/lsRequestId.h" |
| 6 |
#include "LibLsp/JsonRpc/RequestInMessage.h" |
| 7 |
#include "LibLsp/JsonRpc/Condition.h" |
| 8 |
#include "LibLsp/JsonRpc/json.h" |
| 9 |
|
| 10 |
void LspMessage::Write(std::ostream& out) |
| 11 |
{ |
| 12 |
rapidjson::StringBuffer output; |
| 13 |
rapidjson::Writer<rapidjson::StringBuffer> writer(output); |
| 14 |
JsonWriter json_writer {&writer}; |
| 15 |
ReflectWriter(json_writer); |
| 16 |
|
| 17 |
auto const value = |
| 18 |
std::string("Content-Length: ") + std::to_string(output.GetSize()) + "\r\n\r\n" + output.GetString(); |
| 19 |
out << value; |
| 20 |
out.flush(); |
| 21 |
} |
| 22 |
|
| 23 |
std::string LspMessage::ToJson() |
| 24 |
{ |
| 25 |
rapidjson::StringBuffer output; |
| 26 |
rapidjson::Writer<rapidjson::StringBuffer> writer(output); |
| 27 |
JsonWriter json_writer {&writer}; |
| 28 |
this->ReflectWriter(json_writer); |
| 29 |
return output.GetString(); |
| 30 |
} |
| 31 |
|
| 32 |
void Reflect(Reader& visitor, lsRequestId& value) |
| 33 |
{ |
| 34 |
if (visitor.IsInt()) |
| 35 |
{ |
| 36 |
value.type = lsRequestId::kInt; |
| 37 |
value.value = visitor.GetInt(); |
| 38 |
} |
| 39 |
else if (visitor.IsInt64()) |
| 40 |
{ |
| 41 |
value.type = lsRequestId::kInt; |
| 42 |
// `lsRequestId.value` is an `int`, so we're forced to truncate. |
| 43 |
value.value = static_cast<int>(visitor.GetInt64()); |
| 44 |
} |
| 45 |
else if (visitor.IsString()) |
| 46 |
{ |
| 47 |
value.type = lsRequestId::kString; |
| 48 |
value.k_string = visitor.GetString(); |
| 49 |
value.value = atoi(value.k_string.c_str()); |
| 50 |
} |
| 51 |
else |
| 52 |
{ |
| 53 |
value.type = lsRequestId::kNone; |
| 54 |
value.value = -1; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
void Reflect(Writer& visitor, lsRequestId& value) |
| 59 |
{ |
| 60 |
switch (value.type) |
| 61 |
{ |
| 62 |
case lsRequestId::kNone: |
| 63 |
visitor.Null(); |
| 64 |
break; |
| 65 |
case lsRequestId::kInt: |
| 66 |
visitor.Int(value.value); |
| 67 |
break; |
| 68 |
case lsRequestId::kString: |
| 69 |
|
| 70 |
if (value.k_string.empty()) |
| 71 |
{ |
| 72 |
std::string str = std::to_string(value.value); |
| 73 |
visitor.String(str.c_str(), str.length()); |
| 74 |
} |
| 75 |
else |
| 76 |
{ |
| 77 |
visitor.String(value.k_string.c_str(), value.k_string.length()); |
| 78 |
} |
| 79 |
break; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
std::string ToString(lsRequestId const& id) |
| 84 |
{ |
| 85 |
if (id.type != lsRequestId::kNone) |
| 86 |
{ |
| 87 |
if (id.type == lsRequestId::kString) |
| 88 |
{ |
| 89 |
if (!id.k_string.empty()) |
| 90 |
{ |
| 91 |
return id.k_string; |
| 92 |
} |
| 93 |
} |
| 94 |
return std::to_string(id.value); |
| 95 |
} |
| 96 |
|
| 97 |
return ""; |
| 98 |
} |