Mined and Summarized Use Cases For API: xstream

  1. JSON string from Gson: remove double quotes

    You can use JAXB for this. Here a code sample found on XStream implementation website Xstream-website I hope it help you. .. details

    1. package com.thoughtworks.xstream.json.test
    2. import com.thoughtworks.xstream.XStream
    3. import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver
    4. public class ReadTest {
    5. public static void main(String[] args) {
    6. String json = "{\"product\":{\"name\":\"Banana\",\"id\":123" + ",\"price\":23.0}}"
    7. XStream xstream = new XStream(new JettisonMappedXmlDriver())
    8. xstream.alias("product", Product.class)
    9. Product product = (Product) xstream.fromXML(json)
    10. System.out.println(product.getName())
    11. }
    12. }

    • See Also (2)
    1. Convert XML to JSON format

      None details

      Reactions - Positive 0, Negative 0, Others 0

      1. XStream xstream = new XStream(new JettisonMappedXmlDriver())
      2. String json = xstream.toXML(person)

    2. Convert XML to JSON format

      None details

      Reactions - Positive 0, Negative 0, Others 0

      1. String xml = "<person>...</person>"
      2. XStream xstream = new XStream()
      3. Person person = (Person)xstream.fromXML(xml)

  2. Using XStream and JsonHierarchicalStreamDriver to output values, how do I round doubles?

    None .. details

    1. xstream.registerConverter(new HackRoundedDoubleConverter())

  3. Xstream List - Duplicate field

    like. . .. details

    1. xstream.addImplicitCollection(SimplePerson.class, "cars")

    Negative Reactions
    1. FYI, I had the exact same problem and this solution worked for me.
    Other Reactions
    1. edit: One more update, for Xstream 1.2.2 I had to do this: xstream.addImplicitCollection(SimplePerson.class, "cars", String.class);.

  1. system
      Chart will be rendered here
    1. JSON string from Gson: remove double quotes

      You can use JAXB for this. Here a code sample found on XStream implementation website Xstream-website I hope it help you. details

      Reactions - Positive 0, Negative 0, Others 0

      1. package com.thoughtworks.xstream.json.test
      2. import com.thoughtworks.xstream.XStream
      3. import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver
      4. public class ReadTest {
      5. public static void main(String[] args) {
      6. String json = "{\"product\":{\"name\":\"Banana\",\"id\":123" + ",\"price\":23.0}}"
      7. XStream xstream = new XStream(new JettisonMappedXmlDriver())
      8. xstream.alias("product", Product.class)
      9. Product product = (Product) xstream.fromXML(json)
      10. System.out.println(product.getName())
      11. }
      12. }

  2. xstream
      Chart will be rendered here
    1. Convert XML to JSON format

      None details

      Reactions - Positive 0, Negative 0, Others 0

      1. XStream xstream = new XStream(new JettisonMappedXmlDriver())
      2. String json = xstream.toXML(person)

    2. Convert XML to JSON format

      None details

      Reactions - Positive 0, Negative 0, Others 0

      1. String xml = "<person>...</person>"
      2. XStream xstream = new XStream()
      3. Person person = (Person)xstream.fromXML(xml)

    3. JSON string from Gson: remove double quotes

      You can use JAXB for this. Here a code sample found on XStream implementation website Xstream-website I hope it help you. details

      Reactions - Positive 0, Negative 0, Others 0

      1. package com.thoughtworks.xstream.json.test
      2. import com.thoughtworks.xstream.XStream
      3. import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver
      4. public class ReadTest {
      5. public static void main(String[] args) {
      6. String json = "{\"product\":{\"name\":\"Banana\",\"id\":123" + ",\"price\":23.0}}"
      7. XStream xstream = new XStream(new JettisonMappedXmlDriver())
      8. xstream.alias("product", Product.class)
      9. Product product = (Product) xstream.fromXML(json)
      10. System.out.println(product.getName())
      11. }
      12. }

All usage scenarios for API xstream
  • JSON string from Gson: remove double quotes

    You can use JAXB for this. Here a code sample found on XStream implementation website Xstream-website I hope it help you. details

    Reactions - Positive 0, Negative 0, Others 0

    1. package com.thoughtworks.xstream.json.test
    2. import com.thoughtworks.xstream.XStream
    3. import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver
    4. public class ReadTest {
    5. public static void main(String[] args) {
    6. String json = "{\"product\":{\"name\":\"Banana\",\"id\":123" + ",\"price\":23.0}}"
    7. XStream xstream = new XStream(new JettisonMappedXmlDriver())
    8. xstream.alias("product", Product.class)
    9. Product product = (Product) xstream.fromXML(json)
    10. System.out.println(product.getName())
    11. }
    12. }

  • Using XStream and JsonHierarchicalStreamDriver to output values, how do I round doubles?

    None details

    Reactions - Positive 0, Negative 0, Others 0

    1. xstream.registerConverter(new HackRoundedDoubleConverter())

  • Xstream List - Duplicate field

    like. . details

    Reactions - Positive 0, Negative 1, Others 0

    1. xstream.addImplicitCollection(SimplePerson.class, "cars")

    Negative Reactions
    1. FYI, I had the exact same problem and this solution worked for me.
    Other Reactions
    1. edit: One more update, for Xstream 1.2.2 I had to do this: xstream.addImplicitCollection(SimplePerson.class, "cars", String.class);.

  • Convert XML to JSON format

    None details

    Reactions - Positive 0, Negative 0, Others 0

    1. XStream xstream = new XStream(new JettisonMappedXmlDriver())
    2. String json = xstream.toXML(person)

  • Convert XML to JSON format

    None details

    Reactions - Positive 0, Negative 0, Others 0

    1. String xml = "<person>...</person>"
    2. XStream xstream = new XStream()
    3. Person person = (Person)xstream.fromXML(xml)