Ciao ragazzi, non essendo molto scafato con i json mi chiedevo: secondo voi se parto da questa funzione
var
Info : TSearchRec;
Space: int64;
filename:string;
Path: string;
begin
Path:='/tmp/';
If FindFirst (Path + '*',faAnyFile and faDirectory,Info)=0 then
begin
Repeat
With Info do
begin
if ((Name='.') OR (Name='..')) then
begin
end
else
begin
If (Attr and faDirectory) = faDirectory then
begin
//is directory
end
else
begin
//this informations in json?!
Space:=size; //file dimension
filename:=Path + Name;
end;
end;
end;
Until FindNext(info)<>0;
end;
FindClose(Info);
qual'è il modo migliore e maggiormente performante per mettere il contenuto della directory dentro una stringa json e qual'è il modo più veloce di leggere questi dati dalla stringa json?
io partirei da questa funzione (che poi si può scegliere se funzione o procedura)
http://wiki.freepascal.org/findallfiles (http://wiki.freepascal.org/findallfiles)
e poi serializzi con array JSON
Edit:
esempio costruzione array estratto da https://www.freepascal.org/~michael/articles/ (https://www.freepascal.org/~michael/articles/) PDF webdata, pagina 13
Procedure TestLong;
Var
JAB,J,JA : TJSONObject;
JT : TJSONArray;
begin
JA:=TJSonObject.Create;
JA.Add(’street’,’5 Main Street’);
JA.Add(’City’,’San Diego, CA’);
JA.Add(’Zip’,91912);
JT:=TJSonArray.Create;
JT.Add(’619 332-3452’);
JT.Add(’664 223-4667’);
J:=TJSONObject.Create;
J.Add(’name’,’Mary Lebow’);
J.Add(’address’,JA);
J.Add(’phonenumbers’,JT);
JAB:=TJSONObject.Create;
JAB.Add(’addressbook’,J);
Writeln(JAB.AsJSON);
JAB.Free;
end;
Ciao caro.Domanda scema: perchè lo vuoi mettere dentro un json? Perchè è un vincolo di qualche tipo?Potresti usare una cavolo di TStringList e mettere tutto il path completo dentro, file per file.Poi se vuoi scrivere le cose in JSON quando salvi, diventa una bischerata scrivere un file di testo mettendo pezzetto per pezzetto dentro.Una cretinata del tipo:buffer.append('[');... ciclo di lettura della cartella
buffer.append(Format('"%s",[nomedelfilecompleto]));
buffer.append(']');buffer.saveToFile('seguo-la-moda-del-json-anche-se-non-devo-recuperare-byte-in-rete-per-rendere-veloce-la-trasmissione.json');
Giusto per dare l'idea :)
Altrimenti, se non ci sono vincoli, puoi usare il consolidato e vecchio TDocument di Lazarus/FPC.
:)
Risolto così
var
Info : TSearchRec;
Path: string;
O: TJSONObject;
dati: TStringList;
risultato: string;
errore: string;
item_count, i: integer;
begin
dati:=TStringList.Create;
Path:=ValidateDirectorySeparator(Self.GetValue(ARequest,'path'));
try
try
item_count:=0;
If FindFirst (ROOT_FILE + Path + '*',faAnyFile and faDirectory,Info)=0 then
begin
Repeat
With Info do
begin
if ((Name='.') OR (Name='..')) then
begin
end
else
begin
If (Attr and faDirectory) = faDirectory then
begin
//is directory
dati.Add('Type_' + IntToStr(item_count));
dati.Add('DIR');
dati.Add('Name_' + IntToStr(item_count));
dati.Add(Name);
dati.Add('Dimension_' + IntToStr(item_count));
dati.Add('0');
end
else
begin
dati.Add('Type_' + IntToStr(item_count));
dati.Add('FILE');
dati.Add('Name_' + IntToStr(item_count));
dati.Add(Name);
dati.Add('Dimension_' + IntToStr(item_count));
dati.Add(IntToStr(Size));
end;
Inc(item_count);
end;
end;
Until FindNext(info)<>0;
end;
FindClose(Info);
finally
end;
except
on E: Exception do
errore := E.Message;
end;
if trim(errore) = '' then
begin
//si tratta di una richiesta di move file
O:=TJSONObject.Create();
O.Add('Status','ok');
O.Add('Result','TRUE');
O.Add('ItemCount',IntToStr(item_count));
i := 0;
while i < dati.Count do
begin
O.Add(dati[i],dati[i+1]);
Inc(i,2);
end;
Self.SendValue(AResponse, O.AsJSON);
O.Free;
end else begin
//si tratta di una richiesta di move file
O:=TJSONObject.Create(['Status','error',
'Result','FALSE',
'MessageError',errore]);
Self.SendValue(AResponse, O.AsJSON);
O.Free;
end;
dati.Free;
Stilgar, è perchè stò scrivendo il sostituto di un server ftp ma basato su un http server scritto in lazarus.