title: How can I include raw JSON in an object using Jackson?
body: <p>I am trying to include raw JSON inside a Java object when the object is (de)serialized using Jackson. In order to test this functionality, I wrote the following test:</p>
  
  <pre><code>public static class Pojo {
      public String foo;
  
      @JsonRawValue
      public String bar;
  }
  
  @Test
  public void test() throws JsonGenerationException, JsonMappingException, IOException {
  
      String foo = "one";
      String bar = "{\"A\":false}";
  
      Pojo pojo = new Pojo();
      pojo.foo = foo;
      pojo.bar = bar;
  
      String json = "{\"foo\":\"" + foo + "\",\"bar\":" + bar + "}";
  
      ObjectMapper objectMapper = new ObjectMapper();
      String output = objectMapper.writeValueAsString(pojo);
      System.out.println(output);
      assertEquals(json, output);
  
      Pojo deserialized = objectMapper.readValue(output, Pojo.class);
      assertEquals(foo, deserialized.foo);
      assertEquals(bar, deserialized.bar);
  }
  </code></pre>
  
  <p>The code outputs the following line:</p>
  
  <pre><code>{"foo":"one","bar":{"A":false}}
  </code></pre>
  
  <p>The JSON is exactly how I want things to look. Unfortunately, the code fails with an exception when attempting to read the JSON back in to the object. Here is the exception:</p>
  
  <blockquote>
    <p>org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
         at [Source: java.io.StringReader@d70d7a; line: 1, column: 13] (through reference chain: com.tnal.prism.cobalt.gather.testing.Pojo["bar"])</p>
  </blockquote>
  
  <p>Why does Jackson function just fine in one direction but fail when going the other direction? It seems like it should be able to take its own output as input again. I know what I'm trying to do is unorthodox (the general advice is to create an inner object for <code>bar</code> that has a property named <code>A</code>), but I don't want to interact with this JSON at all. My code is acting as a pass-through for this code -- I want to take in this JSON and send it back out again without touching a thing, because when the JSON changes I don't want my code to need modifications.</p>
  
  <p>Thanks for the advice.</p>
  
  <p>EDIT: Made Pojo a static class, which was causing a different error.</p>