Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,11 @@ System.out.println(JToon.encode(data, new EncodeOptions(2, Delimiter.PIPE, true,

### `JToon.decodeToJson(String toon, DecodeOptions options): String`

Converts TOON-formatted strings back to Java objects or JSON.
### `JToon.decodeToMap(String toon): Map<String, Object>`

### `JToon.decodeToMap(String toon, DecodeOptions options): Map<String, Object>`

Converts TOON-formatted strings back to Java objects, JSON or directly into Map<String, Object>.

**Parameters:**

Expand All @@ -296,6 +300,8 @@ For `decode`: A Java object (`Map` for objects, `List` for arrays, primitives fo

For `decodeToJson`: A JSON string representation

For `decodeToMap`: A `Map` representing a Java object

**Example:**

```java
Expand All @@ -312,6 +318,9 @@ Object result = JToon.decode(toon);

// Decode directly to JSON string
String json = JToon.decodeToJson(toon);

// Decode directly to Map<String, Object> (no cast needed)
Map<String, Object> map = JToon.decodeToMap(toon);
```

#### Round-Trip Conversion
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/dev/toonformat/jtoon/JToon.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import dev.toonformat.jtoon.normalizer.JsonNormalizer;
import tools.jackson.databind.JsonNode;

import java.util.Map;

/**
* Main entry point for encoding and decoding TOON (Token-Oriented Object Notation) format.
* Provides static methods to convert between Java objects, JSON strings, and TOON format.
Expand Down Expand Up @@ -118,6 +120,57 @@ public static Object decode(String toon, DecodeOptions options) {
return ValueDecoder.decode(toon, options);
}

/**
* Decodes a TOON-formatted string into a {@code Map<String, Object>} using default options.
*
* <p>
* This method is a convenience wrapper around
* {@link #decode(String)} and expects the decoded result to
* represent a TOON object. If the decoded value is {@code null} or an invalid {@code String},
* an empty map is returned. No deep transformation is applied: the returned map
* directly reflects the structure produced by the decoder.
* </p>
*
* <p>
* The decoded value must be a {@code Map}; otherwise, a {@link ClassCastException}
* will occur due to the unchecked cast.
* </p>
*
* @param toon The TOON-formatted string to decode
* @return A {@code Map<String, Object>} representing the decoded TOON object, or an empty map if the
* input decodes to {@code null} or a {@code String}
* @throws IllegalArgumentException if strict mode is enabled and the input is invalid
*/
public static Map<String, Object> decodeToMap(String toon) {
return decodeToMap(toon, DecodeOptions.DEFAULT);
}

/**
* Decodes a TOON-formatted string into a {@code Map<String, Object>} using the provided options.
*
* <p>
* This method is a convenience wrapper around
* {@link #decode(String, DecodeOptions)} and expects the decoded result to
* represent a TOON object. If the decoded value is {@code null} or is not a
* {@code Map}, an empty map is returned. No deep transformation is applied:
* the returned map directly reflects the structure produced by the decoder.
* </p>
*
* <p>
* The decoded value must be a {@code Map}; otherwise, this method returns an
* empty map instead of attempting an invalid cast.
* </p>
*
* @param toon The TOON-formatted string to decode
* @param options Decoding options (indentation, delimiters, strict mode)
* @return A {@code Map<String, Object>} representing the decoded TOON object, or an empty map
* if the decoded value is {@code null} or not a {@code Map}
* @throws IllegalArgumentException if strict mode is enabled and the input is invalid
*/
public static Map<String, Object> decodeToMap(String toon, DecodeOptions options) {
return ValueDecoder.decodeToMap(toon, options);
}

/**
* Decodes a TOON-formatted string directly to a JSON string using default
* options.
Expand Down
Loading