Hi everybody and welcome to CodeRage 2019.
My name is Xavier Martinez Embarcadero MVP from Andorra
In this presentation, I will show you how to use one of the newest units included in Delphi
This unit is JSON serializers
This unit. We will help you to customize a serialization
Well, let's go to the demo
We have here
This class the person with some properties and
Two methods to the serialization
Traditional string and from JSON to string - JSON string. Sorry
First of all, we need to know this unit
Works with the private part of the class
If we execute the application we can see that
Okay, we have the values from private
Section of the class
But
if we want to
work with the public section
We need to specify
with this attribute
and
If we see this class
This class needs
This enumerator
And this enumeration
has three possible values
Fields is the for value and we get our fields from the private section
Public is the same as fields, but with the public section and
In is the opposite of the others it only returns the values as specified by JSON in attribute
We will see now an example. We have seen the default behavior already
Now we will see behavior with the public enumerator
To declare it we need to use
The JSON serialize attribute
we can write the JSOn serialize attribute or
the JSON serialize only
If we run now the application
We get
The name properties of the public section
See P2
Address creation date display name etc
And
if we want to
return the
Specific fields we need to use the In enumeration like this
Okay, if we run the application we
Don't get any result because we don't have specific anything
If we want to return something we need to use for
example we
change this
We we need use the JSON In attribute
Now if we run the application
We get only
the
P1 property perfect
and if we
Want to exclude
some fields we need to use JSON ignore attribute and
If we run this we get all fields, except P1
See P2 account ID address etc but not P1
Okay now
We will know
how to include or ignore properties but
What happened with our enumerations? For example this - the gender?
To see it we will -
To see it more easily. We will use the In attribute here
With specifying gender
Like only result with JSON In attribute
Okay, we run the application now
And we get only one result and
We can see that we get the string instead male
What we can do to get merge to, we need to define a converter, let's go to see how
We will use the
JSON converter attribute to convert a result
now if we execute
the program
We will get male and if we put female
We will get female perfect and if we get it will put
Something like that we will get an error
how to solve eight
For this purpose, we need to create a new class
inherits from the JSON EnumName converter
And we write
this method by JSON
what we do is check if ordinal value of AValue is a
constant
Is rather than High TGender
If true we return an error
Here or else
We inherit the normal behavior, and now it only remains to change the class to using the converter
We get our class
And
We change the converter with our class
and if we execute the
program
We have to get the program
We can change male
And we get undefined gender perfect
Under here the explanation of the use of this unit
thank you very much for watching
If you want to download the demo you can download it from cadetill.com files coderage2019
JSONdemo.zip
Thank you very much