// !!! TODO: Verify it works as expected: copied from standard JSON parser impl
 @Override
 public String getCurrentName() throws IOException {
   JsonStreamContext ctxt = _filterContext();
   if (_currToken == JsonToken.START_OBJECT || _currToken == JsonToken.START_ARRAY) {
     JsonStreamContext parent = ctxt.getParent();
     return (parent == null) ? null : parent.getCurrentName();
   }
   return ctxt.getCurrentName();
 }
Ejemplo n.º 2
0
  @Override
  public void close() throws IOException {
    super.close();

    /* 05-Dec-2008, tatu: To add [JACKSON-27], need to close open
     *   scopes.
     */
    // First: let's see that we still have buffers...
    if (_outputBuffer != null && isEnabled(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT)) {
      while (true) {
        JsonStreamContext ctxt = getOutputContext();
        if (ctxt.inArray()) {
          writeEndArray();
        } else if (ctxt.inObject()) {
          writeEndObject();
        } else {
          break;
        }
      }
    }
    _flushBuffer();

    /* 25-Nov-2008, tatus: As per [JACKSON-16] we are not to call close()
     *   on the underlying Reader, unless we "own" it, or auto-closing
     *   feature is enabled.
     *   One downside: when using UTF8Writer, underlying buffer(s)
     *   may not be properly recycled if we don't close the writer.
     */
    if (_writer != null) {
      if (_ioContext.isResourceManaged() || isEnabled(JsonGenerator.Feature.AUTO_CLOSE_TARGET)) {
        _writer.close();
      } else if (isEnabled(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM)) {
        // If we can't close it, we should at least flush
        _writer.flush();
      }
    }
    // Internal buffer(s) generator has can now be released as well
    _releaseBuffers();
  }
  public void testEmptyArrayWrite() throws Exception {
    StringWriter sw = new StringWriter();
    JsonGenerator gen = new JsonFactory().createGenerator(sw);

    JsonStreamContext ctxt = gen.getOutputContext();
    assertTrue(ctxt.inRoot());
    assertFalse(ctxt.inArray());
    assertFalse(ctxt.inObject());
    assertEquals(0, ctxt.getEntryCount());
    assertEquals(0, ctxt.getCurrentIndex());

    gen.writeStartArray();

    ctxt = gen.getOutputContext();
    assertFalse(ctxt.inRoot());
    assertTrue(ctxt.inArray());
    assertFalse(ctxt.inObject());
    assertEquals(0, ctxt.getEntryCount());
    assertEquals(0, ctxt.getCurrentIndex());

    gen.writeEndArray();

    ctxt = gen.getOutputContext();
    assertTrue("Should be in root, was " + ctxt.getTypeDesc(), ctxt.inRoot());
    assertFalse(ctxt.inArray());
    assertFalse(ctxt.inObject());
    assertEquals(1, ctxt.getEntryCount());
    // Index won't yet move
    assertEquals(0, ctxt.getCurrentIndex());

    gen.close();
    String docStr = sw.toString();
    JsonParser jp = createParserUsingReader(docStr);
    assertEquals(JsonToken.START_ARRAY, jp.nextToken());
    assertEquals(JsonToken.END_ARRAY, jp.nextToken());
    jp.close();

    // Ok, then array with nested empty array
    sw = new StringWriter();
    gen = new JsonFactory().createGenerator(sw);
    gen.writeStartArray();
    gen.writeStartArray();
    gen.writeEndArray();
    gen.writeEndArray();
    gen.close();
    docStr = sw.toString();
    jp = createParserUsingReader(docStr);
    assertEquals(JsonToken.START_ARRAY, jp.nextToken());
    assertEquals(JsonToken.START_ARRAY, jp.nextToken());
    assertEquals(JsonToken.END_ARRAY, jp.nextToken());
    assertEquals(JsonToken.END_ARRAY, jp.nextToken());
    assertEquals(null, jp.nextToken());
    jp.close();
  }