Jump to content
Andrzej

Best way to write Json api

Recommended Posts

I want write Json5 classes. Assume that file is already parsed and we have Json objects. Is array of 10 json objects where can be simple as integer,float,string or compound arrays and objects. How to write method giving value? Virtual getvalue or like databases, asstring,asfloat etc?

Share this post


Link to post

I think, api should be similar to IMemento xml, especially I moved from xml to json. Main reason, that xml are too big, about 7x greater than binary versions.

Share this post


Link to post

I've to move IMemento and IPersistable to JSON.
I've already made that in Python and moving to Delphi should be simple.

At moment I cannot do that, current project timings.....

cnc_memento.py

Delphi, Sydeny in my case, have a full JSON support.

Here an example of element read to compare with python way....
Delphi JSON is very close to dict in python and so to related python json:

 

function TAPITCPEngineClient.GetAxesInfo: TAPIAxesInfo;
var
  Request: string;
  Response: string;
  JSONValue: TJSONValue;
begin
  try
    if not GetActive then AbortFast;
    Result.Init;
    Request := '{"get":"axes.info"}';
    Response := SendCommand(Request);
    if Response = '' then Exit;
    JSONValue := TJSONObject.ParseJSONValue(Response);
    try
      if not (JSONValue is TJSONObject) then Exit;
      if not JSONValue.TryGetValue<TAPIAxesArray>('["res"]["joint.position"]', Result.JointPosition.V) then AbortFast;
      if not JSONValue.TryGetValue<TAPIAxesArray>('["res"]["machine.position"]', Result.MachinePosition.V) then AbortFast;
      if not JSONValue.TryGetValue<TAPIAxesArray>('["res"]["program.position"]', Result.ProgramPosition.V) then AbortFast;
      if not JSONValue.TryGetValue<TAPIAxesArray>('["res"]["machine.target.position"]', Result.MachineTargetPosition.V) then AbortFast;
      if not JSONValue.TryGetValue<TAPIAxesArray>('["res"]["program.target.position"]', Result.ProgramTargetPosition.V) then AbortFast;
      if not JSONValue.TryGetValue<TAPIAxesSpeedArray>('["res"]["actual.velocity"]', Result.ActualVelocity.V) then AbortFast;
      if not JSONValue.TryGetValue<Cardinal>('["res"]["working.wcs"]', Result.WorkingWCS) then AbortFast;
      if not JSONValue.TryGetValue<TAPIAxesArray>('["res"]["working.offset"]', Result.WorkingOffset.V) then AbortFast;
      if not JSONValue.TryGetValue<Boolean>('["res"]["homing.done"]', Result.HomingDone) then AbortFast;
      if not JSONValue.TryGetValue<Cardinal>('["res"]["homing.done.mask"]', Result.HomingDoneMask) then AbortFast;
      Result.HasData := True;
    finally
      JSONValue.Free;
    end;
  except
    Result.Init;
  end;
end;

JSon is simplest than XML but more readable.
 

Edited by shineworld

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×