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();
  }
  public static String dumpConfigurationSectionAsFlatJson(String section) {
    Class en = CONFIGURATION_SECTIONS.get(section);
    try {
      JsonFactory jfactory = new JsonFactory();
      StringWriter sw = new StringWriter();
      String enumDescription = "";
      JsonGenerator gen = jfactory.createJsonGenerator(sw);
      gen.writeStartArray();
      EnumSet values = EnumSet.allOf(en);
      for (Object v : values) {
        String key = (String) (en.getMethod("getKey")).invoke(v);

        boolean isVisible = (Boolean) (en.getMethod("isVisible")).invoke(v);
        String valueAsString;
        if (isVisible) valueAsString = (String) (en.getMethod("getValueAsString")).invoke(v);
        else valueAsString = "--HIDDEN--";
        boolean isEditable = (Boolean) (en.getMethod("isEditable")).invoke(v);
        boolean isOverridden = (Boolean) (en.getMethod("isOverridden")).invoke(v);
        String valueDescription = (String) (en.getMethod("getValueDescription")).invoke(v);
        Class type = (Class) en.getMethod("getType").invoke(v);

        gen.writeStartObject(); // 					{
        gen.writeStringField("key", key);
        gen.writeStringField("value", valueAsString);
        gen.writeStringField("description", valueDescription); // 						,"description":"description"
        gen.writeStringField("type", type.getSimpleName()); // 						,"type":"type"
        gen.writeBooleanField("editable", isEditable);
        gen.writeBooleanField("overridden", isOverridden);
        gen.writeEndObject(); // 					}
      }
      if (gen.getOutputContext().inArray()) gen.writeEndArray(); // 				]
      gen.close();
      return sw.toString();
    } catch (Exception e) {
      Logger.error(
          "Cannot generate a json for "
              + en.getSimpleName()
              + " Enum. Is it an Enum that implements the IProperties interface?",
          e);
    }
    return "{}";
  } // dumpConfigurationSectionAsJson(String)()
  /**
   * *
   *
   * <p>Returns a json representation of the Enumerator The Enumerator must implements the
   * IProperties interface
   *
   * @param en the Enumerator to serialize. It must implements the IProperties interface
   * @return the representation of the Enumerator
   */
  @SuppressWarnings("unchecked")
  public static String dumpConfigurationAsJson(String section) {
    Class en = CONFIGURATION_SECTIONS.get(section);
    try {
      JsonFactory jfactory = new JsonFactory();
      StringWriter sw = new StringWriter();
      String enumDescription = "";
      JsonGenerator gen = jfactory.createJsonGenerator(sw);

      Method getEnumDescription = en.getMethod("getEnumDescription");
      if (getEnumDescription != null
          && getEnumDescription.getReturnType() == String.class
          && Modifier.isStatic(getEnumDescription.getModifiers()))
        enumDescription = (String) getEnumDescription.invoke(null);
      gen.writeStartObject(); // {
      gen.writeStringField("section", section); // 	 "configuration":"EnumName"
      gen.writeStringField("description", enumDescription); // 	,"description": "EnumDescription"
      gen.writeFieldName("sub sections"); //  ,"sections":
      gen.writeStartObject(); // 		{
      String lastSection = "";
      EnumSet values = EnumSet.allOf(en);
      for (Object v : values) {
        String key = (String) (en.getMethod("getKey")).invoke(v);
        boolean isVisible = (Boolean) (en.getMethod("isVisible")).invoke(v);
        String valueAsString;
        if (isVisible) valueAsString = (String) (en.getMethod("getValueAsString")).invoke(v);
        else valueAsString = "--HIDDEN--";
        boolean isEditable = (Boolean) (en.getMethod("isEditable")).invoke(v);
        String valueDescription = (String) (en.getMethod("getValueDescription")).invoke(v);
        Class type = (Class) en.getMethod("getType").invoke(v);
        String subsection = key.substring(0, key.indexOf('.'));
        if (!lastSection.equals(subsection)) {
          if (gen.getOutputContext().inArray()) gen.writeEndArray();
          gen.writeFieldName(subsection); // 			"sectionName":
          gen.writeStartArray(); // 				[
          lastSection = subsection;
        }
        boolean isOverridden = (Boolean) (en.getMethod("isOverridden")).invoke(v);
        gen.writeStartObject(); // 					{
        gen.writeStringField(key, valueAsString); // 							"key": "value"
        gen.writeStringField("description", valueDescription); // 						,"description":"description"
        gen.writeStringField("type", type.getSimpleName()); // 						,"type":"type"
        gen.writeBooleanField("editable", isEditable); // 						,"editable":"true|false"
        gen.writeBooleanField("visible", isVisible); // 						,"visible":"true|false"
        gen.writeBooleanField("overridden", isOverridden); // 						,"overridden":"true|false"
        gen.writeEndObject(); // 					}
      }
      if (gen.getOutputContext().inArray()) gen.writeEndArray(); // 				]
      gen.writeEndObject(); // 		}
      gen.writeEndObject(); // }
      gen.close();
      return sw.toString();
    } catch (Exception e) {
      Logger.error(
          "Cannot generate a json for "
              + en.getSimpleName()
              + " Enum. Is it an Enum that implements the IProperties interface?",
          e);
    }
    return "{}";
  } // dumpConfigurationAsJson(en)