Liquid XML Objects (C#, Visual Basic .Net) / Using the Generated Code / Writing JSON
In This Topic
    Writing JSON
    In This Topic

    Liquid XML Objects generates an XML Data Model from an XML Schema with functionality to serialize JSON documents out of the XML Data Model. Consider the following XML Schema:

     Sample XML Schema

            

     

    Bookstore.xsd
    Copy Code
    <?xml version="1.0" encoding="utf-8" ?>
    <!--Created with Liquid Studio (https://www.liquid-technologies.com)-->
    <xsd:schema xmlns:bs="http://www.liquid-technologies.com/sample/bookstore" elementFormDefault="qualified" targetNamespace="http://www.liquid-technologies.com/sample/bookstore" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <xsd:complexType name="bookstoreType">
            <xsd:sequence>
                <xsd:element name="book" type="bs:bookType" minOccurs="0" maxOccurs="unbounded" />
            </xsd:sequence>
        </xsd:complexType>
        <xsd:complexType name="bookType">
            <xsd:sequence>
                <xsd:element name="title">
                    <xsd:annotation>
                        <xsd:documentation>The title of the book. 
    Max 50 characters.</xsd:documentation>
                    </xsd:annotation>
                    <xsd:simpleType>
                        <xsd:restriction base="xsd:string">
                            <xsd:maxLength value="50" />
                        </xsd:restriction>
                    </xsd:simpleType>
                </xsd:element>
                <xsd:element name="author" type="bs:authorName" />
                <xsd:element name="genre" minOccurs="0">
                    <xsd:simpleType>
                        <xsd:restriction base="xsd:string">
                            <xsd:enumeration value="Reference" />
                            <xsd:enumeration value="Science fiction" />
                            <xsd:enumeration value="Action and Adventure" />
                            <xsd:enumeration value="Romance" />
                            <xsd:enumeration value="Mystery" />
                            <xsd:enumeration value="Horror" />
                            <xsd:enumeration value="Health" />
                            <xsd:enumeration value="Travel" />
                            <xsd:enumeration value="History" />
                            <xsd:enumeration value="Fantasy" />
                        </xsd:restriction>
                    </xsd:simpleType>
                </xsd:element>
            </xsd:sequence>
            <xsd:attribute name="price" use="required">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:decimal">
                        <xsd:fractionDigits value="2" />
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:attribute>
            <xsd:attribute name="publicationdate" type="xsd:date" />
            <xsd:attribute name="ISBN" type="xsd:string" />
        </xsd:complexType>
        <xsd:element name="bookstore" type="bs:bookstoreType" />
        <xsd:complexType name="authorName">
            <xsd:sequence>
                <xsd:element name="first-name" type="xsd:string">
                    <xsd:annotation>
                        <xsd:documentation>The authors first name.
    Max 50 characters.</xsd:documentation>
                    </xsd:annotation>
                </xsd:element>
                <xsd:element name="last-name" type="xsd:string" />
            </xsd:sequence>
        </xsd:complexType>
    </xsd:schema>
    
     Sample Writer Code

    The following code illustrates how to create a new instance of bookstore from scratch and populate it.

    In order to serialize the class to JSON an LjSerializer object is required.

    Writing data into the generated classes
    Copy Code
    BookstoreElm bookstore = new BookstoreElm();
    BookTypeCt book = new BookTypeCt();
    book.Title = "Artemis";
    book.Author.First_Name = "Andy";
    book.Author.Last_Name = "Weir";
    book.Genre = BookTypeCt.GenreEnum.Science_Fiction;
    book.Price = 7.99;
    book.ISBN = "978-1-78-503025-3";
    book.Publicationdate = LxDateTime.CreateDate(2017, 4, 23);
    
    bookstore.Books.Add(book);
    
    LjSerializer<BookstoreElm> serializer = new LjSerializer<BookstoreElm>();
    using (StringWriter sw = new StringWriter())
    {
        serializer.Serialize(sw, bookstore);
    
        Console.WriteLine(sw.ToString());
    }
    

     

    This example code is generated using the default 'Simplified Model', other model options are explored in Code Generation Models.
     JSON Output
    BookstoreSample.json
    Copy Code
    {
      "bookstore": {
        "@xmlns": "http://www.liquid-technologies.com/sample/bookstore",
        "@xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
        "book": [
          {
            "@price": 7.99,
            "@publicationdate": "2017-04-23Z",
            "@ISBN": "978-1-78-503025-3",
            "title": "Artemis",
            "author": {
              "first-name": "Andy",
              "last-name": "Weir"
            },
            "genre": "Science fiction"
          }
        ]
      }
    }
    

     

     

    LjSerializer

    The LjSerializer uses the meta data attributes associated with the generated class to serialize and de-serialize them to XML.

    The easiest way to use the LjSerializer class is via the generic wrapper LjSerializer<T>. The templated version does some of the configuration and casting making it easier to work with.

    The LjSerializer object is quite expensive to create, and LjSerializer<T> will implicitly create an LjSerializer instance in its constructor (unless one is explicitly supplied). So if your code repeatedly creates instances of LjSerializer<T> (for the same or different types of T), then you should consider explicitly creating an instance of  LjSerializer and passing it to the constructor of LjSerializer<T>.

    LjSerializer (and thus LjSerializer<T>) are not thread safe. i.e. don't share them between threads.