jersey
  1. jersey
  2. JERSEY-380

Add application property to set the default JSON convention

    Details

    • Type: Improvement Improvement
    • Status: Resolved
    • Priority: Critical Critical
    • Resolution: Complete
    • Affects Version/s: 1.0.3
    • Fix Version/s: None
    • Component/s: core
    • Labels:
      None
    • Environment:

      Operating System: All
      Platform: All

    • Issuezilla Id:
      380

      Description

      When I attempt to serialize an array of one element as JSON, the serialization
      is failing to surround my element with backets[]. This is causing problems
      downstream since the expected result is a list.

      A very simple example of this:

      /**
      This example produces a result of

      {"data":"aaa"}

      and it should have produced

      {"data":["aaa"]}

      */
      import javax.ws.rs.GET;
      import javax.ws.rs.Path;
      import javax.ws.rs.Produces;
      import javax.ws.rs.core.MediaType;
      import javax.xml.bind.annotation.XmlElement;
      import javax.xml.bind.annotation.XmlRootElement;

      @Path("test.ui")
      public class Test {

      @GET
      @Produces(MediaType.APPLICATION_JSON)
      public TestData getSessionState() {

      TestData rc = new TestData();
      rc.data = new String[]

      { "aaa" }

      ;
      return rc;
      }

      @XmlRootElement(name="TestDAta")
      public static class TestData

      { @XmlElement public String[] data; }

      }

        Activity

        Hide
        ido_ran added a comment -

        I also run into this same issue.
        I've change my serialization format to XML for now since I didn't find any result for it.

        Show
        ido_ran added a comment - I also run into this same issue. I've change my serialization format to XML for now since I didn't find any result for it.
        Hide
        japod added a comment -

        You can configure Jersey so that your single element array remains an array,
        please see

        https://jersey.dev.java.net/nonav/documentation/1.0.3/user-guide.html#d0e1137

        for details.

        You can either stick with the default mapped JSON notation, and explicitly
        tell it, what arrays you use, or completely rely on the natural notation,
        which should detect arrays automatically.

        Does it solve your issue?

        Show
        japod added a comment - You can configure Jersey so that your single element array remains an array, please see https://jersey.dev.java.net/nonav/documentation/1.0.3/user-guide.html#d0e1137 for details. You can either stick with the default mapped JSON notation, and explicitly tell it, what arrays you use, or completely rely on the natural notation, which should detect arrays automatically. Does it solve your issue?
        Hide
        drampulla added a comment -

        It seems strange that the default behavior is not "natural". Perhaps this
        should be listed as an enhancement since it is very annoying to have to create a
        new provider which returns a JAXBJSONContext for each message returned from my
        actions. This means every action I perform needs to have a provider returning a
        customized context. This generates a lot of excess code.

        I do however want to thank you for your help on this. You really made my day.

        Show
        drampulla added a comment - It seems strange that the default behavior is not "natural". Perhaps this should be listed as an enhancement since it is very annoying to have to create a new provider which returns a JAXBJSONContext for each message returned from my actions. This means every action I perform needs to have a provider returning a customized context. This generates a lot of excess code. I do however want to thank you for your help on this. You really made my day.
        Hide
        sandoz added a comment -

        The default is not natural because it would break backwards compatibility.

        What we require is a property that could be declared in the web.xml or the
        ResourceConfig to set the convention.

        I have updated the title and changed the type to an enhancement.

        Show
        sandoz added a comment - The default is not natural because it would break backwards compatibility. What we require is a property that could be declared in the web.xml or the ResourceConfig to set the convention. I have updated the title and changed the type to an enhancement.
        Hide
        Marek Potociar added a comment -

        Jersey 1.x branch is not under active development anymore. Only security issues and issues reported by the customers of commercially supported products that ship with Jersey are fixed on this code base right now.
        Please, move your code to Jersey 2.x and if you still see the issue there, feel free to file a bug against Jersey 2.x.

        Show
        Marek Potociar added a comment - Jersey 1.x branch is not under active development anymore. Only security issues and issues reported by the customers of commercially supported products that ship with Jersey are fixed on this code base right now. Please, move your code to Jersey 2.x and if you still see the issue there, feel free to file a bug against Jersey 2.x.

          People

          • Assignee:
            Unassigned
            Reporter:
            drampulla
          • Votes:
            2 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: