Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Desenvolvimento de cdUpperCamelCase #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Source/GBJSON.Config.pas
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ interface
System.SysUtils;

type
TCaseDefinition = (cdNone, cdLower, cdUpper, cdLowerCamelCase);
TCaseDefinition = (cdNone, cdLower, cdUpper, cdLowerCamelCase, cdUpperCamelCase);

TGBJSONConfig = class
private
Expand Down
71 changes: 49 additions & 22 deletions Source/GBJSON.RTTI.pas
Original file line number Diff line number Diff line change
Expand Up @@ -275,34 +275,61 @@ function TGBRTTIPropertyHelper.IsVariant: Boolean;
end;

function TGBRTTIPropertyHelper.JSONName: String;
var
I: Integer;
LField: TArray<Char>;
Function prdUpperCamelCase(Value: String): String;
var
I: Integer;
liBaixo: Integer;
LField: TArray<Char>;
begin
LField := Value.ToCharArray;
I := Low(LField);
liBaixo := I;
While I <= High(LField) do
begin
if (liBaixo = I) then
Result := Result + UpperCase(LField[I])
else
Result := Result + LField[I];
Inc(I);
end;
if Result.IsEmpty then
Result := Value;
end;
Function CamelCase(Value: String): String;
var
i: Integer;
LField: TArray<Char>;
begin
// Copy From DataSet-Serialize - https://github.com/viniciussanchez/dataset-serialize
// Thanks Vinicius Sanchez
LField := Value.ToCharArray;
i := Low(LField);
While i <= High(LField) do
begin
if (LField[i] = '_') then
begin
Inc(i);
Result := Result + UpperCase(LField[i]);
end
else
Result := Result + LowerCase(LField[i]);
Inc(i);
end;
if Result.IsEmpty then
Result := Value;
end;

begin
case TGBJSONConfig.GetInstance.CaseDefinition of
cdNone : result := Self.Name;
cdLower: result := Self.Name.ToLower;
cdUpper: result := Self.Name.ToUpper;

cdLowerCamelCase: begin
// Copy From DataSet-Serialize - https://github.com/viniciussanchez/dataset-serialize
// Thanks Vinicius Sanchez
LField := Self.Name.ToCharArray;
I := Low(LField);
While i <= High(LField) do
begin
if (LField[I] = '_') then
begin
Inc(I);
Result := Result + UpperCase(LField[I]);
end
else
Result := Result + LowerCase(LField[I]);
Inc(I);
end;
if Result.IsEmpty then
Result := Self.Name;
end;
cdLowerCamelCase:
Result := CamelCase(Self.Name);

cdUpperCamelCase:
Result := prdUpperCamelCase(CamelCase(Self.Name));
end;
end;

Expand Down
35 changes: 35 additions & 0 deletions Tests/GBJSON.Test.Deserialize.Person.pas
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ type TGBJSONTestDeserializePerson = class
[Test] procedure TestObjectLowerCase;
[Test] procedure TestObjectUpperCase;

[Test] procedure TestObjectLowerCamelCase;
[Test] procedure TestObjectUpperCamelCase;

[Test] procedure TestObjectListFill;
[Test] procedure TestObjectListEmpty;
[Test] procedure TestObjectListOneElement;
Expand Down Expand Up @@ -384,4 +387,36 @@ procedure TGBJSONTestDeserializePerson.TestStringName;
Assert.AreEqual(FPerson.name, FAuxPerson.name);
end;

procedure TGBJSONTestDeserializePerson.TestObjectLowerCamelCase;
begin
FreeAndNil(FJSONObject);
TGBJSONConfig.GetInstance
.CaseDefinition(TCaseDefinition.cdLowerCamelCase);

FUpperPerson.PERSON_ID := 1;
FUpperPerson.PERSON_NAME := 'Test Person';

FJSONObject := TGBJSONDefault.Deserializer<TUpperPerson>
.ObjectToJsonObject(FUpperPerson);

Assert.IsNotNull(FJSONObject.GetValue('personId'));
Assert.IsNotNull(FJSONObject.GetValue('personName'));
end;

procedure TGBJSONTestDeserializePerson.TestObjectUpperCamelCase;
begin
FreeAndNil(FJSONObject);
TGBJSONConfig.GetInstance
.CaseDefinition(TCaseDefinition.cdUpperCamelCase);

FUpperPerson.PERSON_ID := 1;
FUpperPerson.PERSON_NAME := 'Test Person';

FJSONObject := TGBJSONDefault.Deserializer<TUpperPerson>
.ObjectToJsonObject(FUpperPerson);

Assert.IsNotNull(FJSONObject.GetValue('PersonId'));
Assert.IsNotNull(FJSONObject.GetValue('PersonName'));
end;

end.