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 and bind to Java classes.
It also provides methods to serialize Java objects back into a Minimal
NestedText formatted string.
This implementation supports the Minimal NestedText specification.
Usage Example:
record Person(String fullName, int age) {}
// Deserializing from NestedText
String content = """
name: Alice Smith
age: 5
""";
// Use the builder to have field level control over
// serialization/deserialization
NestedText nt = NestedText.builder()
.forType(Person.class, type -> {
// Map "fullName" field in our record to "name"
type.renameField("fullName").to("name");
}).build();
Person p = nt.from(content).as(Person.class);
System.out.println(p.fullName());
// Serializing to NestedText
String nestedText = nt.dump(p);
System.out.println(nestedText);
// Prints:
// name: Alice Smith
// age: 5
Instances of this class are configurable and can be reused.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classA builder class to assist in creating NestedText instances.final classAn intermediate object used to specify the target type for the parsed NestedText data.static classHelper class to complete the renameField operation.static classProvides a fluent API for configuring type-specific mapping rules. -
Method Summary
Modifier and TypeMethodDescriptionstatic NestedText.Builderbuilder()Create a new NestedText BuilderDumps (serializes) a Java object into a NestedText formatted string.dump(Object obj, DumpOptions options) Dumps (serializes) a Java object into a NestedText formatted string.from(byte[] data) Loads (parses) NestedText content from a byte array.Loads (parses) NestedText content from a string.Loads (parses) NestedText content from a path.
-
Method Details
-
builder
Create a new NestedText Builder- Returns:
- a new NestedText Builder
-
from
Loads (parses) NestedText content from a path.- Parameters:
path- The path to the file to read from.- Returns:
- a Reader which can convert the NestedText to Java types.
- Throws:
NestedTextException- if the file content is not valid NestedText.IOException- if an I/O error occurs while reading the file.
-
from
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 Reader which can convert the NestedText to Java types.
- Throws:
NestedTextException- if the byte array is not valid UTF-8 or if the decoded string is not valid NestedText.
-
from
Loads (parses) NestedText content from a string.- Parameters:
contents- The string containing NestedText data.- Returns:
- a Reader which can convert the NestedText to Java types.
- 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.
-
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.options- The serialization options, such as indent, eol char, etc.- Returns:
- A string containing the NestedText representation of the object.
- Throws:
NestedTextException- if an unsupported object type is encountered or a reflection error occurs.
-