#1 2021-04-23 05:54:18

CalebB
Member
Registered: 2021-04-23
Posts: 1

Difficulty using VariantLoadJSON from VariantSaveJSON string.

Hello,

I've been working on a cross platform solution sending data from a windows server to an Android phone and was using variants with VariantSaveJSON on the windows server side and repacking them with the TJSONVariantData (in SynCrossPlatformJSON) on the Android side. This has been working great and I'm now implementing the same thing, but this time on arm-linux (Latest stable Lazarus and FPC), without the cross platform libraries. VariantLoadJSON is not cooperating nicely and I'm unsure as to why, and am pretty sure this is an issue in my understanding.

Windows server side:

var
  lVariant : Variant;
begin
  lVariant := _Obj(['License','','Extensions',_Arr([]),'Time','','FrameType','','PlayTone','','LockOut','','Duress','','HoldLength','']);
  //Package up the lVariant object with data
  result := VariantSaveJSON(lVariant);
  //Send the resulting string to the client
end

Client side:

var
  lVariant : Variant;
  lVarJSON : RawUTF8;
  lLicense : RawUTF8;
begin
  lVarJSON := {"License":"LAS Herston", "Extensions":["CBN", "JBD"], "Time":"23/04/2021 04:28:24", "FrameType":0, "PlayTone":1, "LockOut":1, "Duress":0, "HoldLength":2};
  lVariant := VariantLoadJSON(lVarJSON);
  lLicense := lVariant.License;

Trying to assign lLicense provides an EVariantInvalidOpError exception with the message "Invalid variant operation". I know I can create and pack a variant using the same fields and immediately retrieve the data with that syntax no issues but trying to create the vairant from JSON is giving me a hard time.

Is there something obvious I'm missing, or am I misunderstanding how to use JSONLoadVariant?

Thanks kindly,
-Caleb

Offline

#2 2021-04-23 14:03:30

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,258
Website

Re: Difficulty using VariantLoadJSON from VariantSaveJSON string.

Here you want a TDocVariant kind of variant.

So you need either:
- to add some TryCustomVariants options, e.g. @JSON_OPTIONS[true] to VariantLoadJson()
- just use lVariant := _JsonFast(lVarJSON)

Please refer to the doc when something is not what you expect.
This is clearly documented.

Offline

Board footer

Powered by FluxBB