@@ -11,7 +11,6 @@ TJsonDTO = class
1111 FOptions: TJsonOptions;
1212 function GetAsJson : string;
1313 procedure SetAsJson (aValue: string);
14- class function GetSubTypeItemClassFromList (ObjectList: TObject): TClass;
1514 class procedure PrettyPrintPair (aJSONValue: TJSONPair; aOutputStrings: TStrings; Last: Boolean; Indent: Integer);
1615 class procedure PrettyPrintJSON (aJSONValue: TJsonValue; aOutputStrings: TStrings; Indent: Integer = 0 ); overload;
1716 class procedure PrettyPrintArray (aJSONValue: TJSONArray; aOutputStrings: TStrings; Last: Boolean; Indent: Integer);
@@ -41,7 +40,6 @@ function TJsonDTO.GetAsJson: string;
4140
4241const
4342 INDENT_SIZE = 2 ;
44-
4543class procedure TJsonDTO.PrettyPrintJSON (aJSONValue: TJsonValue; aOutputStrings: TStrings; Indent: Integer);
4644var
4745 i: Integer;
@@ -122,61 +120,24 @@ class procedure TJsonDTO.PrettyPrintPair(aJSONValue: TJSONPair; aOutputStrings:
122120 aOutputStrings.Add(Line);
123121end ;
124122
125- class function TJsonDTO.GetSubTypeItemClassFromList (ObjectList: TObject): TClass;
126- var
127- CtxRtti: TRttiContext;
128- TypeRtti: TRttiType;
129- MethodRtti: TRttiMethod;
130- ParameterRtti: TRttiParameter;
131- begin
132- Result := nil ;
133- CtxRtti := TRttiContext.Create;
134- TypeRtti := CtxRtti.GetType(ObjectList.ClassType);
135- MethodRtti := TypeRtti.GetMethod(' Add' );
136-
137- for ParameterRtti in MethodRtti.GetParameters do
138- if SameText(ParameterRtti.Name , ' Value' ) then
139- if ParameterRtti.ParamType.IsInstance then
140- Exit(ParameterRtti.ParamType.AsInstance.MetaclassType);
141- end ;
142-
143123procedure TJsonDTO.SetAsJson (aValue: string);
144124var
145125 JSONValue: TJsonValue;
146126 JSONObject: TJSONObject;
147- var
148- RttiField: TRttiField;
149- RttiContext: TRttiContext;
150-
151- Value : TValue;
152- List: TObjectList<TObject>;
153- ElementType: TClass;
154- Element: TObject;
155127begin
156- RttiContext := TRttiContext.Create;
157128 JSONValue := TJSONObject.ParseJSONValue(aValue);
158129 try
159130 if not Assigned(JSONValue) then
160131 Exit;
161132
162133 if (JSONValue is TJSONArray) then
163134 begin
164- RttiField := TRttiContext.Create.GetType(ClassInfo).GetField(' FItems' );
165- Value := RttiField.GetValue(Self);
166- List := TObjectList<TObject>(Value .AsObject);
167- ElementType := GetSubTypeItemClassFromList(Value .AsObject);
168-
169- for JSONValue in (JSONValue as TJSONArray) do
170- begin
171- Element := ElementType.Create;
172- if (JSONValue is TJSONObject) then
173- begin
174- TJson.JsonToObject(Element, TJSONObject(JSONValue), FOptions);
175- List.Add(Element);
176- end
177- else
178- Element.Free;
179- end ;
135+ with TJSONUnMarshal.Create do
136+ try
137+ SetFieldArray(Self, ' Items' , (JSONValue as TJSONArray));
138+ finally
139+ Free;
140+ end ;
180141
181142 Exit;
182143 end ;
@@ -199,3 +160,4 @@ procedure TJsonDTO.SetAsJson(aValue: string);
199160end ;
200161
201162end .
163+
0 commit comments