Recent

Author Topic: Improved FPC JSON-RPC support  (Read 5061 times)

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2032
  • Former Delphi 1-7, 10.2 user
Improved FPC JSON-RPC support
« on: December 28, 2021, 10:04:41 pm »
From Michael Van Canneyt on the fpc-pascal mailing list (28/12/21, 4:25 pm +0100)

Hello,

Thanks to the magic of RTTI and Invoke(), creating a JSON-RPC server has
just become significantly easier !

Given an interface definition:

Code: Pascal  [Select][+][-]
  1.   IMyOtherInterface = interface ['{4D52BEE3-F709-44AC-BD31-870CBFF44632}']
  2.     Function SayHello : string;
  3.     function Echo(args : TStringArray) : String;
  4.   end;

Creating a JSON-RPC server for this is now as simple as:

Code: Pascal  [Select][+][-]
  1. // Create a class that implements the interface
  2. Type
  3.   TIntf2Impl = class(TInterfacedObject, IMyOtherInterface)
  4.   public
  5.     function Echo(args: TStringArray): String;
  6.     function SayHello: string;
  7.   end;
  8.  
  9. function TIntf2Impl.Echo(args: TStringArray): String;
  10. var
  11.   S : String;
  12. begin
  13.   Result:='';
  14.   For S in Args do
  15.     begin
  16.     if Result<>'' then
  17.       Result:=Result+' ';
  18.     Result:=Result+S;
  19.     end
  20. end;
  21.  
  22. function TIntf2Impl.SayHello: string;
  23. begin
  24.   Result:='Hello, World!';
  25. end;
  26.  
  27. // Register the class using an interface factory:
  28. Function GetMyOtherInterface(Const aName : string) : IInterface;
  29.  
  30. begin
  31.   Result:=TIntf2Impl.Create as IInterface;
  32. end;
  33.  
  34. initialization
  35.   RTTIJSONRPCRegistry.Add(TypeInfo(IMyOtherInterface),@GetMyOtherInterface,'Service2');
  36. end.

And calling it from a client program is even more simple:

Code: Pascal  [Select][+][-]
  1. var
  2.   client: IMyOtherInterface;
  3.   aRPCClient: TFPRPCClient;
  4.  
  5. begin
  6.   // Register interface with name 'Service2'
  7.   RPCServiceRegistry.Add(TypeInfo(IMyOtherInterface),'Service2');
  8.   // Create client
  9.   aRPCClient:=TFPRPCClient.Create(Nil);
  10.   aRPCClient.BaseURL:='http://localhost:8080/RPC';
  11.  
  12.   // Just typecast the client to the desired interface
  13.   Client:=aRPCClient as IMyotherInterface;
  14.   // or explitly create using the registered service name
  15.   // Client:=RPC.Specialize CreateService<IMyotherInterface>('Service2');
  16.  
  17.   Writeln('Sayhello: ',client.SayHello);
  18.   Writeln('Sayhello: ',client.Echo(['This','is','Sparta']));
  19. end.

The service can also be consumed by pas2js.

The support for various argument types is still limited, but this will improve soon.
Currently simple types and arrays of simple types are improved. Proper
support for records and other structured types needs extended RTTI...

An example server and client programs have been committed to the FPC repo.
(packages/fcl-web/examples/jsonrpc/rtti)

With many thanks to Sven Barth for putting me on the right track...

Enjoy!
Michael.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Improved FPC JSON-RPC support
« Reply #1 on: December 30, 2021, 07:27:31 am »
Invoke has been fully implemented? Great! Now I can emulate goroutines without pointer hacks.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6315
  • Compiler Developer
Re: Improved FPC JSON-RPC support
« Reply #2 on: December 30, 2021, 03:40:07 pm »
Invoke has been fully implemented? Great! Now I can emulate goroutines without pointer hacks.

Since 3.2.0 already. However it requires libffi on all platforms (this is done by adding the FFI.Manager unit in the main project) except for the register calling convention on i386 and the default calling convention on Win64. Further calling conventions (especially the other important one on x86_64: SysV) are a work-in-progress.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Improved FPC JSON-RPC support
« Reply #3 on: December 30, 2021, 08:02:57 pm »
However it requires libffi on all platforms
I would sacrifice my pure Pascal pride for this to work, let alone a single library dependency :D

vfclists

  • Hero Member
  • *****
  • Posts: 1157
    • HowTos Considered Harmful?
Re: Improved FPC JSON-RPC support
« Reply #4 on: February 04, 2026, 06:47:02 am »
From Michael Van Canneyt on the fpc-pascal mailing list (28/12/21, 4:25 pm +0100)

Hello,

Thanks to the magic of RTTI and Invoke(), creating a JSON-RPC server has
just become significantly easier !

Given an interface definition:

...

Enjoy!
Michael.

Which stable version of Lazarus/FPC is this packaged with?

I am running Lazarus 3.0 which is built with FPC 3.2.2 but this is not packaged with it?

I just checked and FPC 3.2.2 was released around August 2020 which is early than this announcement. My bad.

Which version of FPC should I compile Lazarus with to include it?

The latest Lazarus 4.4 is still being built with FPC 3.2.2
« Last Edit: February 04, 2026, 06:57:22 am by vfclists »
Lazarus 3.0/FPC 3.2.2

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: Improved FPC JSON-RPC support
« Reply #5 on: February 04, 2026, 09:29:21 am »
The latest Lazarus 4.4 is still being built with FPC 3.2.2
Of course. 3.2.2 is the current release.
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

vfclists

  • Hero Member
  • *****
  • Posts: 1157
    • HowTos Considered Harmful?
Re: Improved FPC JSON-RPC support
« Reply #6 on: February 04, 2026, 09:32:11 am »
The latest Lazarus 4.4 is still being built with FPC 3.2.2
Of course. 3.2.2 is the current release.

I'm sure a lot of the more savvy users have moved on from 3.2.2.

What would you recommend for someone who needs those features in the compiler?
Lazarus 3.0/FPC 3.2.2

PascalDragon

  • Hero Member
  • *****
  • Posts: 6315
  • Compiler Developer
Re: Improved FPC JSON-RPC support
« Reply #7 on: February 05, 2026, 09:15:07 pm »
I'm sure a lot of the more savvy users have moved on from 3.2.2.

3.2.2 is still the recommended version, because main may break at any time.

What would you recommend for someone who needs those features in the compiler?

There is no recommendation. Just test whether current main works good enough with your code, but don't be surprised if you encounter some problem down the line.

 

TinyPortal © 2005-2018