Skip to content

NeonFormat

Paolo Rossi edited this page May 2, 2024 · 3 revisions

NeonFormat Attribute

The NeonFormat attribute allows TBytes properties or fields to be serialized in Base64 format, this feature is useful to output large chunk of data stored as TBytes With Neon v3.0.0 the format is completely customizable bacause you need to pass as parameter to the NeonFormat attribute is a string that you have to match in your custom serializer.

In Neon there is a TBytes custom serializer ready to use (you must register the TBytesSerializer in the config), but TBytesSerializer is an example to extend or redo a custom format output for Delphi types.

By default TBytes are serialized by an array of bytes:

var b: TBytes;
b := [5,12,6,55,30];

// JSON ==> "[5,12,6,55,30]"

If you register the TBytesSerializer

var
  LConf: INeonConfiguration;
begin
  LConf := TNeonConfiguration.Default
    .RegisterItemFactory(TPetFactory);

the default output for the TBytes properties (or fields) becomes Base64

var b: TBytes;
b := [5,12,6,55,30];

// JSON ==> "BQwGNx4="

In order to (selectively) switch to the native format even with the TBytesSerializer you must annotate the property (or field) with the NeonFormat attribute

[NeonFormat('native')]
property Stuff: TBytes read FStuff write FStuff;

Obj.Stuff := [5,12,6,55,30];

// JSON ==> "[5,12,6,55,30]"
Clone this wiki locally