Package org.loxlylabs.nestedtext
Class NestedText
java.lang.Object
org.loxlylabs.nestedtext.NestedText
The main class for loading and dumping NestedText data. This class provides methods
to parse NestedText from a String or File into Java objects (typically a
Map<String, Object>,
List<Object>, or String) and to serialize Java objects back into a NestedText formatted string.
This implementation supports the Minimal NestedText specification.
Usage Example:
// Create a new NestedText instance
NestedText nt = new NestedText();
// Loading NestedText
String data = """
name: John Doe
age: 42
children:
- Jane
- Bill
""";
Map<String, Object> person = (Map<String, Object>) nt.load(data);
System.out.println(person.get("name")); // Prints "John Doe"
// Serializing to NestedText
Map<String, Object> address = Map.of("city", "Anytown", "zip", "12345");
String nestedText = nt.dump(address);
System.out.println(nestedText);
// Prints:
// city: Anytown
// zip: 12345
Instances of this class are configurable and can be reused.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceA functional interface for providing a custom serialization strategy for a specific class. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionDumps (serializes) a Java object into a NestedText formatted string.indent(int numSpaces) Sets the number of spaces to use for each level of indentation when dumping data.lineSeparator(String lineSeparator) Sets the line separator string to use when dumping data.load(byte[] data) Loads (parses) NestedText content from a byte array.Loads (parses) NestedText content from a file.Loads (parses) NestedText content from a string.Loads (parses) NestedText content from a path.<T> NestedTextregisterAdapter(Class<T> type, NestedText.Adapter<T> adapter) Registers a custom adapter for serializing a specific class to a NestedText-compatible format.useReflection(boolean useReflection) Enables or disables the use of reflection for serializing arbitrary Java objects during a dump operation.
-
Constructor Details
-
NestedText
public NestedText()Creates a new NestedText instance with default settings.- Indentation: 4 spaces
- Line Separator: System default
- Reflection for dumping: enabled
-
-
Method Details
-
registerAdapter
Registers a custom adapter for serializing a specific class to a NestedText-compatible format.- Type Parameters:
T- The type of the class.- Parameters:
type- The class type to register the adapter for.adapter- The adapter that converts an instance of the class to aString,Map, orList.- Returns:
- This
NestedTextinstance for fluent configuration.
-
indent
Sets the number of spaces to use for each level of indentation when dumping data. The default is 4.- Parameters:
numSpaces- The number of spaces for indentation (must be positive).- Returns:
- This
NestedTextinstance for fluent configuration.
-
lineSeparator
Sets the line separator string to use when dumping data. The default is the system's line separator.- Parameters:
lineSeparator- The string to use for line breaks (e.g., "\n" or "\r\n").- Returns:
- This
NestedTextinstance for fluent configuration.
-
useReflection
Enables or disables the use of reflection for serializing arbitrary Java objects during a dump operation. When enabled (default), the library will attempt to serialize records and POJOs by inspecting their fields and components. If disabled, aNestedTextExceptionwill be thrown for unsupported types.- Parameters:
useReflection-trueto enable reflection (default),falseto disable.- Returns:
- This
NestedTextinstance for fluent configuration.
-
load
Loads (parses) NestedText content from a file.- Parameters:
file- The file to read from.- Returns:
- A
Map<String, Object>,List<Object>,String, ornullif the file is empty. - Throws:
NestedTextException- if the file content is not valid NestedText.IOException- if an I/O error occurs while reading the file.
-
load
Loads (parses) NestedText content from a path.- Parameters:
path- The path to the file to read from.- Returns:
- A
Map<String, Object>,List<Object>,String, ornullif the file is empty. - Throws:
NestedTextException- if the file content is not valid NestedText.IOException- if an I/O error occurs while reading the file.
-
load
Loads (parses) NestedText content from a byte array.This method decodes the byte array as UTF-8 with strict error handling. Malformed byte sequences will result in an exception.
- Parameters:
data- The byte array containing NestedText data, encoded in UTF-8.- Returns:
- A
Map<String, Object>,List<Object>,String, ornullif the content is empty. - Throws:
NestedTextException- if the byte array is not valid UTF-8 or if the decoded string is not valid NestedText.
-
load
Loads (parses) NestedText content from a string.- Parameters:
contents- The string containing NestedText data.- Returns:
- A
Map<String, Object>,List<Object>,String, ornullif the string is empty or contains only whitespace/comments. - Throws:
NestedTextException- if the string is not valid NestedText.
-
dump
Dumps (serializes) a Java object into a NestedText formatted string.The method supports common Java types by default:
Map(keys are converted to strings)Collectionand arrays (become lists)String,Number,Boolean,Character,Enum(become strings)- Java Records and POJOs (become dictionaries, requires reflection)
- Parameters:
obj- The object to serialize.- Returns:
- A string containing the NestedText representation of the object.
- Throws:
NestedTextException- if an unsupported object type is encountered or a reflection error occurs.
-