XStream
  1. XStream
  2. XSTR-371

JSON writer generates malformed output when inner content has null fields

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.2.2
    • Component/s: None
    • Labels:
      None
    • JDK version and platform:
      JDK 1.5

      Description

      If you are serializing objects that contain objects and the inner object has no non-null fields or primitive types, the close bracket is not emitted.

      This test case
      ===
      public void testSimpleTypesCanBeRepresentedAsJsonWithNullMembers() {

      String expected = (
      "{'innerMessage': {\n" +
      " 'greeting': 'hello',\n" +
      " 'innerMessage':

      {\n" + " }

      \n" +
      "}}").replace('\'', '"');

      XStream xs = new XStream(new JsonHierarchicalStreamDriver());

      xs.alias("innerMessage", Msg.class);

      Msg message = new Msg("hello");

      Msg message2 = new Msg(null);

      message.innerMessage = message2;

      String xs_sgtr = xs.toXML(message);
      assertEquals(expected, xs_sgtr);
      }

      public static class Msg
      {
      String greeting;
      Msg innerMessage;
      public Msg(String greeting)

      { this.greeting = greeting; }

      }
      ===
      Should produce
      ===
      {"innerMessage": {
      "greeting": "hello",
      "innerMessage": {
      }
      }}
      ===
      But instead produces
      ===
      {"innerMessage": {
      "greeting": "hello",
      "innerMessage":

      { }}

      ===

        Activity

        Hide
        Jörg Schaible added a comment -

        Hi Matthiew,

        I've committed a fix for this. If it works for you, close this issue ...

        Cheers,
        Jörg

        Show
        Jörg Schaible added a comment - Hi Matthiew, I've committed a fix for this. If it works for you, close this issue ... Cheers, Jörg
        Hide
        J. Matthew Pryor added a comment -

        Sorry for the delayed response, yes this works nicely

        Show
        J. Matthew Pryor added a comment - Sorry for the delayed response, yes this works nicely

          People

          • Assignee:
            Unassigned
            Reporter:
            J. Matthew Pryor
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: