The ColorJSON
class is designed to transform various data types into a chat-friendly, colored JSON representation. It offers a range of customization options for the formatting and styling of JSON output. This documentation outlines how to use the ColorJSON
class, including its properties and methods.
- Color customization for different JSON elements.
- Inline threshold settings for compact JSON representation.
- Maximum depth control for object traversal.
- Class name inclusion in output.
- Ability to override methods for custom formatting.
- Cycle detection and handling.
import ColorJSON from '@bedrock-oss/bedrock-boost';
const jsonFormatter = new ColorJSON();// or use default instance `ColorJSON.DEFAULT`
To convert a value to a colored JSON string:
const result = jsonFormatter.stringify(yourValue);
Customize JSON tokens like braces, brackets, comma, etc.:
jsonFormatter.OpenObject = '{';
jsonFormatter.CloseObject = '}';
// ...similarly for other tokens
Set the inline threshold to control how short arrays and objects representation must be to be displayed inline:
jsonFormatter.InlineThreshold = 60;
Set the maximum depth to control how deep the object traversal must go:
jsonFormatter.MaxDepth = 5; // or 0 for unlimited
Toggle inclusion of class names:
jsonFormatter.IncludeClassNames = true; // or false
Customize colors for various elements:
jsonFormatter.OpenCloseObjectColor = ChatColor.YELLOW;
jsonFormatter.StringColor = ChatColor.DARK_GREEN;
// ...similarly for other color properties
The ColorJSON
class contains several methods intended for overriding to customize the output. These methods handle the stringification of different data types and structural elements in JSON.
stringifyString(value: string): string
stringifyNumber(value: number): string
stringifyBoolean(value: boolean): string
stringifyFunction(value: function): string
stringifyNull(): string
stringifyUndefined(): string
stringifyCycle(): string
stringifyArray(value: any[]): string
stringifyObject(value: object, className: string, entries: any[][], indentLevel: number): string
To override these methods, extend the ColorJSON
class and redefine the methods as per your requirements.
class CustomColorJSON extends ColorJSON {
// Override methods here
stringifyString(value) {
// Custom implementation
}
// ... other overrides
}
class MyColorJSON extends ColorJSON {
stringifyString(value) {
return `${this.StringColor}["${value}"]${ChatColor.RESET}`;
}
stringifyNumber(value) {
return `${this.NumberColor}{${value}}${ChatColor.RESET}`;
}
// ... other method overrides
}
const jsonFormatter = new MyColorJSON();
const result = jsonFormatter.stringify(yourValue);
The class automatically detects cycles within objects and arrays to prevent infinite loops.
const data = { name: "Alice", age: 30, active: true };
const coloredJson = jsonFormatter.stringify(data);
world.sendMessage(coloredJson);
jsonFormatter.StringColor = ChatColor.BLUE;
jsonFormatter.BooleanColor = ChatColor.RED;
const customColoredJson = jsonFormatter.stringify(data);
world.sendMessage(customColoredJson);