Skip to content

Commit

Permalink
2.1.3 (lithium)
Browse files Browse the repository at this point in the history
FIX #64
ADDED Unit Tests for avoid regressions
  • Loading branch information
danieleteti committed Jan 29, 2017
1 parent 9a3ab1f commit c807464
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 12 deletions.
Binary file added docs/periodic_table.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 24 additions & 10 deletions sources/ObjectsMappers.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1949,6 +1949,8 @@ class procedure Mapper.InternalJSONObjectFieldsToObject(ctx: TRTTIContext;
LClassName: string;
LJSONKeyIsNotPresent: boolean;
begin
if not Assigned(AJSONObject) then
raise EMapperException.Create('JSON Object cannot be nil');
jvalue := nil;
_type := ctx.GetType(AObject.ClassInfo);
_fields := _type.GetFields;
Expand Down Expand Up @@ -2216,6 +2218,8 @@ class procedure Mapper.InternalJSONObjectToObject(ctx: TRTTIContext;
ListItem: TValue;
ListParam: TRttiParameter;
begin
if not Assigned(AJSONObject) then
raise EMapperException.Create('JSON Object cannot be nil');
_type := ctx.GetType(AObject.ClassInfo);
_fields := _type.GetProperties;
for _field in _fields do
Expand Down Expand Up @@ -2437,11 +2441,16 @@ class procedure Mapper.LoadJSONObjectFieldsStringToObject(AJSONObjectString: str
lJSON: TJSONObject;
begin
lJSON := TJSONObject.ParseJSONValue(AJSONObjectString) as TJSONObject;
try
InternalJSONObjectFieldsToObject(ctx, lJSON, AObject);
finally
lJSON.Free;
end;
if Assigned(lJSON) then
begin
try
InternalJSONObjectFieldsToObject(ctx, lJSON, AObject);
finally
lJSON.Free;
end;
end
else
EMapperException.Create('Invalid JSON');
end;

class function Mapper.JSONObjectFieldsToObject(AJSONObject
Expand Down Expand Up @@ -2477,11 +2486,16 @@ class function Mapper.JSONObjectStringToObject<T>(const AJSONObjectString
JObj: TJSONObject;
begin
JObj := TJSONObject.ParseJSONValue(AJSONObjectString) as TJSONObject;
try
Result := JSONObjectToObject<T>(JObj);
finally
JObj.Free;
end;
if Assigned(JObj) then
begin
try
Result := JSONObjectToObject<T>(JObj);
finally
JObj.Free;
end;
end
else
raise EMapperException.Create('Invalid JSON');
end;

class procedure Mapper.JSONObjectToDataSet(AJSONObject: TJSONObject;
Expand Down
2 changes: 1 addition & 1 deletion sources/dmvcframeworkbuildconsts.inc
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
const
DMVCFRAMEWORK_VERSION = '2.1.2 (helium)';
DMVCFRAMEWORK_VERSION = '2.1.3 (lithium)';
36 changes: 35 additions & 1 deletion unittests/Several/FrameworkTestsU.pas
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ TTestMappers = class(TTestCase)
procedure TestJSONArrayToObjectListNoGenericsWrappedList;
procedure TestCheckMapperSerializeAsStringIsEmptyStrIfObjIsNil;
procedure TestJSONObjectToObjectWithNullInJSONString;

procedure TestJSONObjectStringToObject;
procedure TestJSONObjectStringToObjectWithWrongJSON;
end;

TTestRouting = class(TTestCase)
Expand Down Expand Up @@ -586,6 +587,39 @@ procedure TTestMappers.TestJSONArrayToObjectListNoGenericsWrappedList;
end;
end;

procedure TTestMappers.TestJSONObjectStringToObject;
const
MYOBJECTJSON =
'{"PropString":"Some text \u00E0\u00E8\u00E9\u00EC\u00F2\u00F9",' +
'"PropAnsiString":"This is an ANSI text","PropInteger":-1234,' +
'"PropUInt32":1234,"PropInt64":-1234567890,"PropUInt64":1234567890,' +
'"PropUInt16":12345,"PropInt16":-12345,"PropBoolean":true,' +
'"PropDate":"2010-10-20","PropTime":"10:20:30",' +
'"PropDateTime":"2010-10-20 10:20:30",' +
'"PropTimeStamp":63423339630040,"PropCurrency":1234.5678}';
var
lMyObject: TMyObject;
lMyObject2: TMyObject;
begin
lMyObject := Mapper.JSONObjectStringToObject<TMyObject>(MYOBJECTJSON);
try
lMyObject2 := GetMyObject;
try
CheckTrue(lMyObject.Equals(lMyObject2));
finally
lMyObject2.Free;
end;
finally
lMyObject.Free;
end;
end;

procedure TTestMappers.TestJSONObjectStringToObjectWithWrongJSON;
begin
ExpectedException := EMapperException;
Mapper.JSONObjectStringToObject<TObject>('{wrongjson}');
end;

procedure TTestMappers.TestJSONObjectToObjectAndBack;
var
Obj: TMyObject;
Expand Down

0 comments on commit c807464

Please sign in to comment.