Added serialization support with snake_case case

This commit is contained in:
João Antônio Duarte 2020-10-15 21:37:55 -03:00
parent 40417c81a5
commit 6b2e1c1a1b
3 changed files with 65 additions and 38 deletions

View File

@ -663,7 +663,8 @@ uses
IdCoder3to4,
System.NetEncoding,
System.Character,
MVCFramework.Serializer.JsonDataObjects, MVCFramework.Serializer.Commons;
MVCFramework.Serializer.JsonDataObjects, MVCFramework.Serializer.Commons,
System.RegularExpressions;
var
GlobalAppName, GlobalAppPath, GlobalAppExe: string;
@ -1438,41 +1439,50 @@ begin
end;
function SnakeCase(const Value: string): string;
var
I: Integer;
lSB: TStringBuilder;
C: Char;
lIsUpperCase, lIsLowerCase, lLastWasLowercase: Boolean;
lCanInsertAnUnderscore: Boolean;
begin
lCanInsertAnUnderscore := False;
lLastWasLowercase := False;
lSB := TStringBuilder.Create;
try
for I := 0 to Length(Value) - 1 do
begin
C := Value.Chars[I];
lIsUpperCase := CharInSet(C, ['A' .. 'Z']);
lIsLowerCase := CharInSet(C, ['a' .. 'z']);
lCanInsertAnUnderscore := lCanInsertAnUnderscore and lLastWasLowercase;
if lIsUpperCase and (I > 0) and lCanInsertAnUnderscore then
begin
lSB.Append('_');
lCanInsertAnUnderscore := False;
end
else
begin
lCanInsertAnUnderscore := True;
end;
lSB.Append(LowerCase(C));
lLastWasLowercase := lIsLowerCase;
end;
Result := lSB.ToString;
finally
lSB.Free;
end;
// Convert multiple underlines to just one
Result := TRegex.Replace(Value, '([_][_{1}]+)', '_');
// Adds underscores between a lowercase character and an uppercase character
Result := TRegex.Replace(Result, '([a-z0-9])([A-Z])', '\1_\2');
Result := Result.ToLower;
end;
//function SnakeCase(const Value: string): string;
//var
// I: Integer;
// lSB: TStringBuilder;
// C: Char;
// lIsUpperCase, lIsLowerCase, lLastWasLowercase: Boolean;
// lCanInsertAnUnderscore: Boolean;
//begin
// lCanInsertAnUnderscore := False;
// lLastWasLowercase := False;
// lSB := TStringBuilder.Create;
// try
// for I := 0 to Length(Value) - 1 do
// begin
// C := Value.Chars[I];
// lIsUpperCase := CharInSet(C, ['A' .. 'Z']);
// lIsLowerCase := CharInSet(C, ['a' .. 'z']);
// lCanInsertAnUnderscore := lCanInsertAnUnderscore and lLastWasLowercase;
// if lIsUpperCase and (I > 0) and lCanInsertAnUnderscore then
// begin
// lSB.Append('_');
// lCanInsertAnUnderscore := False;
// end
// else
// begin
// lCanInsertAnUnderscore := True;
// end;
// lSB.Append(LowerCase(C));
// lLastWasLowercase := lIsLowerCase;
// end;
// Result := lSB.ToString;
// finally
// lSB.Free;
// end;
//end;
function StrToJSONObject(const aString: String): TJsonObject;
begin
Result := MVCFramework.Serializer.JSONDataObjects.StrToJSONObject(aString);

View File

@ -59,7 +59,7 @@ type
TMVCSerializationType = (stUnknown, stDefault, stProperties, stFields);
TMVCNameCase = (ncAsIs, ncUpperCase, ncLowerCase, ncCamelCase, ncPascalCase);
TMVCNameCase = (ncAsIs, ncUpperCase, ncLowerCase, ncCamelCase, ncPascalCase, ncSnakeCase);
TMVCDataType = (dtObject, dtArray);
@ -627,6 +627,10 @@ begin
begin
Result := CamelCase(Value, True);
end;
ncSnakeCase:
begin
Result := SnakeCase(Value);
end;
ncAsIs:
begin
Result := Value;

View File

@ -199,8 +199,8 @@ type
[TestFixture]
TTestNameCase = class(TObject)
private
fOutDATA: array [1 .. 4] of array [ncAsIs .. ncPascalCase] of string;
fOrigDATA: array [1 .. 4] of string;
fOutDATA: array [1 .. 5] of array [ncAsIs .. ncSnakeCase] of string;
fOrigDATA: array [1 .. 5] of string;
public
[SetupFixture]
procedure SetupFixture;
@ -1794,31 +1794,44 @@ begin
fOrigDATA[2] := 'ONE_TWO_THREE';
fOrigDATA[3] := 'JustOne';
fOrigDATA[4] := '_with__underscores_';
fOrigDATA[5] := 'oneTwo___three';
fOutDATA[1][ncAsIs] := fOrigDATA[1];
fOutDATA[2][ncAsIs] := fOrigDATA[2];
fOutDATA[3][ncAsIs] := fOrigDATA[3];
fOutDATA[4][ncAsIs] := fOrigDATA[4];
fOutDATA[5][ncAsIs] := fOrigDATA[5];
fOutDATA[1][ncUpperCase] := 'ONE_TWO';
fOutDATA[2][ncUpperCase] := 'ONE_TWO_THREE';
fOutDATA[3][ncUpperCase] := 'JUSTONE';
fOutDATA[4][ncUpperCase] := '_WITH__UNDERSCORES_';
fOutDATA[5][ncUpperCase] := 'ONETWO___THREE';
fOutDATA[1][ncLowerCase] := 'one_two';
fOutDATA[2][ncLowerCase] := 'one_two_three';
fOutDATA[3][ncLowerCase] := 'justone';
fOutDATA[4][ncLowerCase] := '_with__underscores_';
fOutDATA[5][ncLowerCase] := 'onetwo___three';
fOutDATA[1][ncCamelCase] := 'oneTwo';
fOutDATA[2][ncCamelCase] := 'oneTwoThree';
fOutDATA[3][ncCamelCase] := 'justOne';
fOutDATA[4][ncCamelCase] := 'WithUnderscores';
fOutDATA[5][ncCamelCase] := 'oneTwoThree';
fOutDATA[1][ncPascalCase] := 'OneTwo';
fOutDATA[2][ncPascalCase] := 'OneTwoThree';
fOutDATA[3][ncPascalCase] := 'JustOne';
fOutDATA[4][ncPascalCase] := 'WithUnderscores';
fOutDATA[5][ncPascalCase] := 'OneTwoThree';
fOutDATA[1][ncSnakeCase] := 'one_two';
fOutDATA[2][ncSnakeCase] := 'one_two_three';
fOutDATA[3][ncSnakeCase] := 'just_one';
fOutDATA[4][ncSnakeCase] := '_with_underscores_';
fOutDATA[5][ncSnakeCase] := 'one_two_three';
end;
procedure TTestNameCase.TestNameCase;
@ -1829,9 +1842,9 @@ var
lOutData: string;
lActualOutData: string;
begin
for lNameCaseIdx := ncAsIs to ncPascalCase do
for lNameCaseIdx := ncAsIs to ncSnakeCase do
begin
for I := 1 to 4 do
for I := 1 to 5 do
begin
lOrig := fOrigDATA[I];
lOutData := fOutDATA[I][lNameCaseIdx];