title: How do you set an ObjectCodec on a JsonParser when not going through an ObjectMapper?
body: <p>Note: this is using Jackson 2.3.2</p>
  
  <p>For the needs of one of my projects, I am writing a custom <code>JsonParser</code> which records a <code>Map</code> where keys are <code>JsonPointer</code>s and values are <code>Integer</code>s (line numbers where the pointer points to).</p>
  
  <p>The class is named <code>LineRecorderJsonParser</code>. It is produced by a <code>LineRecorderJsonFactory</code> which is a simple delegate:</p>
  
  <pre><code>public final class LineRecorderJsonFactory
      extends JsonFactory
  {
      @Override
      protected JsonParser _createParser(final InputStream in,
          final IOContext ctxt)
          throws IOException, JsonParseException
      {
          final JsonParser parser = super._createParser(in, ctxt);
          return new LineRecorderJsonParser(parser);
      }
  
      @Override
      protected JsonParser _createParser(final Reader r, final IOContext ctxt)
          throws IOException, JsonParseException
      {
          final JsonParser parser = super._createParser(r, ctxt);
          return new LineRecorderJsonParser(parser);
      }
  
      @Override
      protected JsonParser _createParser(final byte[] data, final int offset,
          final int len, final IOContext ctxt)
          throws IOException, JsonParseException
      {
          final JsonParser parser = super._createParser(data, offset, len, ctxt);
          return new LineRecorderJsonParser(parser);
      }
  }
  </code></pre>
  
  <p>I have tested that the code works using the following code:</p>
  
  <pre><code>public static void main(final String... args)
      throws IOException
  {
      final JsonFactory factory = new LineRecorderJsonFactory();
      final ObjectMapper mapper = new ObjectMapper(factory);
      final Closer closer = Closer.create();
      final InputStream in;
  
      try {
          in = closer.register(LineTesting.class.getResourceAsStream
              ("/testfile.json"));
          mapper.readTree(in);
      } finally {
          closer.close();
      }
  }
  </code></pre>
  
  <p>But now I want to test it; so I wrote the following test code:</p>
  
  <pre><code>public final class LineRecorderJsonParserTest
  {
      private static final String INCORRECT_LINE_INFO
          = "generated line info is incorrect; expected: %s, actual: %s";
  
      private JsonFactory factory;
      private ObjectMapper mapper;
  
      @BeforeMethod
      public void initFactory()
      {
          factory = new LineRecorderJsonFactory();
          mapper = new ObjectMapper();
      }
  
      @DataProvider
      public Iterator&lt;Object[]&gt; getLineData()
      {
          final List&lt;Object[]&gt; list = Lists.newArrayList();
  
          list.add(new Object[] { "1" });
          return list.iterator();
      }
  
      @Test(dataProvider = "getLineData")
      public void lineNumbersAreCorrectlyReported(final String subdir)
          throws IOException
      {
          final String basePath = "/parser/" + subdir + '/';
          final Closer closer = Closer.create();
          final TypeReference&lt;Map&lt;JsonPointer, Integer&gt;&gt; typeRef
              = new TypeReference&lt;Map&lt;JsonPointer, Integer&gt;&gt;() {};
  
          final InputStream input, lines;
          final Map&lt;JsonPointer, Integer&gt; actual, expected;
          final LineRecorderJsonParser parser;
  
          try {
              input = closer.register(inputFrom(basePath + "input.json"));
              lines = closer.register(inputFrom(basePath + "lines.json"));
  
              expected = mapper.readValue(lines, typeRef);
  
              parser = (LineRecorderJsonParser) factory.createParser(input);
              //parser.setCodec(factory.getCodec()); // Doesn't change anything...
              parser.readValueAsTree(); // FAILS HERE
              actual = parser.getLineInfo();
  
              assertEquals(actual, expected,
                  String.format(INCORRECT_LINE_INFO, expected, actual));
          } catch (IOException e) {
              throw closer.rethrow(e);
          } finally {
              closer.close();
          }
      }
  
      private static InputStream inputFrom(final String path)
      {
          return LineRecorderJsonParserTest.class.getResourceAsStream(path);
      }
  }
  </code></pre>
  
  <p>Unfortunately this doesn't work. I get the following exception:</p>
  
  <pre><code>java.lang.IllegalStateException: No ObjectCodec defined for the parser, can not deserialize JSON into JsonNode tree
      at com.fasterxml.jackson.core.JsonParser.readValueAsTree(JsonParser.java:1507)
      at com.fasterxml.jackson.core.util.JsonParserDelegate.readValueAsTree(JsonParserDelegate.java:371)
      at com.github.fge.jackson.parse.LineRecorderJsonParserTest.lineNumbersAreCorrectlyReported(LineRecorderJsonParserTest.java:84)
  </code></pre>
  
  <p>Even when I (try and) set an <code>ObjectCodec</code> myself (see code), I get this exception...</p>
  
  <p>What am I doing wrong? How can I test this class, since when you read using an <code>ObjectMapper</code> directly, you don't get a chance to grab the underlying parser?</p>