-
-
Save JensMertelmeyer/db915827dd550f19e46cb3f6de09df1f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unit Unit1; | |
interface uses | |
DUnitX.TestFramework, | |
System.SysUtils, | |
Neon.Core.Utils; | |
type | |
TMyRecord = record | |
rawBytes: TBytes; | |
end; | |
[TestFixture] | |
TTestRecordWithBytes = class | |
[TestCase] | |
procedure TestNeonDefault(); | |
[TestCase] | |
procedure TestCustomBytesSerializer_Numbers(); | |
[TestCase] | |
procedure TestCustomBytesSerializer_String(); | |
[TestCase] | |
procedure TestCustomByteSerializer_EmptyArray(); | |
end; | |
implementation uses | |
System.JSON, | |
System.Rtti, | |
System.TypInfo, | |
Neon.Core.Persistence.JSON, | |
Neon.Core.Persistence, | |
Neon.Core.Types, | |
Unit2; | |
{ TTestRecord } | |
{ TestCase } | |
procedure TTestRecordWithBytes.TestCustomByteSerializer_EmptyArray(); | |
var | |
config: INeonConfiguration; | |
before, after: TMyRecord; | |
asJson: TJsonValue; | |
begin | |
config := TNeonConfiguration.Default(); | |
config.GetSerializers().RegisterSerializer(TBytesSerializer); | |
before.rawBytes := []; | |
asJson := TNeon.ValueToJSON( TValue.From(before), config ); | |
after := TNeon.JSONToValue<TMyRecord>(asJson, config); | |
Assert.AreEqual(0, Length(after.rawBytes)); | |
end; | |
procedure TTestRecordWithBytes.TestCustomBytesSerializer_Numbers(); | |
var | |
config: INeonConfiguration; | |
before, after: TMyRecord; | |
asJson: TJsonValue; | |
begin | |
config := TNeonConfiguration.Default(); | |
config.GetSerializers().RegisterSerializer(TBytesSerializer); | |
before.rawBytes := [1, 5, Byte.MaxValue]; | |
asJson := TNeon.ValueToJSON( TValue.From(before), config ); | |
after := TNeon.JSONToValue<TMyRecord>(asJson, config); | |
Assert.AreEqual(3, Length(after.rawBytes)); | |
Assert.AreEqual<Byte>(1, after.rawBytes[0]); | |
Assert.AreEqual<Byte>(5, after.rawBytes[1]); | |
Assert.AreEqual<Byte>(Byte.MaxValue, after.rawBytes[2]); | |
end; | |
procedure TTestRecordWithBytes.TestCustomBytesSerializer_String(); | |
const | |
text = 'Hello World 😎'; | |
var | |
config: INeonConfiguration; | |
before, after: TMyRecord; | |
asJson: TJsonValue; | |
begin | |
config := TNeonConfiguration.Default(); | |
config.GetSerializers().RegisterSerializer(TBytesSerializer); | |
before.rawBytes := TEncoding.Unicode.GetBytes(text); | |
asJson := TNeon.ValueToJSON( TValue.From(before), config ); | |
after := TNeon.JSONToValue<TMyRecord>(asJson, config); | |
var textBefore := TEncoding.Unicode.GetString(before.rawBytes); | |
var textAfter := TEncoding.Unicode.GetString(after.rawBytes); | |
Assert.AreEqual(textBefore, textAfter); | |
end; | |
procedure TTestRecordWithBytes.TestNeonDefault(); | |
const | |
text = 'Hello World 😎'; | |
var | |
before, after: TMyRecord; | |
asJson: TJSONValue; | |
begin | |
before.rawBytes := TEncoding.Unicode.GetBytes(text); | |
asJson := TNeon.ValueToJSON( TValue.From(before) ); | |
after := TNeon.JSONToValue<TMyRecord>(asJson); | |
var textBefore := TEncoding.Unicode.GetString(before.rawBytes); | |
var textAfter := TEncoding.Unicode.GetString(after.rawBytes); | |
Assert.AreEqual(textBefore, textAfter); | |
end; | |
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unit Unit2; | |
interface uses | |
System.TypInfo, | |
System.Rtti, | |
System.JSON, | |
Neon.Core.Persistence; | |
type | |
TBytesSerializer = class(TCustomSerializer) | |
protected | |
class function GetTargetInfo(): PTypeInfo; override; | |
class function CanHandle(AType: PTypeInfo): Boolean; override; | |
public | |
function Serialize( | |
const AValue: TValue; | |
ANeonObject: TNeonRttiObject; | |
AContext: ISerializerContext | |
): TJSONValue; override; | |
function Deserialize( | |
AValue: TJSONValue; | |
const AData: TValue; | |
ANeonObject: TNeonRttiObject; | |
AContext: IDeserializerContext | |
): TValue; override; | |
end; | |
implementation uses | |
System.SysUtils, | |
Neon.Core.Utils; | |
class function TBytesSerializer.GetTargetInfo: PTypeInfo; | |
begin | |
Result := TypeInfo(TBytes); | |
end; | |
class function TBytesSerializer.CanHandle(AType: PTypeInfo): Boolean; | |
begin | |
Result := (AType = GetTargetInfo()); | |
end; | |
function TBytesSerializer.Serialize( | |
const AValue: TValue; | |
ANeonObject: TNeonRttiObject; | |
AContext: ISerializerContext | |
): TJSONValue; | |
var | |
LBytes: TBytes; | |
LBase64: string; | |
begin | |
LBytes := AValue.AsType<TBytes>(); | |
LBase64 := TBase64.Encode(LBytes); | |
Result := TJSONString.Create(LBase64); | |
end; | |
function TBytesSerializer.Deserialize( | |
AValue: TJSONValue; | |
const AData: TValue; | |
ANeonObject: TNeonRttiObject; | |
AContext: IDeserializerContext | |
): TValue; | |
var | |
LBytes: TBytes; | |
begin | |
LBytes := TBase64.Decode(AValue.Value); | |
Result := TValue.From(LBytes); | |
end; | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment