12

We are creating an application that uses a lot of XML parsing and I thought maybe to use JSON, because we can use JSON as an alternative to XML.

I need to test which is faster JSON or XML, but thought to ask an opinion here first.

JSON in JavaScript is native and it's fast. Certainly faster than parsing XML. But in Delphi there's no native classes for doing that.

So my question is - which is faster in Delphi, using XML or JSON parser library? Or are they equal in speed?

And if it's faster then which you prefer - Delphi Web Utils, JSON Delphi Library or JSON Toolkit

evilone
  • 22,410
  • 7
  • 80
  • 107

1 Answers1

12

With a well written parser, XML and JSON will have more or less the same timing. You can have a slow JSON parser, and a fast XML parser.

Perhaps a bit slower for XML because the syntax is more complex than JSON's.

But the bottleneck will be mostly reading from the hard drive, not parsing the content.

We used JSON for the Client/Server of our ORM, for several reasons (but you'll find others, I don't want to troll here, just speak from our little experiment):

  • Like XML, it's a text-based, human-readable format for representing simple data structures and associative arrays (called objects);
  • It's easier to read (for both human beings and machines), quicker to implement, and usually smaller in size than XML;
  • It's a very efficient format for data caching;
  • Its layout allows to be rewritten in place into individual zero-terminated UTF-8 strings, with almost no wasted space: this feature is used for blazzling fast JSON to text conversion of the tables results, with no memory allocation nor data copy;
  • It's natively supported by the JavaScript language, making it a perfect serialization format in any AJAX (i.e. Web 2.0) application;
  • The JSON format is specified in a well known and simple RFC;
  • The default text encoding for both JSON and our ORM is UTF-8, which allows the full Unicode charset to be stored and communicated;
  • It is the default data format used by ASP.NET AJAX services created in Windows Communication Foundation (WCF) since .NET framework 3.5; so it's Microsoft officialy "ready";
  • For binary blob transmission, there is no CDATA as in XML. So we simply encode the binary data as hexadecimal or Base64 (uses less space) inside a JSON string.

About parsing speed, you could take a look at our in-place parser and JSON writer from SQLite3 results. It was very optimized for speed, and fast it is. We wrote a simple but efficient JSON serialization for any TPersistent, including collections. We just add a dynamic array JSON serializer, which is also very fast.

Additional note:

All those parsers differ from the one you mentioned because they parse the JSON content and format it as text inside the input buffer: there is no memory allocation made during parsing, so it should be faster than other solutions. Text content is unescaped, fields are #0 ended, and a pointer to the beginning of the text is computed. So to access a value, you just use the pointer to get the data. It usually parses some MB of JSON content with no time.

Also take a look at the JSON parser embedded in DWS. The author claimed it was fast. But still allocated a memory block for each object.

Arnaud Bouchez
  • 42,305
  • 3
  • 71
  • 159
  • About our in-place JSON parsing, and DOM/SAX XML parser techniques, you can take a look at http://blog.synopse.info/post/2011/06/02/Fast-JSON-parsing – Arnaud Bouchez Jun 03 '11 at 09:20
  • @Bouchez: is your JSON parser available for others to use? If yes, could you please provide the links to download it? Thank you – boggy Jun 11 '13 at 00:12
  • The parser is available in our [SynCommons.pas unit](http://synopse.info/fossil/finfo?name=SynCommons.pas), as a set of low-level functions. There is no high level classes able to convert directly any kind of JSON content into a tree, since we try to avoid such memory and CPU consuming process in mORMot, and we rely on simple objects or arrays. But you have powerful JSONToObject() and ObjectToJSON() functions. – Arnaud Bouchez Sep 17 '13 at 05:11