Javatpoint Logo
Javatpoint Logo

JsonGenerator Class in Jackson

All the public APIs which are used to write JSON data are defined by the JsonGenerator class. So, it is a base class for all the public APIs. We use factory methods of JsonFactory class for creating an instance of JsonGenerator.

The declaration of JsonGenerator class for com.fasterxml.jackson.core.JsonGenerator class is as follows:

Nested Class of JsonGenerator Class

JsonGenerator.Feature is an enumeration that defines all the togglable features for generators.

Syntax:

JsonGenerator Class Fields

protected PrettyPrinter _cfgPrettyPrinter

It is an object which is used for handling the pretty-printing at the time of output.

Constructors of the JsonGenerator Class

S.N. Constructor Description
1 protected JsonGenerator() Default constructor

It is an object which is used for handling the pretty-printing at the time of output.

Methods of the JsonGenerator Class

S.N. Method Description
1 protected void _reportError(String msg) It is a helper method that is used to construct and throw JsonGenerationException with the specified base message.
2 protected void _writeSimpleObject(Object value) It is used for calling appropriate write method to given untyped Object.
3 boolean canOmitFields() - Introspection method to call to check whether it is ok to omit writing of Object fields or not. The method is used for checking whether it is ok to omit the writing of Object fields or not.
4 boolean canUseSchema(FormatSchema schema) It is used to check whether the given schema can be used with the current generator or not.
5 boolean canWriteBinaryNatively() It is used to check whether the underlying data format supports "native" binary data or not.
6 boolean canWriteObjectId() It is used to check whether the underlying data format supports some kind of Object Ids natively or not.
7 boolean canWriteTypeId() It is used to check whether the underlying data format supports some kind of Type Ids natively or not.
8 abstract void close() - Method called to close this generator so that no more content can be written. It is used for closing the current generator to stop writing content.
9 JsonGenerator configure(JsonGenerator.Feature f, boolean state) It is used to disable or enable the given feature.
10 void copyCurrentEvent(JsonParser jp) In order to copy the data of the current event pointed by the given parser instance, we use this method.
11 void copyCurrentStructure(JsonParser jp) In order to copy the data of the current event and following events that it encloses pointed by the given parser instance, we use this method.
12 abstract JsonGenerator disable(JsonGenerator.Feature f) It is used to disable the given feature.
13 abstract JsonGenerator enable(JsonGenerator.Feature f) It is used to enable the given parser features.
14 abstract void flush() The method is used for:

1. flushing any buffered data to the underlying target.

2. Flushing the target itself as well.

15 CharacterEscapes getCharacterEscapes() It is used to access the custom escapes factory uses for JsonGenerators it creates.
16 abstract ObjectCodec getCodec() It is used to access the Object used to write Java Object as JSON content.
17 abstract int getFeatureMask() In order to get the state of all standard JsonGenerator.Features, we use this bulk access method.
18 int getHighestEscapedChar() It is an accessor method, i.e., used to test what is the highest unescaped character configured for this generator.
19 Object getOutputTarget() It is used to get access to Object, i.e., used as the target for the generated output.
20 PrettyPrinter getPrettyPrinter() It is an accessor, i.e., used to check whether the current generator has a configured PrettyPrinter.
21 FormatSchema getSchema() It is used to access the schema that the current parser uses if any.
22 abstract boolean isClosed() It is used to determine whether the current generator is closed or not.
23 abstract boolean isEnabled(JsonGenerator.Feature f) It is used to check whether the given feature is enabled or not.
24 JsonGenerator setCharacterEscapes(CharacterEscapes esc) It is used to define the custom escapes factory uses for JsonGenerators it creates.
25 abstract JsonGenerator setCodec(ObjectCodec oc) In order to set or reset the Object for using to write to use for writing Java objects as JsonContent, we use this method.
26 abstract JsonGenerator setFeatureMask(int mask)-Bulk set method for (re)setting states of all standard JsonGenerator.Features In order to reset the states of all standard JsonGenerator.Features, we use this bulk set method.
27 JsonGenerator setHighestNonEscapedChar(int charCode) In order to escape all character codes above the specified code point, we request the generator by using this method.
28 JsonGenerator setPrettyPrinter(PrettyPrinter pp) It is used to set a custom pretty printer, i.e., used for adding indentation to improve human readability.
29 JsonGenerator setRootValueSeparator(SerializableString sep) It allows overriding String used for separating root-level JSON values.
30 void setSchema(FormatSchema schema) It is used for defining the schema which will be used by the current generator.
31 abstract JsonGenerator useDefaultPrettyPrinter() The method is used to enable pretty-printing using the pretty default printer.
32 abstract Version version() It is an accessor, i.e., used to find out the version of the bundle provided by the current generator instance.
33 void writeArrayFieldStart(String fieldName) It is used to output a field entry having JSON Array value. It is also used to output the START_ARRAY marker.
34 abstract void writeBinary(Base64Variant b64variant, byte[] data, int offset, int len) It is used to output a given chunk of binary data as a complete String value, i.e., surrounded by double-quotes.
35 abstract int writeBinary(Base64Variant b64variant, InputStream data, int dataLength) It is similar to the writeBinary(Base64Variant,byte[],int,int) but it accept input through a stream. Stream allows us to write incremental without holding the whole input in memory.
36 void writeBinary(byte[] data) It similar to writeBinary(Base64Variant,byte[],int,int), but assumes default to using the Jackson default Base64 variant, i.e., Base64Variants.MIME_NO_LINEFEEDS.
37 void writeBinary(byte[] data, int offset, int len) It similar to writeBinary(Base64Variant,byte[],int,int), but default to using the Jackson default Base64 variant, i.e., Base64Variants.MIME_NO_LINEFEEDS.
38 int writeBinary(InputStream data, int dataLength) It is similar to the writeBinary(Base64Variant, InputStream, int) but assumes default to using the Jackson default Base64 variant, i.e., Base64Variants.MIME_NO_LINEFEEDS.
39 void writeBinaryField(String fieldName, byte[] data) It is used to output a field entry having specified data in the base64-encoded form.
40 abstract void writeBoolean(boolean state) It is used to output literal JSON Boolean.
41 void writeBooleanField(String fieldName, boolean value) It is used to output a field entry having a Boolean value.
42 abstract void writeEndArray() It is used to write:
1. Closing marker of a JSON Array value.
2. If pretty-printing is enabled, it can also write possible white space decoration.
43 abstract void writeEndObject() It is used to write:
3. Closing marker of a JSON Object value.
4. If pretty-printing is enabled, it can also write possible white space decoration.
44 abstract void writeFieldName(SerializableString name) It is the same as writeFieldName(String name), but it accepts serializable strings except for a normal string. It can perform better as some of the processing and can be reused for later calls.
45 abstract void writeFieldName(String name) It is used to write the field name. The method accepts field name as a string, i.e., surrounded by double-quotes. It can be decorated with white space if pretty-printing is enabled.
46 abstract void writeNull() It is used to output literal JSON null values.
47 void writeNullField(String fieldName) It is used to output a field having JSON literal value null.
48 abstract void writeNumber(BigDecimal dec) It is used to output the given BigDecimal value as a JSON number.
49 abstract void writeNumber(BigInteger v) It is used to output the given BigInteger value as a JSON number.
50 abstract void writeNumber(double d) It is used to output the given double value as a JSON number.
51 abstract void writeNumber(float f) It is used to output the given float value as a JSON number.
52 abstract void writeNumber(int v) It is used to output the given int value as a JSON number.
53 abstract void writeNumber(long v) It is used to output the given long value as a JSON number.
54 void writeNumber(short v) It is used to output the given short value as a JSON number.
55 abstract void writeNumber(String encodedValue) It is used to output the custom numeric type, which cannot be easily converted to standard Java number types.
56 void writeNumberField(String fieldName, BigDecimal value) It is another important method, i.e., used to output a field entry having a specified numeric value.
57 void writeNumberField(String fieldName, double value) It is another important method, i.e., used to output a field entry having a specified numeric value.
58 void writeNumberField(String fieldName, float value) It is another important method, i.e., used to output a field entry having a specified numeric value.
59 void writeNumberField(String fieldName, int value) It is another important method, i.e., used to output a field entry having a specified numeric value.
60 void writeNumberField(String fieldName, long value) It is another important method, i.e., used to output a field entry having a specified numeric value.
61 abstract void writeObject(Object pojo) - Method for writing given Java object (POJO) as Json. It is one of the most important methods, i.e., used to write the given Java Object as JSON.
62 void writeObjectField(String fieldName, Object pojo) It is used to output a field entry having data of a specific Object as its value.
63 void writeObjectFieldStart(String fieldName) It is a convenient method that is used to output a field entry and the START_OBJECT marker.
64 void writeObjectId(Object id) This method can be invoked to output the so-called native Object Id.
65 void writeObjectRef(Object id) - Method that can be called to output references to native Object Ids. This method is rarely used by the developer to output references to native Object Ids.
66 abstract void writeRaw(char c) It allows the generator to copy input text verbatim without any modifications. Modifications include no escaping and no separators.
67 abstract void writeRaw(char[] text, int offset, int len) It allows the generator to copy input text verbatim without any modifications. Modifications include no escaping and no separators.
68 void writeRaw(SerializableString raw) It allows the generator to copy input text verbatim without any modifications. Modifications include no escaping and no separators.
69 abstract void writeRaw(String text) It allows the generator to copy input text verbatim without any modifications. Modifications include no escaping and no separators.
70 abstract void writeRaw(String text, int offset, int len) - Method that will force generator to copy input text verbatim with no modifications (including that no escaping is done and no separators are added even if context [array, Object] would otherwise require such). It allows the generator to copy input text verbatim without any modifications. Modifications include no escaping and no separators.
71 abstract void writeRawUTF8String(byte[] text, int offset, int length) This method is similar to the writeString(String) method; instead of accepting string, it accepts UTF-8 encoded string without additional escaping.
72 abstract void writeRawValue(String text) It allows the generator to copy input text verbatim without any modifications but assumes it must constitute a single legal JSON value.
73 abstract void writeRawValue(String text, int offset, int len) It is used to write raw values.
74 abstract void writeStartArray() The method is used to write starting marker of a JSON array value.
75 abstract void writeStartObject() It is another important method that is used to write starting marker of a JSON Object value.
76 abstract void writeString(char[] text, int offset, int len) It is used to output a String value.
77 abstract void writeString(SerializableString text) It is similar to the writeString(String) method, but instead of accepting string, it accepts SerializableString.
78 abstract void writeString(String text) - Method for outputting a String value. This method is used to output a String value.
79 void writeStringField(String fieldName, String value It is a convenient method that is used to output field entries having a string value.
80 void writeTypeId(Object id) The writeTypeId() can be used to output the so-called native Type Id.
81 abstract void writeUTF8String(byte[] text, int offset, int length) It is similar to the writeString(String) method, but that takes input as a UTF-8 encoded String.

The given string has not been escaped using whatever escaping scheme data format requires.

82 abstract void writeTree(TreeNode rootNode) It is an important method that is used to write the given JSON tree by using this generator.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA