Sormanne Sormanne - 1 month ago 32
JSON Question

How modify a .json file using a more simple way ( not with TJsonObject class )?

I have some code that makes changes to a

.json
file. My trouble is that I need to implement this same logic in an old project written in Delphi 7, but my code is compiling with sucess only in Delphi XE6 and higher, because the
System.Json
unit is not present in earlier versions.

So, how can I make code similar to below that compiles in Delphi 7?

uses
System.Json, ShFolder;

...

function GetSpecialFolderPath(folder : integer) : string;
const
SHGFP_TYPE_CURRENT = 0;
var
path: array [0..MAX_PATH] of char;
begin
if SUCCEEDED(SHGetFolderPath(0,folder,0,SHGFP_TYPE_CURRENT,@path[0])) then
Result := path
else
Result := '';
end;

procedure ChangeChromeSetting(const ATarget, Avalue: string);
var
specialfolder: integer;
chromePath: String;
JSONObj, ObjIpp: TJSONObject;
JSONPair: TJSONPair;
OldValue: string;
begin
specialFolder := CSIDL_LOCAL_APPDATA;
chromePath := GetSpecialFolderPath(specialFolder);
chromePath := chromePath + '\Google\Chrome\User Data\Local State';

if fileexists(chromePath) then
begin
JSONObj := TJSONObject.ParseJSONValue(TFile.ReadAllText(chromePath)) as TJSONObject;
if not Assigned(JSONObj) then Exit; {raise Exception.Create('Cannot read file: ' + chromePath);}
try
OldValue := JSONObj.GetValue<string>(ATarget);
if OldValue = '' then
begin
ObjIpp := TJSONObject.Create;
ObjIpp.AddPair('enabled', TJSONBool.Create(False));
JSONObj.AddPair('hardware_acceleration_mode', ObjIpp);

TFile.WriteAllText(chromePath, JSONObj.ToJSON);
end
else
if not SameText(OldValue, Avalue) then
begin
JSONPair := JSONObj.Get(ATarget);
JSONPair.JsonValue.Free;
JSONPair.JsonValue := TJSONString.Create(Avalue);

ObjIpp := TJSONObject.Create;
ObjIpp.AddPair('enabled', TJSONBool.Create(False));
JSONObj.AddPair('hardware_acceleration_mode', ObjIpp);

TFile.WriteAllText(chromePath, JSONObj.ToJSON);
end;
finally
JSONObj.Free;
end;
end;
end;


Usage:

ChangeChromeSetting('hardware_acceleration_mode_previous', 'false');

Answer

I have used the lkJSON library in old versions of Delphi (like Delphi 6). It's free. The code that you might write with it is similar to code for the JSON library that is included with later versions of Delphi.

The code equivalent would look like this:

procedure ChangeChromeSetting(const ATarget, Avalue: string);
var
  specialfolder: integer;
  chromePath: String;
  JSONObj, ObjIpp: TlkJSONobject;
  JSONPair: TJSONPair;
  OldValue: string;
  num:integer;
begin
  specialFolder := CSIDL_LOCAL_APPDATA;
  chromePath := GetSpecialFolderPath(specialFolder);
  chromePath := chromePath + '\Google\Chrome\User Data\Local State';

 if fileexists(chromePath) then
  begin
    TlkJSONbase(JSONObj) := TlkJSON.ParseText(TFile.ReadAllText(chromePath));    // convert text to JSONObject

    if not Assigned(JSONObj) then Exit; {raise Exception.Create('Cannot read file: ' + chromePath);}
    try
      // Access target
      num := JSONObj.IndexOfName(ATarget);   // find the element
      if (num <> -1) then begin
        OldValue := JSONObj.Field[ATarget].Value;   // get existing value
      end;

      // not finded?
      if OldValue = '' then begin
        ObjIpp := TlkJSONobject.Create;
        ObjIpp.Add('enabled', BoolToStr(False, True));
        JSONObj.Add('hardware_acceleration_mode', ObjIpp);
        TFile.WriteAllText(chromePath, JSONObj.ToString);
      end
      else
      // Finded with different value
      if not SameText(OldValue, Avalue) then
      begin
        TlkJSONBase(ObjIpp) := JSONObj.Field[ATarget];
        ObjIpp.Value := BoolToStr(False, True);
        TFile.WriteAllText(chromePath, JSONObj.ToJSON);
      end;
    finally
      JSONObj.Free;
    end;
  end;
Comments