Interface WriterContext
- All Known Implementing Classes:
JsonWriter
- Author:
- Kenny Partlow ([email protected])
Copyright (c) Cedar Software LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
License
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
-
Method Summary
Modifier and TypeMethodDescriptionProvide access to all objects that are referencedGets the write options for the current serializationvoidwriteArrayFieldStart(String name) Writes a complete JSON array field start with automatic comma handling.voidwriteBooleanField(String name, boolean value) Writes a complete JSON boolean field with automatic comma handling.voidWrites a JSON array closing bracket].voidWrites a JSON object closing brace "}".voidwriteFieldName(String name) Writes a JSON field name followed by a colon.voidWrite any object fully.voidwriteNumberField(String name, Number value) Writes a complete JSON number field with automatic comma handling.voidwriteObject(Object obj, boolean showType, boolean bodyOnly) Allows you to use the current JsonWriter to write an object out.voidwriteObjectField(String name, Object value) Writes a complete JSON object field with automatic serialization and comma handling.voidwriteObjectFieldStart(String name) Writes a complete JSON object field start with automatic comma handling.voidWrites a JSON array opening bracket[.voidWrites a JSON object opening brace "{".voidwriteStringField(String name, String value) Writes a complete JSON string field with automatic comma handling.voidwriteValue(Object value) Writes a JSON value by serializing the given object.voidwriteValue(String value) Writes a JSON string value with proper quote escaping.
-
Method Details
-
getWriteOptions
WriteOptions getWriteOptions()Gets the write options for the current serialization- Returns:
- WriteOptions
-
writeObject
Allows you to use the current JsonWriter to write an object out.- Throws:
IOException
-
writeImpl
Write any object fully.- Throws:
IOException
-
getObjsReferenced
Provide access to all objects that are referenced -
writeFieldName
Writes a JSON field name followed by a colon. This method handles quote escaping and proper JSON formatting automatically.Example:
writeFieldName("name")produces"name":Important: This method does NOT write a preceding comma. If you need a comma before the field (for non-first fields in an object), you must write it manually. Consider using the higher-level methods like
writeStringField(String, String)orwriteObjectField(String, Object)which handle commas automatically.- Parameters:
name- the field name to write (without quotes)- Throws:
IOException- if an I/O error occurs
-
writeStringField
Writes a complete JSON string field with automatic comma handling.Example:
writeStringField("name", "John")produces,"name":"John"This method automatically:
- Writes a preceding comma (for proper JSON object formatting)
- Escapes special characters in both field name and value
- Handles null values by writing
,"name":null
Usage in custom writers:
public void write(Object obj, boolean showType, Writer output, WriterContext context) { MyClass instance = (MyClass) obj; output.write('{'); context.writeStringField("firstName", instance.getFirstName()); context.writeStringField("lastName", instance.getLastName()); output.write('}'); }- Parameters:
name- the field namevalue- the string value (may be null)- Throws:
IOException- if an I/O error occurs
-
writeObjectField
Writes a complete JSON object field with automatic serialization and comma handling.Example:
writeObjectField("address", addressObj)produces,"address":{...}where the address object is fully serialized according to json-io's rules.This method automatically:
- Writes a preceding comma (for proper JSON object formatting)
- Serializes the value object with proper type information and reference tracking
- Handles null values by writing
,"name":null - Handles circular references and object deduplication
Usage in custom writers:
public void write(Object obj, boolean showType, Writer output, WriterContext context) { MyClass instance = (MyClass) obj; output.write('{'); context.writeObjectField("config", instance.getConfig()); context.writeObjectField("data", instance.getData()); output.write('}'); }- Parameters:
name- the field namevalue- the object to serialize (may be null)- Throws:
IOException- if an I/O error occurs
-
writeStartObject
Writes a JSON object opening brace "{".This should be paired with
writeEndObject()to properly close the object.Usage pattern:
context.writeStartObject(); context.writeStringField("name", "value"); context.writeEndObject();- Throws:
IOException- if an I/O error occurs- See Also:
-
writeEndObject
Writes a JSON object closing brace "}".This should be paired with a preceding
writeStartObject()call.- Throws:
IOException- if an I/O error occurs- See Also:
-
writeStartArray
Writes a JSON array opening bracket[.This should be paired with
writeEndArray()to properly close the array.Usage pattern:
context.writeStartArray(); for (Item item : items) { context.writeStartObject(); context.writeObjectField("data", item); context.writeEndObject(); } context.writeEndArray();- Throws:
IOException- if an I/O error occurs- See Also:
-
writeEndArray
Writes a JSON array closing bracket].This should be paired with a preceding
writeStartArray()call.- Throws:
IOException- if an I/O error occurs- See Also:
-
writeValue
Writes a JSON string value with proper quote escaping.Example:
writeValue("Hello")produces"Hello"This method:
- Writes opening quote
- Escapes special characters (quotes, backslashes, control characters)
- Writes closing quote
- Handles null by writing
null(without quotes)
Usage pattern:
context.writeFieldName("name"); context.writeValue("John Doe"); // Produces: "name":"John Doe"- Parameters:
value- the string value to write (may be null)- Throws:
IOException- if an I/O error occurs
-
writeValue
Writes a JSON value by serializing the given object.Example:
writeValue(myObject)produces the full JSON representation of myObjectThis method:
- Serializes the object with proper type information and reference tracking
- Handles null by writing
null - Handles primitives, strings, collections, maps, and custom objects
- Handles circular references and object deduplication
Usage pattern:
context.writeFieldName("config"); context.writeValue(configObject); // Produces: "config":{...serialized config...}- Parameters:
value- the object to serialize (may be null)- Throws:
IOException- if an I/O error occurs
-
writeArrayFieldStart
Writes a complete JSON array field start with automatic comma handling.Example:
writeArrayFieldStart("items")produces,"items":[This is a convenience method that combines three operations:
- Writes a leading comma (for proper JSON object formatting)
- Writes the field name with quotes and colon
- Writes the array opening bracket
Usage pattern:
context.writeArrayFieldStart("entries"); for (Entry entry : entries) { // Write array elements... } context.writeEndArray(); // Produces: ,"entries":[...array elements...]This method writes a LEADING comma, making it suitable for fields after the first field.
- Parameters:
name- the field name- Throws:
IOException- if an I/O error occurs
-
writeObjectFieldStart
Writes a complete JSON object field start with automatic comma handling.Example:
writeObjectFieldStart("config")produces,"config":{This is a convenience method that combines three operations:
- Writes a leading comma (for proper JSON object formatting)
- Writes the field name with quotes and colon
- Writes the object opening brace
Usage pattern:
context.writeObjectFieldStart("metadata"); context.writeStringField("version", "1.0"); context.writeNumberField("count", 42); context.writeEndObject(); // Produces: ,"metadata":{"version":"1.0","count":42}This method writes a LEADING comma, making it suitable for fields after the first field.
- Parameters:
name- the field name- Throws:
IOException- if an I/O error occurs
-
writeNumberField
Writes a complete JSON number field with automatic comma handling.Example:
writeNumberField("count", 42)produces,"count":42This method automatically:
- Writes a preceding comma (for proper JSON object formatting)
- Escapes the field name
- Writes the number value WITHOUT quotes (proper JSON number format)
- Handles null values by writing
,"name":null
Usage pattern:
context.writeNumberField("capacity", 16); context.writeNumberField("loadFactor", 0.75f); context.writeNumberField("size", 100L); // Produces: ,"capacity":16,"loadFactor":0.75,"size":100This method writes a LEADING comma, making it suitable for fields after the first field.
- Parameters:
name- the field namevalue- the number value (may be null)- Throws:
IOException- if an I/O error occurs
-
writeBooleanField
Writes a complete JSON boolean field with automatic comma handling.Example:
writeBooleanField("active", true)produces,"active":trueThis method automatically:
- Writes a preceding comma (for proper JSON object formatting)
- Escapes the field name
- Writes the boolean value WITHOUT quotes (proper JSON boolean format)
Usage pattern:
context.writeBooleanField("caseSensitive", true); context.writeBooleanField("enabled", false); // Produces: ,"caseSensitive":true,"enabled":falseThis method writes a LEADING comma, making it suitable for fields after the first field.
- Parameters:
name- the field namevalue- the boolean value- Throws:
IOException- if an I/O error occurs
-