Reader small image

You're reading from  Expert Delphi - Second Edition

Product typeBook
Published inFeb 2024
Reading LevelExpert
PublisherPackt
ISBN-139781805121107
Edition2nd Edition
Languages
Right arrow
Authors (2):
Marco Cantù
Marco Cantù
author image
Marco Cantù

Marco Cantù is an experienced Delphi expert, who started working with the product since its introduction in 1995. He is currently working as a Product Manager for RAD Studio at Embarcadero Technologies, an Idera company. Prior to that, Marco was a Delphi trainer and consultant for Wintech Italia. Over the years, Marco has written 20 books on Delphi, from the classic Mastering Delphi series to the recent Object Pascal Handbook. Marco has been a speaker at many Delphi and programming conferences worldwide, including over 10 Borland US Conferences, the Software Development Conference, Borland European conferences, EKON (Germany), DCon (UK), Conference to the Max (Holland), DelphiTage, the Italian Delphi Day, and a few editions of Delphi Developer Days. Marco is based in Italy.
Read more about Marco Cantù

Paweł Głowacki
Paweł Głowacki
author image
Paweł Głowacki

Paweł Głowacki was Embarcadero's European Technical Lead for Developer Tools. Previously, Paweł spent over 7 years working as a senior consultant and trainer for Delphi within Borland Education Services and CodeGear. Apart from working with Embarcadero customers across the region, he represented Embarcadero internationally as a conference and seminar speaker. Paweł passed away in mid-December 2017, but he is alive in the hearts of the Delphi developers community, worldwide.
Read more about Paweł Głowacki

View More author details
Right arrow

Packing Up Your Toolbox

There are many simple everyday programming skills that every Delphi developer needs to have. Pack your everyday programmer toolbox with everything you will need for simple things such as file input/output and more complex ones such as working with JSON and XML. These techniques are all part of the Delphi Runtime Library (RTL), a very large collection of core functions and classes that don’t involve the user interface and generally work on all target platforms.

This chapter will cover the following topics:

  • Working with files and streams
  • Working with JSON
  • Working with XML

The objective of this chapter is to become fluent in using Delphi programming through useful techniques such as working with files, streams, JSON, and XML.

Technical requirements

All the code in this chapter can be found in the following link: https://github.com/PacktPublishing/Expert-Delphi_Second-edition

Working with files and streams

Almost every app needs to persist data. Imagine that you have just downloaded an app and worked with it for a while. The next time you open it, you would like to see that it has remembered what you have done so far. An app can store its data in the cloud, in an embedded database, or in a file. This last option is the easiest to use, and so it’s the one we want to start from.

A local file can store information in different formats. It could be a binary file, which is just an array of bytes that is left to an app to make sense of, or it could be a text file. Your app can store information in plain text or it can use file formats such as JSON or XML to make it easier to process structured information, even if saved as text.

Imagine that you would like to write a small mobile app to keep track of your favorite locations on the internet. To keep it simple, it could be just a list of favorite items made up of two strings: a URL and a caption. Let...

Working with JSON

A better format for storing favorite entries is JSON. This is currently the most popular data interchange format. Its strength is its simplicity. The JSON specification is an interesting read and can be found at http://json.org. With a very simple type system, JSON is able to represent complex data structures. One of the key design objectives of JSON is to keep it simple. Most programming languages can process JSON, and Object Pascal is not an exception.

There are two main ways to work with JSON in Delphi. The first approach is to build an in-memory representation of the JSON tree using objects from the System.JSON unit. The second approach is based on streaming and provides sequential access to JSON data. In reality, both ways have their advantages and disadvantages. Let’s look at both.

JSON is a very popular format for sharing information and all kinds of datasets. Imagine you have just locally downloaded a piece of JSON. Now what?

Here is the favorites...

Working with XML

Before JSON dominated the world, XML was the most popular format for data interchange. With the addition of many new specifications, XML became very complex to process in code, and very cumbersome for a number of use cases. However, at a practical level, XML is still used in many circumstances and it’s important to understand how to process XML in case you need to.

XML processing in Delphi is based on building an in-memory representation of the XML file using XML parsers from different vendors. The key component for working with XML is TXMLDocument. XML parsing is considerably more difficult than it is with JSON. TXMLDocument has a pluggable XML parser architecture, and Delphi comes with a number of XML parser implementations from different vendors. There are different vendor implementations available depending on the target platform, as you can see in Figure 3.6 for Windows.

Figure 3.6: The available DOM vendors in the TXMLDocument component

Figure 3.6: The available DOM vendors in the TXMLDocument...

Summary

In this chapter, we have learned some useful everyday skills that Delphi developers can benefit from.

In the first part, we looked into working with text files. Later, we moved on to taking advantage of popular structured file formats such as JSON and XML. We have looked into working with both file formats by building their in-memory representation. In the case of JSON, we have also looked into reading and writing data using dedicated readers and writers. While the focus of the chapter was on reading and writing files of different formats, we took advantage of this topic to explore many practical Delphi techniques and compared different approaches to accomplish the same task.

In the next chapter, we’ll focus on another core building block of the Delphi RTL – namely, the support for threads, tasks, and parallel programming. This is a complex topic but one you cannot ignore.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Expert Delphi - Second Edition
Published in: Feb 2024Publisher: PacktISBN-13: 9781805121107
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Marco Cantù

Marco Cantù is an experienced Delphi expert, who started working with the product since its introduction in 1995. He is currently working as a Product Manager for RAD Studio at Embarcadero Technologies, an Idera company. Prior to that, Marco was a Delphi trainer and consultant for Wintech Italia. Over the years, Marco has written 20 books on Delphi, from the classic Mastering Delphi series to the recent Object Pascal Handbook. Marco has been a speaker at many Delphi and programming conferences worldwide, including over 10 Borland US Conferences, the Software Development Conference, Borland European conferences, EKON (Germany), DCon (UK), Conference to the Max (Holland), DelphiTage, the Italian Delphi Day, and a few editions of Delphi Developer Days. Marco is based in Italy.
Read more about Marco Cantù

author image
Paweł Głowacki

Paweł Głowacki was Embarcadero's European Technical Lead for Developer Tools. Previously, Paweł spent over 7 years working as a senior consultant and trainer for Delphi within Borland Education Services and CodeGear. Apart from working with Embarcadero customers across the region, he represented Embarcadero internationally as a conference and seminar speaker. Paweł passed away in mid-December 2017, but he is alive in the hearts of the Delphi developers community, worldwide.
Read more about Paweł Głowacki