Monday, May 14, 2018

JSON Data Binding with Liquid XML 2018 – Update 1

XML to JSON and JSON to XML the smart way


Liquid XML Data Binder has helped developers work with XML for nearly 20 years. In more recent times passing data between heterogeneous systems, specifically backend to mobile devices, has become increasingly reliant on using JSON data structures.

In order to facilitate this, we have introduced the FromJson and ToJson set of methods to the Liquid Runtime API which will serialize and deserialize JSON data to and from the generated data objects.

Supported languages are C++, C#, Visual Basic .Net and Java.

C# Example

    // create an instance of the class to load the XML file into
    Bookstore elm = new Bookstore();
                    
    // load the initial XML data from a file into the object
    elm.FromXmlFile(filename);

    // change the price of the first book to 10.99
    elm.BookCol[0].Price = 10.99;
    
    // extract the updated data into an XML string
    string strXml = elm.ToXml();
    Console.WriteLine($"The XML created\n{strXml}");
    
    // extract the updated data into a JSON string
    string strJson = elm.ToJson();
    Console.WriteLine($"The JSON created\n{strJson}");

C++ Example

    // create an instance of the class to load the XML file into
    CBookstorePtr spElm = CBookstore::CreateInstance();
    
    // load the initial XML data from a file into the object
    spElm ->FromXmlFile(lpctFilename);
    
    // change the price of the first book to 10.99
    spElm->GetBookCol()->Item(0)->SetPrice(10.99);

    // extract the updated data into an XML string
    std::tstring strXml = spElm ->ToXml();
    _tprintf(_T("The XML created\n%s"), strXml.c_str());
    
    // extract the updated data into a JSON string
    std::tstring strJson = spElm ->ToJson();
    _tprintf(_T("The JSON created\n%s"), strJson.c_str());

The XML created

    <bs:bookstore xmlns:bs="http://www.liquid-technologies.com/sample/bookstore">
                   xmlns:xs="http://www.w3.org/2001/XMLSchema-instance">
      <bs:book price="10.99" publicationdate="2008-10-20" ISBN="978-0747596837">
      <bs:title>The Graveyard Book</bs:title>
      <bs:author>
        <bs:first-name>Neil</bs:first-name>
        <bs:last-name>Gaiman</bs:last-name>
      </bs:author>
      <bs:genre>Horror</bs:genre>
      </bs:book>
... etc ...
    </bs:bookstore>

The JSON created

  {
    "bs:bookstore": {
      "@xmlns:bs": "http://www.liquid-technologies.com/sample/bookstore",
      "@xmlns:xs": "http://www.w3.org/2001/XMLSchema-instance",
      "bs:book": [
        {
          "@price": 10.99,
          "@publicationdate": "2008-10-20",
          "@ISBN": "978-0747596837",
          "bs:title": "The Graveyard Book",
          "bs:author": {
            "bs:first-name": "Neil",
            "bs:last-name": "Gaiman"
          },
          "bs:genre": "Horror"
        },
... etc ...
      ]
    }
  }

 

JSON to Object Roundtripping 

 

Looking at the JSON document, by default, attributes are prefixed with '@', namespace attributes are written out '@xmlns:bs' and names contain the namespace prefix 'bs:'. This is to enable full roundtripping of JSON back into the data objects using ToJson and FromJson methods.

If we want a vanilla JSON document, we can control the output using the SerializationContext.

 

C# Example

    SerializationContext.Default.JsonContext.SupportRoundtrip = JsonContext.RoundTrip.None;

C++ Example

    CSerializationContext::GetDefaultContext().GetJsonContext().SetSupportRoundtrip(JsonRoundTrip_None);

Now when the code is executed, the JSON output is as follows

  {
    "bookstore": {
      "book": [
        {
          "price": 10.99,
          "publicationdate": "2008-10-20",
          "ISBN": "978-0747596837",
          "title": "The Graveyard Book",
          "author": {
            "first-name": "Neil",
            "last-name": "Gaiman"
          },
          "genre": "Horror"
        },
... etc ...
      ]
    }
  }


Download a free trial of Liquid XML Data Binder today:
https://www.liquid-technologies.com/trial-download