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":

      { }}

      ===

        People

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

          Dates

          • Created:
            Updated:
            Resolved: