mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-08-15 08:49:25 +02:00
* Stream objectlists to JSON
git-svn-id: trunk@32762 -
This commit is contained in:
parent
283fd540da
commit
b47d158920
@ -5,7 +5,7 @@ unit fpjsonrtti;
|
|||||||
interface
|
interface
|
||||||
|
|
||||||
uses
|
uses
|
||||||
Classes, SysUtils, typinfo, fpjson, rttiutils, jsonparser;
|
Classes, SysUtils, contnrs, typinfo, fpjson, rttiutils, jsonparser;
|
||||||
|
|
||||||
Type
|
Type
|
||||||
|
|
||||||
@ -62,6 +62,8 @@ Type
|
|||||||
Function ObjectToJSON(Const AObject : TObject) : TJSONObject;
|
Function ObjectToJSON(Const AObject : TObject) : TJSONObject;
|
||||||
// Stream a collection - always returns an array
|
// Stream a collection - always returns an array
|
||||||
function StreamCollection(Const ACollection: TCollection): TJSONArray;
|
function StreamCollection(Const ACollection: TCollection): TJSONArray;
|
||||||
|
// Stream an objectlist - always returns an array
|
||||||
|
function StreamObjectList(Const AnObjectList: TObjectList): TJSONArray;
|
||||||
// Stream a TStrings instance as an array
|
// Stream a TStrings instance as an array
|
||||||
function StreamTStringsArray(Const AStrings: TStrings): TJSONArray;
|
function StreamTStringsArray(Const AStrings: TStrings): TJSONArray;
|
||||||
// Stream a TStrings instance as an object
|
// Stream a TStrings instance as an object
|
||||||
@ -669,6 +671,8 @@ begin
|
|||||||
Result.Add('Strings',StreamTStrings(Tstrings(AObject)))
|
Result.Add('Strings',StreamTStrings(Tstrings(AObject)))
|
||||||
else If AObject is TCollection then
|
else If AObject is TCollection then
|
||||||
Result.Add('Items',StreamCollection(TCollection(AObject)))
|
Result.Add('Items',StreamCollection(TCollection(AObject)))
|
||||||
|
else If AObject is TObjectList then
|
||||||
|
Result.Add('Objects',StreamObjectList(TObjectList(AObject)))
|
||||||
else
|
else
|
||||||
begin
|
begin
|
||||||
PIL:=TPropInfoList.Create(AObject,tkProperties);
|
PIL:=TPropInfoList.Create(AObject,tkProperties);
|
||||||
@ -889,7 +893,24 @@ begin
|
|||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
Function TJSONStreamer.StreamClassProperty(Const AObject : TObject): TJSONData;
|
function TJSONStreamer.StreamObjectList(const AnObjectList: TObjectList): TJSONArray;
|
||||||
|
Var
|
||||||
|
I : Integer;
|
||||||
|
|
||||||
|
begin
|
||||||
|
if not Assigned(AnObjectList) then
|
||||||
|
Result:=Nil;
|
||||||
|
Result:=TJSONArray.Create;
|
||||||
|
try
|
||||||
|
For I:=0 to AnObjectList.Count-1 do
|
||||||
|
Result.Add(ObjectToJSON(AnObjectList.Items[i]));
|
||||||
|
except
|
||||||
|
FreeAndNil(Result);
|
||||||
|
Raise;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TJSONStreamer.StreamClassProperty(const AObject: TObject): TJSONData;
|
||||||
|
|
||||||
Var
|
Var
|
||||||
C : TCollection;
|
C : TCollection;
|
||||||
@ -910,6 +931,8 @@ begin
|
|||||||
Result:=StreamTStrings(TStrings(AObject))
|
Result:=StreamTStrings(TStrings(AObject))
|
||||||
else if (AObject is TCollection) then
|
else if (AObject is TCollection) then
|
||||||
Result:=StreamCollection(TCollection(Aobject))
|
Result:=StreamCollection(TCollection(Aobject))
|
||||||
|
else If AObject is TObjectList then
|
||||||
|
Result:=StreamObjectList(TObjectList(AObject))
|
||||||
else // Normally, this is only TPersistent.
|
else // Normally, this is only TPersistent.
|
||||||
Result:=ObjectToJSON(AObject);
|
Result:=ObjectToJSON(AObject);
|
||||||
end;
|
end;
|
||||||
@ -980,7 +1003,8 @@ begin
|
|||||||
Result:=TJSONInt64Number.Create(GetOrdProp(AObject,PropertyInfo));
|
Result:=TJSONInt64Number.Create(GetOrdProp(AObject,PropertyInfo));
|
||||||
tkQWord :
|
tkQWord :
|
||||||
Result:=TJSONFloatNumber.Create(GetOrdProp(AObject,PropertyInfo));
|
Result:=TJSONFloatNumber.Create(GetOrdProp(AObject,PropertyInfo));
|
||||||
tkObject,
|
tkObject :
|
||||||
|
Result:=ObjectToJSON(GetObjectProp(AObject,PropertyInfo));
|
||||||
tkArray,
|
tkArray,
|
||||||
tkRecord,
|
tkRecord,
|
||||||
tkInterface,
|
tkInterface,
|
||||||
|
Loading…
Reference in New Issue
Block a user