Uploaded image for project: 'WildFly'
  1. WildFly
  2. WFLY-20556

Json binding : fields after polymorphic field are null

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Major Major
    • 34.0.0.Final
    • 29.0.1.Final
    • EE
    • None
    • Hide

      To deserialize the text, one can use a servlet :
       

      @SuppressWarnings("serial")
      @WebServlet("/test")
      public class HelloWorldServlet3 extends HttpServlet {
       
      @Override
      protectedvoid doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      String s = "{\n"
      + " \"@type\": \"DataRequest\",\n"
      + " \"description\": \"Fetch yesterday prices\",\n"
      + " \"formatting\": {\n"
      + " \"@type\": \"MediaType\",\n"
      + " \"outputMediaType\": \"application/json\"\n"
      + " },\n"
      + " \"identifier\": \"uNrreFadSZPc\"\n"
      + "}";
      Request request = JsonbBuilder.create().fromJson(s, Request.class);
      resp.getWriter().append("identifier is: " + String.valueOf(request.getIdentifier()));
      }
      

       

       

      {color:#000000}}

      Show
      To deserialize the text, one can use a servlet :   @SuppressWarnings( "serial" ) @WebServlet( "/test" ) public class HelloWorldServlet3 extends HttpServlet {   @Override protectedvoid doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String s = "{\n" + " \" @type\ ": \" DataRequest\ ",\n" + " \" description\ ": \" Fetch yesterday prices\ ",\n" + " \" formatting\ ": {\n" + " \" @type\ ": \" MediaType\ ",\n" + " \" outputMediaType\ ": \" application/json\ "\n" + " },\n" + " \" identifier\ ": \" uNrreFadSZPc\ "\n" + "}" ; Request request = JsonbBuilder.create().fromJson(s, Request.class); resp.getWriter().append( "identifier is: " + String .valueOf(request.getIdentifier())); }     {color:#000000}}
    • Low
    • ---
    • ---

      Deserialization of this text leaves the field "identifier" null :

      String s = "{\n"
      + " \"@type\": \"DataRequest\",\n"
      + " \"description\": \"Fetch yesterday prices\",\n"
      + " \"formatting\": {\n"
      + " \"@type\": \"MediaType\",\n"
      + " \"outputMediaType\": \"application/json\"\n"
      + " },\n"
      + " \"identifier\": \"uNrreFadSZPc\"\n"
      + "}";
      Request request = JsonbBuilder.create().fromJson(s, Request.class);
      

      But if I change the order and put "formatting" at the end, it works : identifier is not null.
       

      String s = "{\n"
      + " \"@type\": \"DataRequest\",\n"
      + " \"description\": \"Fetch yesterday prices\",\n"
      + " \"identifier\": \"uNrreFadSZPc\",\n"
      + " \"formatting\": {\n"
      + " \"@type\": \"MediaType\",\n"
      + " \"outputMediaType\": \"application/json\"\n"
      + " }\n"
      + "}";
      

       

      Here are my classes :

      @JsonbTypeInfo({
        @JsonbSubtype(alias = "DataRequest", type = RequestData.class)
      })
      public class Request {
      	@JsonbProperty("description")
      	private String description;
      	@JsonbProperty("identifier")
      	private String identifier;
      	@JsonbProperty("formatting")
      	private Formatting formatting;
      //setter, getter
      }
      
      public class RequestData extends Request {
      }
      
      @JsonbTypeInfo({
        @JsonbSubtype(alias = "MediaType", type = FormattingMediaType.class)
      })
      public class Formatting {
      }
      public class FormattingMediaType extends Formatting {
      	@JsonbProperty("outputMediaType")
      	private String outputMediaType;
      //public getter/setter
      }
      

       

              jperkins-rhn James Perkins
              grigouille Frédéric Jean-Prost (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated:
                Resolved: