summaryrefslogtreecommitdiff
path: root/graphics/asymptote/LspCpp/src/jsonrpc/message.cpp
blob: 4a23d2016ab457dca4e1680f43fae35946c5f10a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "LibLsp/JsonRpc/message.h"
#include <rapidjson/stringbuffer.h>
#include <rapidjson/writer.h>
#include "LibLsp/JsonRpc/serializer.h"
#include "LibLsp/JsonRpc/lsRequestId.h"
#include "LibLsp/JsonRpc/RequestInMessage.h"
#include "LibLsp/JsonRpc/Condition.h"
#include "LibLsp/JsonRpc/json.h"

void LspMessage::Write(std::ostream& out) {
	rapidjson::StringBuffer output;
	rapidjson::Writer<rapidjson::StringBuffer> writer(output);
	JsonWriter json_writer{ &writer };
	ReflectWriter(json_writer);

	const auto value = std::string("Content-Length: ") + std::to_string(output.GetSize()) + "\r\n\r\n" + output.GetString();
	out << value;
	out.flush();
}

std::string LspMessage::ToJson() {
	rapidjson::StringBuffer output;
	rapidjson::Writer<rapidjson::StringBuffer> writer(output);
	JsonWriter json_writer{ &writer };
	this->ReflectWriter(json_writer);
	return  output.GetString();
}

void Reflect(Reader& visitor, lsRequestId& value) {
	if (visitor.IsInt()) {
		value.type = lsRequestId::kInt;
		value.value = visitor.GetInt();
	}
	else if (visitor.IsInt64()) {
		value.type = lsRequestId::kInt;
		// `lsRequestId.value` is an `int`, so we're forced to truncate.
		value.value = static_cast<int>(visitor.GetInt64());
	}
	else if (visitor.IsString()) {
		value.type = lsRequestId::kString;
		value.k_string = visitor.GetString();
		value.value = atoi(value.k_string.c_str());
		
	}
	else {
		value.type = lsRequestId::kNone;
		value.value = -1;
	}
}

void Reflect(Writer& visitor, lsRequestId& value) {
	switch (value.type) {
	case lsRequestId::kNone:
		visitor.Null();
		break;
	case lsRequestId::kInt:
		visitor.Int(value.value);
		break;
	case lsRequestId::kString:
		
		if(value.k_string.empty())
		{
			std::string str = std::to_string(value.value);
			visitor.String(str.c_str(), str.length());
		}
		else
		{
			visitor.String(value.k_string.c_str(), value.k_string.length());
		}
		break;
	}
}

std::string ToString(const lsRequestId& id) {
	if (id.type != lsRequestId::kNone)
	{
		if(id.type == lsRequestId::kString)
		{
			if (!id.k_string.empty())
				return id.k_string;
		}
		return std::to_string(id.value);
	}
	
	return "";
}