public static void main(String[] args) {

        try {

            JsonFactory jfactory = new JsonFactory();

            /*** write to file ***/
            JsonGenerator jGenerator = jfactory.createJsonGenerator(new File(
                    "D:\\Java SE\\src\\json\\jackson_streaming_api_to_read_and_write_json\\file.json"), JsonEncoding.UTF8);

            jGenerator.writeStartObject(); // {

            jGenerator.writeStringField("name", "mkyong"); // "name" : "mkyong"

            jGenerator.writeNumberField("age", 29); // "age" : 29

            jGenerator.writeFieldName("messages"); // "messages" :

            jGenerator.writeStartArray(); // [

            jGenerator.writeString("msg 1"); // "msg 1"

            jGenerator.writeString("msg 2"); // "msg 2"

            jGenerator.writeString("msg 3"); // "msg 3"

            jGenerator.writeEndArray(); // ]

            jGenerator.writeEndObject(); // }

            jGenerator.close();

        } catch (JsonGenerationException e) {

            e.printStackTrace();

        } catch (JsonMappingException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }