You've already forked lazarus-ccr
git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@305 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
@ -29,7 +29,10 @@ type
|
|||||||
FCallProcedureName : string;
|
FCallProcedureName : string;
|
||||||
FCallTarget : string;
|
FCallTarget : string;
|
||||||
FIDObject : TJSONData;
|
FIDObject : TJSONData;
|
||||||
Protected
|
FVersion : string;
|
||||||
|
FVersionEnum : TJonRPCVersion;
|
||||||
|
protected
|
||||||
|
procedure SetVersion(const AValue : string);
|
||||||
procedure BeginCallResponse(Const AProcName,ATarget:string);
|
procedure BeginCallResponse(Const AProcName,ATarget:string);
|
||||||
procedure EndCallResponse();
|
procedure EndCallResponse();
|
||||||
procedure BeginCallRead(ACallContext : ICallContext);
|
procedure BeginCallRead(ACallContext : ICallContext);
|
||||||
@ -41,13 +44,17 @@ type
|
|||||||
);
|
);
|
||||||
procedure EndExceptionList();
|
procedure EndExceptionList();
|
||||||
public
|
public
|
||||||
|
constructor Create();override;
|
||||||
destructor Destroy();override;
|
destructor Destroy();override;
|
||||||
|
property Version : string read FVersion;
|
||||||
|
property VersionEnum : TJonRPCVersion read FVersionEnum;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure Server_service_RegisterJsonFormat();
|
procedure Server_service_RegisterJsonFormat();
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
uses jsonparser;
|
uses
|
||||||
|
jsonparser, StrUtils;
|
||||||
|
|
||||||
procedure Server_service_RegisterJsonFormat();
|
procedure Server_service_RegisterJsonFormat();
|
||||||
begin
|
begin
|
||||||
@ -90,10 +97,29 @@ end;
|
|||||||
|
|
||||||
{ TJsonRpcFormatter }
|
{ TJsonRpcFormatter }
|
||||||
|
|
||||||
|
procedure TJsonRpcFormatter.SetVersion(const AValue : string);
|
||||||
|
var
|
||||||
|
i : PtrInt;
|
||||||
|
begin
|
||||||
|
if ( FVersion = AValue ) then
|
||||||
|
Exit;
|
||||||
|
i := AnsiIndexStr(AValue,[s_json_rpc_version_10,s_json_rpc_version_11]);
|
||||||
|
if ( i < 0 ) then
|
||||||
|
Error('JSON-RPC version not supported : %s',[AValue]);
|
||||||
|
FVersion := AValue;
|
||||||
|
FVersionEnum := TJonRPCVersion(i);
|
||||||
|
end;
|
||||||
|
|
||||||
procedure TJsonRpcFormatter.BeginCallResponse(const AProcName, ATarget : string);
|
procedure TJsonRpcFormatter.BeginCallResponse(const AProcName, ATarget : string);
|
||||||
|
var
|
||||||
|
locBuffer : string;
|
||||||
begin
|
begin
|
||||||
Clear();
|
Clear();
|
||||||
BeginObject('',nil);
|
BeginObject('',nil);
|
||||||
|
if ( VersionEnum = jsonRPC_11 ) then begin
|
||||||
|
locBuffer := s_json_rpc_version_11;
|
||||||
|
Put(s_json_version,TypeInfo(string),locBuffer);
|
||||||
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TJsonRpcFormatter.EndCallResponse();
|
procedure TJsonRpcFormatter.EndCallResponse();
|
||||||
@ -101,11 +127,15 @@ var
|
|||||||
locRoot : TJSONObject;
|
locRoot : TJSONObject;
|
||||||
begin
|
begin
|
||||||
locRoot := GetRootData();
|
locRoot := GetRootData();
|
||||||
|
if ( locRoot.IndexOfName(s_json_result) < 0 ) then
|
||||||
|
locRoot.Elements[s_json_result] := TJSONNull.Create();
|
||||||
|
if ( VersionEnum = jsonRPC_10 ) then
|
||||||
locRoot.Elements[s_json_error] := TJSONNull.Create();
|
locRoot.Elements[s_json_error] := TJSONNull.Create();
|
||||||
if Assigned(FIDObject) then begin
|
if Assigned(FIDObject) then begin
|
||||||
locRoot.Elements[s_json_id] := FIDObject;
|
locRoot.Elements[s_json_id] := FIDObject;
|
||||||
FIDObject := nil;
|
FIDObject := nil;
|
||||||
end else begin
|
end else begin
|
||||||
|
if ( VersionEnum = jsonRPC_10 ) then
|
||||||
locRoot.Elements[s_json_id] := TJSONNull.Create();
|
locRoot.Elements[s_json_id] := TJSONNull.Create();
|
||||||
end;
|
end;
|
||||||
EndScope();
|
EndScope();
|
||||||
@ -115,19 +145,46 @@ procedure TJsonRpcFormatter.BeginCallRead(ACallContext : ICallContext);
|
|||||||
var
|
var
|
||||||
nameBuffer, strBuffer : string;
|
nameBuffer, strBuffer : string;
|
||||||
rootObj : TJSONObject;
|
rootObj : TJSONObject;
|
||||||
|
tmpObj : TJSONData;
|
||||||
i : PtrInt;
|
i : PtrInt;
|
||||||
|
paramsAsArray : Boolean;
|
||||||
begin
|
begin
|
||||||
ClearStack();
|
ClearStack();
|
||||||
FreeAndNil(FIDObject);
|
FreeAndNil(FIDObject);
|
||||||
rootObj := GetRootData();
|
rootObj := GetRootData();
|
||||||
PushStack(rootObj,stObject);
|
PushStack(rootObj,stObject);
|
||||||
|
|
||||||
|
i := rootObj.IndexOfName(s_json_version);
|
||||||
|
strBuffer := s_json_rpc_version_10; // Assume 1.0 by default
|
||||||
|
if ( i > -1 ) then begin
|
||||||
|
tmpObj := rootObj.Items[i];
|
||||||
|
if not rootObj.Items[i].IsNull then
|
||||||
|
strBuffer := tmpObj.AsString;
|
||||||
|
end;
|
||||||
|
SetVersion(strBuffer);
|
||||||
|
|
||||||
nameBuffer := s_json_method;
|
nameBuffer := s_json_method;
|
||||||
Get(TypeInfo(string),nameBuffer,FCallProcedureName);
|
Get(TypeInfo(string),nameBuffer,FCallProcedureName);
|
||||||
i := rootObj.IndexOfName(s_json_id);
|
i := rootObj.IndexOfName(s_json_id);
|
||||||
if ( i > -1 ) then
|
if ( i > -1 ) then
|
||||||
FIDObject := Clone(rootObj);
|
FIDObject := Clone(rootObj.Items[i]);
|
||||||
|
|
||||||
|
if ( VersionEnum = jsonRPC_11 ) then begin
|
||||||
|
i := rootObj.IndexOfName(s_json_params);
|
||||||
|
if ( i > 0 ) then begin
|
||||||
|
paramsAsArray := ( rootObj.Items[i].JSONType() = jtArray );
|
||||||
|
end else begin
|
||||||
|
rootObj.Add(s_json_params,TJSONArray.Create());
|
||||||
|
paramsAsArray := True;
|
||||||
|
end;
|
||||||
|
end else begin
|
||||||
|
paramsAsArray := True;
|
||||||
|
end;
|
||||||
nameBuffer := s_json_params;
|
nameBuffer := s_json_params;
|
||||||
BeginArrayRead(nameBuffer,nil,asScoped,'');
|
if paramsAsArray then
|
||||||
|
BeginArrayRead(nameBuffer,nil,asScoped,'')
|
||||||
|
else
|
||||||
|
BeginObjectRead(nameBuffer,nil);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TJsonRpcFormatter.GetCallProcedureName() : String;
|
function TJsonRpcFormatter.GetCallProcedureName() : String;
|
||||||
@ -151,7 +208,10 @@ begin
|
|||||||
BeginObject('',nil);
|
BeginObject('',nil);
|
||||||
|
|
||||||
locRoot := GetRootData();
|
locRoot := GetRootData();
|
||||||
locRoot.Elements[s_json_result] := TJSONNull.Create();
|
case VersionEnum of
|
||||||
|
jsonRPC_10 : locRoot.Elements[s_json_result] := TJSONNull.Create();
|
||||||
|
jsonRPC_11 : locRoot.Add(s_json_version,s_json_rpc_version_11)
|
||||||
|
end;
|
||||||
locError := TJSONObject.Create();
|
locError := TJSONObject.Create();
|
||||||
locRoot.Elements[s_json_error] := locError;
|
locRoot.Elements[s_json_error] := locError;
|
||||||
locError.Add(s_json_name,'');
|
locError.Add(s_json_name,'');
|
||||||
@ -170,6 +230,12 @@ begin
|
|||||||
EndScope();
|
EndScope();
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
constructor TJsonRpcFormatter.Create();
|
||||||
|
begin
|
||||||
|
inherited Create();
|
||||||
|
SetVersion(s_json_rpc_version_10);
|
||||||
|
end;
|
||||||
|
|
||||||
destructor TJsonRpcFormatter.Destroy();
|
destructor TJsonRpcFormatter.Destroy();
|
||||||
begin
|
begin
|
||||||
FreeAndNil(FIDObject);
|
FreeAndNil(FIDObject);
|
||||||
|
Reference in New Issue
Block a user