REST easy with kbmMW #24 – XML, JSON or YAML to object conversion

How often have you received an XML or JSON or for that matter a YAML file that you need to interpret?

I know I personally have received many from customers or customer partners who would like a new customer system to integrate nicely with them, transferring XML or JSON files.

Sometimes you may get an XSD (an XML Schema Definition file), which explains how the XML document is formatted, which is great, because then you can use kbmMW’s XSD importer to interpret the XSD and generate Delphi class definitions making it easy to read and/or write the XML file according to the format given in the XSD. In fact, having the XSD, makes it easy to also read and write the data from and to JSON and other kbmMW supported object notation formats.

However I can’t count the times I have received an example of the actual data file, rather than the XSD definition.

To interpret the file in such case, there are two options

  • to use a generic XML or JSON or YAML reader to read the file, and manually extract required data from it
  • or to handcode Delphi class definitions with relevant kbmMW attributes, making it easy for kbmMW to both parse and generate such files

However the first option is not a very generic way to do things, and it does not help with writing a compatible file. The second option can be time consuming, because you need to analyse the file to determine its structure, to be able to generate the relevant object classes and lists, not to mention augmenting the classes and lists with relevant attributes.

Fortunately kbmMW comes to the rescue…again 🙂 That’s the whole point of programming… to make life easier… so when I notice stuff that could make my life easier, kbmMW receives a new feature, hopefully making the (programming) life of kbmMW users easier too.

So next release of kbmMW contains built in support for parsing and analysing data files (XML, JSON and YAML for the time being), and generates relevant attribute augmented Delphi classes, making it easy to use kbmMW’s marshalling framework to read and write such files.

Lets look at a couple of files and the code generated for handling them.

One file is called auctions.json. I found it somewhere on the internet, but it is just an example. Any other JSON file could have been used.

As you can see, its fairly big, and relatively complex with multiple arrays of objects.

We make kbmMW analyse it and output the relevant Delphi based source code, that makes it easy to marshal/unmarshal such data:

What happens is that we ask the JSON marshalling class to generate the source code for the classes needed for us, matching the contents of auctions.json, producing a unit called Unit2 and a main class called TJSONData. The reason for the main class, is that the main object in the json file is unnamed or anonymous, hence we need to provide some name for the generator to use in the Delphi code.

Simply write the contents of s to a file called Unit2.pas and add it to the projects where you need to read or write auctions.json files.

This is the produce:

To actually read the file, create a program, add Unit2.pas to it, and write this code:

It is as simple as that.

Another example is the XML file courses.xml, also picked up from the internet somewhere.

To create Delphi source code to marshal and unmarshal that type of file

That will analyse courses.xml and produce a string containing Delphi source code for a unit called Unit3. We also, by convention, provide a main class name, but it will not be used since XML do not, like JSON, operate with an anonymous root datastructure.

The generated unit will look like this:

And to read and write such XML data, this code can be used

If the kbmMW file analyser detects a type it does not know what to do with, it will auto generate one called TUNKNOWN, which will need to be manually handled by the developer, by replacing it with more relevant class definitions to be able to marshal and unmarshal that particular part of the data.

If you like what you see, please share the word. Reshare blog posts and let others know about the product!

Essentially help us to help you 🙂

204 total views, 2 views today

Author: kimbomadsen

1 thought on “REST easy with kbmMW #24 – XML, JSON or YAML to object conversion

Leave a Reply

Your email address will not be published. Required fields are marked *

five × 1 =

This site uses Akismet to reduce spam. Learn how your comment data is processed.