jSon_encode like function for Delphi which accepts TDataSet
delphi
Solution
In a TDataset, you can loop through the Fields collection and construct the json output and then in the loop, check the fieldtype and encode the value accordingly. Something like:
uses db;
function DSToJSON(aDataset:TDataset):string;
function fieldToJSON(thisField:TField):string;
begin
result := '"'+thisField.fieldName+'":';
case thisField.DataType of
ftInteger,
ftSmallint,
ftCurrency,
ftFloat,
ftLargeInt:
result := result+thisField.value+^n^j;
ftString :
result := noSingleQuotes(thisField.value)+^n^j;
else
end; // case
end; // of fieldToJSON
function rowToJSON(ds:TDataset):string;
var
fieldIx : integer;
begin
for fieldIx := 0 to ds.fieldcount-1 do
result := result + fieldToJSON(ds.Fields[fieldIx]);
// trim comma after last col
result := '{'+copy(result,1,length(result)-1)+'},';
end; // of rowToJSON
begin
result := '';
with aDataset do
begin
if not bof then first;
while not eof do
begin
result := result + rowToJSON(aDataset);
next;
end;
end;
//strip last comma and add
if length(result)>0 then
result := copy(result,1,length(result)-1);
result := '['+result+']';
end; // of DSToJSON
Problem
I have been tasked with creating a Indy server in Delphi 2007 which communicates with clients and returns json formatted data from Sql based databases. Someone from our office created a prototype using php. And in the prototype they use the jSon_encode function extensively to return the data from tables. I was wondering if there was a similar Delphi function which could accept a TDataSet parameter and return properly formatted json data. Anyone know of such function? Update 12/10/2013 - my modification to @user2748835 answer: ``` function jsonencode(mString: String): String; begin result := StringReplace(mString,'''','\''',[rfReplaceAll,rfIgnoreCase]); result := StringReplace(mString,'\','\\',[rfReplaceAll,rfIgnoreCase]); result := StringReplace(result,crlf,'\n',[rfReplaceAll,rfIgnoreCase]); result := StringReplace(result,'"','\"',[rfReplaceAll,rfIgnoreCase]); result := StringReplace(result,'/','\/',[rfReplaceAll,rfIgnoreCase]); result := StringReplace(result,'#9','\t',[rfReplaceAll,rfIgnoreCase]); end; function jSon_encode(aDataset:TDataset):string; function fieldToJSON(thisField:TField):string; begin try result := '"'+thisField.fieldName+'":'; case thisField.DataType of ftInteger,ftSmallint,ftLargeint: result := result+inttostr(thisField.AsInteger); ftDateTime: result := result+'"'+formatdatetime('YYYY-MM-DD HH:NN:SS',thisField.AsDateTime)+'"'; ftCurrency, ftFloat: result := result + floattostr(thisField.AsFloat); ftString : result := result + '"'+jsonencode(thisField.AsString)+'"'; else end; // case result := result + ','; except on e: Exception do begin appendtolog('problem escaping field '+thisfield.fieldname); end; end; end; // of fieldToJSON function rowToJSON(ds:TDataset):string; var fieldIx : integer; begin result := ''; for fieldIx := 0 to ds.fieldcount-1 do result := result + fieldToJSON(ds.Fields[fieldIx]); // trim comma after last col result := '{'+copy(result,1,length(result)-1)+'},'; end; // of rowToJSON begin result := ''; with aDataset do begin if not bof then first; while not eof do begin result := result + rowToJSON(aDataset); next; end; end; //strip last comma and add if length(result)>0 then result := copy(result,1,length(result)-1); result := '['+result+']'; end; // of DSToJSON ```