Neon.Core.Serializers.DB.pas 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. {******************************************************************************}
  2. { }
  3. { Neon: Serialization Library for Delphi }
  4. { Copyright (c) 2018-2019 Paolo Rossi }
  5. { https://github.com/paolo-rossi/neon-library }
  6. { }
  7. {******************************************************************************}
  8. { }
  9. { Licensed under the Apache License, Version 2.0 (the "License"); }
  10. { you may not use this file except in compliance with the License. }
  11. { You may obtain a copy of the License at }
  12. { }
  13. { http://www.apache.org/licenses/LICENSE-2.0 }
  14. { }
  15. { Unless required by applicable law or agreed to in writing, software }
  16. { distributed under the License is distributed on an "AS IS" BASIS, }
  17. { WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. }
  18. { See the License for the specific language governing permissions and }
  19. { limitations under the License. }
  20. { }
  21. {******************************************************************************}
  22. unit Neon.Core.Serializers.DB;
  23. interface
  24. uses
  25. System.SysUtils, System.Classes, System.Rtti, System.SyncObjs, System.TypInfo,
  26. System.Generics.Collections, System.Math.Vectors, System.JSON, Data.DB,
  27. Neon.Core.Types,
  28. Neon.Core.Attributes,
  29. Neon.Core.Persistence;
  30. type
  31. TDataSetSerializer = class(TCustomSerializer)
  32. protected
  33. class function GetTargetInfo: PTypeInfo; override;
  34. class function CanHandle(AType: PTypeInfo): Boolean; override;
  35. public
  36. function Serialize(const AValue: TValue; ANeonObject: TNeonRttiObject; AContext: ISerializerContext): TJSONValue; override;
  37. function Deserialize(AValue: TJSONValue; const AData: TValue; ANeonObject: TNeonRttiObject; AContext: IDeserializerContext): TValue; override;
  38. end;
  39. procedure RegisterDefaultSerializers(ARegistry: TNeonSerializerRegistry);
  40. implementation
  41. uses
  42. Neon.Core.Utils;
  43. procedure RegisterDefaultSerializers(ARegistry: TNeonSerializerRegistry);
  44. begin
  45. ARegistry.RegisterSerializer(TDataSetSerializer);
  46. end;
  47. { TDataSetSerializer }
  48. class function TDataSetSerializer.GetTargetInfo: PTypeInfo;
  49. begin
  50. Result := TDataSet.ClassInfo;
  51. end;
  52. class function TDataSetSerializer.CanHandle(AType: PTypeInfo): Boolean;
  53. begin
  54. Result := TypeInfoIs(AType);
  55. end;
  56. function TDataSetSerializer.Deserialize(AValue: TJSONValue; const AData:
  57. TValue; ANeonObject: TNeonRttiObject; AContext: IDeserializerContext): TValue;
  58. begin
  59. Result := AData;
  60. TDataSetUtils.JSONToDataSet(AValue, AData.AsObject as TDataSet, AContext.GetConfiguration.GetUseUTCDate);
  61. end;
  62. function TDataSetSerializer.Serialize(const AValue: TValue; ANeonObject:
  63. TNeonRttiObject; AContext: ISerializerContext): TJSONValue;
  64. var
  65. LDataSet: TDataSet;
  66. begin
  67. LDataSet := AValue.AsType<TDataSet>;
  68. if ANeonObject.NeonInclude.Value = IncludeIf.NotEmpty then
  69. if LDataSet.IsEmpty then
  70. Exit(nil);
  71. Result := TDataSetUtils.DataSetToJSONArray(LDataSet, AContext.GetConfiguration.GetUseUTCDate);
  72. end;
  73. end.