{"id":74,"date":"2011-02-11T08:00:00","date_gmt":"2011-02-11T14:00:00","guid":{"rendered":"http:\/\/blogs.wp.stage.cpanel.net\/2011\/02\/json_vs_xml_in_livephp\/"},"modified":"2011-02-11T08:00:00","modified_gmt":"2011-02-11T14:00:00","slug":"json_vs_xml_in_livephp","status":"publish","type":"post","link":"https:\/\/devel.www.cpanel.net\/blog\/products\/json_vs_xml_in_livephp\/","title":{"rendered":"JSON vs. XML in LivePHP"},"content":{"rendered":"

A while back, Matt Dees blogged<\/a> about our upcoming change to LivePHP in 11.28. Specifically, he mentions the use of JSON. In this article I will illustrate, in brief, why this change was made. The decision process, as you’ll see, wasn’t exactly straight forward, but a solid compromise.<\/p>\n

I’m not going to expound on the benefits or drawbacks of using XML vs. JSON as a format. We’ve see those debates for a while now in various online (and offline) venues. Instead, I’d like to talk about our choice to use JSON within LivePHP, based on data from a three point comparison we did.<\/p>\n

First, let’s go over some background information. The LivePHP class was originally written for PHP 4 compatibility. Those were the days when we couldn’t depend on having a parser for XML or JSON readily available within a given PHP binary. One of the best options when we initially wrote LivePHP was to ‘include’ a pure PHP XML parser. But thankfully, PHP 4 has nearly gone the way of the dinosaurs and cPanel’s internal build of PHP has been 5.2.x. for awhile now. This allowed us the opportunity to refactor the LivePHP class.<\/p>\n

Once we decided to refactor the LivePHP class, we boiled our wish list down to two key points:<\/p>\n

    \n
  1. Reduce resource consumption.<\/li>\n
  2. Maintain backwards compatibility for anyone who relied on an external parser or any legacy code within cPanel that produces a particular output string.<\/li>\n<\/ol>\n

    Maintaining backwards compatibility for the cPanel strings was a fairly easy task. We knew that XML strings were always sent in the legacy code, so as long as we could detect a “pure” XML structure, we could use SimpleXML or the pure XML parsing functions that were already being shipped. But which one — SimpleXML or pure PHP? SimpleXML seemed like the obvious answer until we explored the full meaning of this compatibility goal.<\/p>\n

    Since we not only wanted compatibility on the string level, but also from the utility level, the idea of changing the external parsing functions needed more consideration and, as you’ll see, compromise.<\/p>\n

    The likelihood that someone uses those pure PHP functions directly (in their own, neighboring code) is small. However, if we were to suddenly remove them or rewrite their internals, the potential risk of breaking someone’s application seemed too great. And thus, we struck a compromise: we would ship the parsing functions as they had always been and simply create a logic flow within the class that would fallback to the old functions if a legacy-style string happened to come across the wire.<\/p>\n

    The other goal was to reduce resource consumption. The refactoring process was not only about optimizing the code, but also making it “better. ” While evaluating this class, we found a ton of stuff that we wanted to change. In the end, we had to ask ourselves, “How can we cover the most ground while making the fewest changes?” Sure, there were plenty of other changes like more robust logging, conforming to standard PHP 5 class conventions, and a more PEAR-ish coding style that would eventually make it into the final revision of LivePHP. But really, the true gain would ultimately come down to dealing with the overhead of encoding and decoding the data stream. This is where a few simple tests helped us decide that PHP’s native JSON functions would work best for us.<\/p>\n

    For brevity’s sake, I won’t include the actual code in this article. Fortunately, it’s straight forward enough to describe the tests and present the results in a few charts.<\/p>\n

    We used two datasets, both based on real output from the Email::listpopswithdisk API2 function.. This function’s return value is an itemized list of email account details for a cPanel account. The first set is very small; only two email accounts exist for the cPanel user. The second set is large, but not uncommon at 5000 email accounts for the cPanel user.<\/p>\n

    We’ll compare three scenarios that all produce a PHP associative array:<\/p>\n

      \n
    1. JSON string decoded with “json_decode($str, true)” [“true” produces the array]<\/li>\n
    2. XML string decoded with the legacy pure PHP functions<\/li>\n
    3. XML string decoded with “simplexml_load_string($str)” [in a recursive callback to get an array]<\/li>\n<\/ol>\n
      \"\"<\/a>
      Click to enlarge...<\/figcaption><\/figure>\n

      The first, small dataset yielded mildly interesting results. The data makes it pretty clear that SimpleXML takes less memory at compile and execution time and during the life of the script. It also responds just as fast as the JSON.<\/p>\n

      However, in the large dataset, the results are a bit more intriguing. The JSON version is using more memory at the start of execution, but once the script is done, it consumes the least memory over time. As one might guess, the pure SimpleXML parsing consumes a noticeably larger amount of memory during its lifetime. The pure PHP XML functions perform well, but as a whole they just aren’t as good as the native JSON parsing.<\/p>\n

      And then there’s the execution time on the larger dataset…wow! The pure PHP XML functions are just atrocious in comparison to the other two methods. SimpleXML, while consuming 42% more memory, took twice as long as the JSON.<\/p>\n

      Ultimately, we decided that PHP’s native JSON parsing was the best fit. We had several debates about the various permutations of these scenarios and dataset sizes. That is, with really small datasets, SimpleXML is the obvious tool of choice. We also did some analogous Perl testing to see how the other end of the system would perform. The results were conclusively in favor of JSON (we predominantly use JSON::XS nowadays). So, in the end, we came to the consensus that datasets tend to be larger rather than smaller and that execution time is the hardest resource to compromise. This makes JSON the best all around solution for LivePHP.<\/p>\n","protected":false},"excerpt":{"rendered":"

      A while back, Matt Dees blogged about our upcoming change to LivePHP in 11.28. Specifically, he mentions the use of JSON. In this article I will illustrate, in brief, why this change was made. The decision process, as you’ll see, wasn’t exactly straight forward, but a solid compromise.<\/p>\n","protected":false},"author":77,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[49],"tags":[],"class_list":["post-74","post","type-post","status-publish","format-standard","hentry","category-products"],"acf":[],"yoast_head":"\nJSON vs. XML in LivePHP | cPanel<\/title>\n <!-- Mirrored from cpanel.net/wp-json/wp/v2/posts/74/ by HTTrack Website Copier/3.x [XR&CO'2014], Fri, 06 Sep 2024 09:53:40 GMT --> <meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=_-2063.html \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JSON vs. XML in LivePHP | cPanel\" \/>\n<meta property=\"og:description\" content=\"A while back, Matt Dees blogged about our upcoming change to LivePHP in 11.28. Specifically, he mentions the use of JSON. In this article I will illustrate, in brief, why this change was made. The decision process, as you’ll see, wasn’t exactly straight forward, but a solid compromise.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/devel.www.cpanel.net\/blog\/products\/json_vs_xml_in_livephp\/\" \/>\n<meta property=\"og:site_name\" content=\"cPanel\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/cpanel\/\" \/>\n<meta property=\"article:published_time\" content=\"2011-02-11T14:00:00+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/blog.cpanel.com\/wp-content\/uploads\/2011\/02\/Blog_charts-300x181.png\" \/>\n<meta name=\"author\" content=\"cPanel Community\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@cPanel\" \/>\n<meta name=\"twitter:site\" content=\"@cPanel\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"cPanel Community\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/devel.www.cpanel.net\/blog\/products\/json_vs_xml_in_livephp\/\",\"url\":\"https:\/\/devel.www.cpanel.net\/blog\/products\/json_vs_xml_in_livephp\/\",\"name\":\"JSON vs. XML in LivePHP | cPanel\",\"isPartOf\":{\"@id\":\"https:\/\/devel.www.cpanel.net\/#website\"},\"datePublished\":\"2011-02-11T14:00:00+00:00\",\"dateModified\":\"2011-02-11T14:00:00+00:00\",\"author\":{\"@id\":\"https:\/\/devel.www.cpanel.net\/#\/schema\/person\/8cf97408aad4fb70cf55d11a1d4f57f8\"},\"breadcrumb\":{\"@id\":\"https:\/\/devel.www.cpanel.net\/blog\/products\/json_vs_xml_in_livephp\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/devel.www.cpanel.net\/blog\/products\/json_vs_xml_in_livephp\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/devel.www.cpanel.net\/blog\/products\/json_vs_xml_in_livephp\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/devel.www.cpanel.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JSON vs. XML in LivePHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/devel.www.cpanel.net\/#website\",\"url\":\"https:\/\/devel.www.cpanel.net\/\",\"name\":\"cPanel\",\"description\":\"Hosting Platform of Choices\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/devel.www.cpanel.net\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/devel.www.cpanel.net\/#\/schema\/person\/8cf97408aad4fb70cf55d11a1d4f57f8\",\"name\":\"cPanel Community\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/devel.www.cpanel.net\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e1949945083b5526bb95711bd3d616b3?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e1949945083b5526bb95711bd3d616b3?s=96&d=mm&r=g\",\"caption\":\"cPanel Community\"},\"description\":\"The web hosting industry's most reliable management solution since 1997. With our first-class support and rich feature set, it's easy to see why our customers and partners make cPanel & WHM their hosting platform of choice. For more information, visit cPanel.net.\",\"sameAs\":[\"https:\/\/cpanel.net\"],\"url\":\"https:\/\/devel.www.cpanel.net\/blog\/author\/cpadmin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JSON vs. XML in LivePHP | cPanel","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/devel.www.cpanel.net\/blog\/products\/json_vs_xml_in_livephp\/","og_locale":"en_US","og_type":"article","og_title":"JSON vs. XML in LivePHP | cPanel","og_description":"A while back, Matt Dees blogged about our upcoming change to LivePHP in 11.28. Specifically, he mentions the use of JSON. In this article I will illustrate, in brief, why this change was made. The decision process, as you’ll see, wasn’t exactly straight forward, but a solid compromise.","og_url":"https:\/\/devel.www.cpanel.net\/blog\/products\/json_vs_xml_in_livephp\/","og_site_name":"cPanel","article_publisher":"https:\/\/www.facebook.com\/cpanel\/","article_published_time":"2011-02-11T14:00:00+00:00","og_image":[{"url":"http:\/\/blog.cpanel.com\/wp-content\/uploads\/2011\/02\/Blog_charts-300x181.png"}],"author":"cPanel Community","twitter_card":"summary_large_image","twitter_creator":"@cPanel","twitter_site":"@cPanel","twitter_misc":{"Written by":"cPanel Community","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/devel.www.cpanel.net\/blog\/products\/json_vs_xml_in_livephp\/","url":"https:\/\/devel.www.cpanel.net\/blog\/products\/json_vs_xml_in_livephp\/","name":"JSON vs. XML in LivePHP | cPanel","isPartOf":{"@id":"https:\/\/devel.www.cpanel.net\/#website"},"datePublished":"2011-02-11T14:00:00+00:00","dateModified":"2011-02-11T14:00:00+00:00","author":{"@id":"https:\/\/devel.www.cpanel.net\/#\/schema\/person\/8cf97408aad4fb70cf55d11a1d4f57f8"},"breadcrumb":{"@id":"https:\/\/devel.www.cpanel.net\/blog\/products\/json_vs_xml_in_livephp\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/devel.www.cpanel.net\/blog\/products\/json_vs_xml_in_livephp\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/devel.www.cpanel.net\/blog\/products\/json_vs_xml_in_livephp\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/devel.www.cpanel.net\/"},{"@type":"ListItem","position":2,"name":"JSON vs. XML in LivePHP"}]},{"@type":"WebSite","@id":"https:\/\/devel.www.cpanel.net\/#website","url":"https:\/\/devel.www.cpanel.net\/","name":"cPanel","description":"Hosting Platform of Choices","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/devel.www.cpanel.net\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/devel.www.cpanel.net\/#\/schema\/person\/8cf97408aad4fb70cf55d11a1d4f57f8","name":"cPanel Community","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/devel.www.cpanel.net\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e1949945083b5526bb95711bd3d616b3?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e1949945083b5526bb95711bd3d616b3?s=96&d=mm&r=g","caption":"cPanel Community"},"description":"The web hosting industry's most reliable management solution since 1997. With our first-class support and rich feature set, it's easy to see why our customers and partners make cPanel & WHM their hosting platform of choice. For more information, visit cPanel.net.","sameAs":["https:\/\/cpanel.net"],"url":"https:\/\/devel.www.cpanel.net\/blog\/author\/cpadmin\/"}]}},"_links":{"self":[{"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/posts\/74"}],"collection":[{"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/users\/77"}],"replies":[{"embeddable":true,"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/comments?post=74"}],"version-history":[{"count":0,"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/posts\/74\/revisions"}],"wp:attachment":[{"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/media?parent=74"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/categories?post=74"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/tags?post=74"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}