Java JsonGeneratorDelegate-class And Method Code Example

Here is an example of how to use the JsonGeneratorDelegate class from the package in Java:

import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerator;


public class Main {
    public static void main(String[] args) throws Exception {
        JsonFactory jsonFactory = new JsonFactory();
        StringWriter writer = new StringWriter();
        JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(writer);
        JsonGeneratorDelegate delegate = new JsonGeneratorDelegate(jsonGenerator);

        delegate.writeStringField("name", "John");
        delegate.writeNumberField("age", 30);


In this example, we create a JsonFactory, JsonGenerator and JsonGeneratorDelegate objects. We use the JsonGeneratorDelegate object to write a JSON object with two fields, "name" and "age". We then close the JsonGeneratorDelegate and print the resulting JSON string.

Please note that this class has been removed from library, it was present in version 2.6, But as of latest version it has been removed, you can use other libraries like Jackson library's JsonGenerator to achieve similar functionality.