![]() Pretty simple – because we delegate the actual work to a class named TUTCDateTimeInterceptor, which we will have a look at later. To abbreviate this information we want an attribute named JsonUTCDate, so that a field declaration should look like this: In addition there should be a null date equivalent to the ISO string “T00:00:00.000Z”, but the Json for that shall be a null value for these instead of that string value. What if we could just say: Hey, this is a TDateTime field, but I want it to be converted to UTC in the Json representation. While the date format is not a bad choice, the requirements to provide UTC dates may force some work unwanted on us. As mentioned above, the AddBody method uses the default TJsonOptions for the ObjectToJsonObject call, which are joDateIsUTC and joDateFormatISO8601. Let’s start with a simple task as a warm-up. The allies in this endeavor are attributes. I hate to disappoint you, but the generic object lists will be covered in the next blog post, while this one tackles an easier task – it would have been too long otherwise.Īlthough the Json serializing is quite resistant to extensions, there are means to achieve more than appears possible on a first look. What can we do to make serializing generic object lists being serialized properly in both directions? Unfortunately those are not handled pretty well and in most cases are they even not what a non Delphi counterparts accept or deliver – mostly they use arrays for these lists. Generic object lists derived from TObjectList are way more feasible and thus the preferred way to go. In reality the use of object arrays as fields inside a class is rarely a valid approach. Without any tweaking the Json serializer takes each field of the class, where it does a pretty good job for simple (value) types, objects and arrays of both. We make a note that the call just accepts the default TJsonOptions for the second parameter. If we inspect the above mentioned generic AddBody method we soon find out that the serializing part is outsourced to the TJson.ObjectToJsonObject method. Nourishing this with some advanced techniques shown in this post may be enough to keep the benefits without the need for external code. With a bit of care taken when designing the objects to serialize the results are often quite satisfying and fit the requirements. I guess it is not myself alone being tempted to make use of it. The ease and elegance of a TRESTRequest.AddBody(myInstance) call is hard to attain with other means. Simplicity and the availability with every (decent) Delphi installation being the most mentioned ones. While the majority suggests to use another library or a self implemented solution, there are others who would prefer the built-in tools for a couple of reasons. Serializing objects to Json as well as de-serializing them with the Delphi standard libraries has been subject to many discussions. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2022
Categories |