Skip to content

Adiqq/xml-to-json-converter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

xml-to-json-converter

Kotlin XML to JSON converter

Inspired by

Code

Input:

<test>
    <inside>
        <someString>string</someString>
        <someNumber>123</someNumber>
        <someBoolean>true</someBoolean>
        <someOtherNumber>123.45</someOtherNumber>
    </inside>
    <someArray>
        <arrayItem>1</arrayItem>
        <arrayItem>2</arrayItem>
        <arrayItem>3</arrayItem>
    </someArray>
    <complexArray>
        <complexItem>
            <name>test1</name>
            <value>1</value>
            <enabled>true</enabled>
        </complexItem>
        <complexItem>
            <name>test2</name>
            <value>2</value>
            <enabled>false</enabled>
        </complexItem>
        <complexItem>
            <name>test3</name>
            <value>3</value>
            <enabled>true</enabled>
        </complexItem>
    </complexArray>
</test>

Case - omit quotes from numbers and boolean

Code

@Test
    fun testXmltoJsonConversion_omit_both() {
        val converter = XmlToJsonConverter()
        val xml = IOUtils.toString(
                this::class.java.getResourceAsStream("/test.xml"),
                "UTF-8"
        )
        val json = IOUtils.toString(
                this::class.java.getResourceAsStream("/test_omitted_both.json"),
                "UTF-8"
        )
        val result = converter.convert(xml)!!.replace("\r\n", "\n")
        assertEquals(json,result)
    }

Output

{
  "test" : {
    "inside" : {
      "someString" : "string",
      "someNumber" : 123,
      "someBoolean" : true,
      "someOtherNumber" : "123.45"
    },
    "someArray" : {
      "arrayItem" : [ 1, 2, 3 ]
    },
    "complexArray" : {
      "complexItem" : [ {
        "name" : "test1",
        "value" : 1,
        "enabled" : true
      }, {
        "name" : "test2",
        "value" : 2,
        "enabled" : false
      }, {
        "name" : "test3",
        "value" : 3,
        "enabled" : true
      } ]
    }
  }
}

Case - omit quotes from numbers only

Code

@Test
    fun testXmltoJsonConversion_omit_booleans_false() {
        val converter = XmlToJsonConverter(omitQuotesFromBoolean = false)
        val xml = IOUtils.toString(
                this::class.java.getResourceAsStream("/test.xml"),
                "UTF-8"
        )
        val json = IOUtils.toString(
                this::class.java.getResourceAsStream("/test_omitted_booleans.json"),
                "UTF-8"
        )
        val result = converter.convert(xml)!!.replace("\r\n", "\n")
        assertEquals(json,result)
    }

Output

{
  "test" : {
    "inside" : {
      "someString" : "string",
      "someNumber" : 123,
      "someBoolean" : "true",
      "someOtherNumber" : "123.45"
    },
    "someArray" : {
      "arrayItem" : [ 1, 2, 3 ]
    },
    "complexArray" : {
      "complexItem" : [ {
        "name" : "test1",
        "value" : 1,
        "enabled" : "true"
      }, {
        "name" : "test2",
        "value" : 2,
        "enabled" : false
      }, {
        "name" : "test3",
        "value" : 3,
        "enabled" : "true"
      } ]
    }
  }
}

Case - omit quotes from booleans only

Code

@Test
    fun testXmltoJsonConversion_omit_numbers_false() {
        val converter = XmlToJsonConverter(omitQuotesFromNumbers = false)
        val xml = IOUtils.toString(
                this::class.java.getResourceAsStream("/test.xml"),
                "UTF-8"
        )
        val json = IOUtils.toString(
                this::class.java.getResourceAsStream("/test_omitted_numbers.json"),
                "UTF-8"
        )
        val result = converter.convert(xml)!!.replace("\r\n", "\n")
        assertEquals(json,result)
    }

Output

{
  "test" : {
    "inside" : {
      "someString" : "string",
      "someNumber" : "123",
      "someBoolean" : true,
      "someOtherNumber" : "123.45"
    },
    "someArray" : {
      "arrayItem" : [ "1", "2", "3" ]
    },
    "complexArray" : {
      "complexItem" : [ {
        "name" : "test1",
        "value" : "1",
        "enabled" : true
      }, {
        "name" : "test2",
        "value" : "2",
        "enabled" : false
      }, {
        "name" : "test3",
        "value" : "3",
        "enabled" : true
      } ]
    }
  }
}

About

Kotlin XML to JSON converter

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages