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
|
#include "slang-json-rpc.h"
namespace Slang {
// https://www.jsonrpc.org/specification
// m_sourceManager.initialize(nullptr, nullptr);
// m_diagnosticSink.init(&m_sourceManager, &JSONLexer::calcLexemeLocation);
/* static */SlangResult JSONRPCUtil::parseJSON(const UnownedStringSlice& slice, JSONContainer* container, DiagnosticSink* sink, JSONValue& outValue)
{
SourceManager* sourceManager = sink->getSourceManager();
// Now need to parse as JSON
String contents(slice);
SourceFile* sourceFile = sourceManager->createSourceFileWithString(PathInfo::makeUnknown(), contents);
SourceView* sourceView = sourceManager->createSourceView(sourceFile, nullptr, SourceLoc());
JSONLexer lexer;
lexer.init(sourceView, sink);
JSONBuilder builder(container);
JSONParser parser;
SLANG_RETURN_ON_FAIL(parser.parse(&lexer, sourceView, &builder, sink));
outValue = builder.getRootValue();
return SLANG_OK;
}
SlangResult JSONRPCUtil::parseJSONAndConsume(HTTPPacketConnection* connection, JSONContainer* container, DiagnosticSink* sink, JSONValue& outValue)
{
if (!connection->hasContent())
{
return SLANG_FAIL;
}
auto content = connection->getContent();
UnownedStringSlice text((const char*)content.begin(), content.getCount());
SlangResult res = parseJSON(text, container, sink, outValue);
// Consume the content
connection->consumeContent();
return res;
}
} // namespace Slang
|