Sample SMA strategy
This commit is contained in:
@@ -32,7 +32,6 @@ uses
|
||||
class function TRttiAstHelper.JsonToRecordDefinition(const AJson: string): TScalarRecordDefinition;
|
||||
var
|
||||
jsonValue: TJSONValue;
|
||||
rootObj: TJSONObject;
|
||||
fieldsArray: TJSONArray;
|
||||
i: Integer;
|
||||
fieldObj: TJSONObject;
|
||||
@@ -44,12 +43,11 @@ begin
|
||||
exit;
|
||||
|
||||
try
|
||||
if not (jsonValue is TJSONObject) then
|
||||
// The root element is expected to be a JSON array directly.
|
||||
if not (jsonValue is TJSONArray) then
|
||||
exit;
|
||||
|
||||
rootObj := jsonValue as TJSONObject;
|
||||
if not rootObj.TryGetValue<TJSONArray>('fields', fieldsArray) then
|
||||
exit;
|
||||
fieldsArray := jsonValue as TJSONArray;
|
||||
|
||||
SetLength(fields, fieldsArray.Count);
|
||||
for i := 0 to fieldsArray.Count - 1 do
|
||||
@@ -75,11 +73,9 @@ var
|
||||
rttiType: TRttiType;
|
||||
rttiRecordType: TRttiRecordType;
|
||||
field: TRttiField;
|
||||
rootObj: TJSONObject;
|
||||
fieldsArray: TJSONArray;
|
||||
fieldObj: TJSONObject;
|
||||
begin
|
||||
// 1. Create TRttiContext and get TRttiRecordType.
|
||||
ctx := TRttiContext.Create;
|
||||
rttiType := ctx.GetType(ATypeInfo);
|
||||
|
||||
@@ -88,32 +84,21 @@ begin
|
||||
|
||||
rttiRecordType := rttiType as TRttiRecordType;
|
||||
|
||||
// 2. Create root TJSONObject and a TJSONArray for 'fields'.
|
||||
rootObj := TJSONObject.Create;
|
||||
// The root element is now the array itself.
|
||||
fieldsArray := TJSONArray.Create;
|
||||
try
|
||||
fieldsArray := TJSONArray.Create;
|
||||
rootObj.AddPair('fields', fieldsArray);
|
||||
|
||||
// 3. Loop through all fields of the record type.
|
||||
for field in rttiRecordType.GetFields do
|
||||
begin
|
||||
// 4a. Create a TJSONObject for the field definition.
|
||||
fieldObj := TJSONObject.Create;
|
||||
|
||||
// 4b. Add field name.
|
||||
fieldObj.AddPair('name', TJSONString.Create(field.Name));
|
||||
|
||||
// 4c. Call TypeToScalarKind to get the type and add it.
|
||||
fieldObj.AddPair('kind', TJSONString.Create(GetEnumName(TypeInfo(TScalarKind), Ord(TypeToScalarKind(field.FieldType)))));
|
||||
|
||||
// 4d. Add the field object to the 'fields' array.
|
||||
fieldsArray.Add(fieldObj);
|
||||
end;
|
||||
|
||||
// 5. Convert the root JSON object to a string and set it as Result.
|
||||
Result := rootObj.ToJSON;
|
||||
// Convert the array directly to a JSON string.
|
||||
Result := fieldsArray.ToJSON;
|
||||
finally
|
||||
rootObj.Free;
|
||||
fieldsArray.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user