Skip to content

Commit

Permalink
doc: add Java doc to XjxSerdes
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-grgt committed Jan 7, 2024
1 parent ecc41bd commit dc570fc
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions xjx-serdes/src/main/java/io/jonasg/xjx/serdes/XjxSerdes.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import java.util.HashMap;
import java.util.Map;

/**
* XjxSerdes provides functionality for serializing and deserializing objects to and from XML.
*/
public class XjxSerdes {

private final SaxParser saxParser;
Expand All @@ -23,20 +26,56 @@ private XjxSerdes(SaxParser saxParser, PathWriterIndexFactory pathWriterIndexFac
this.pathWriterIndexFactory = pathWriterIndexFactory;
}

/**
* Constructs an XjxSerdes instance with default configurations.
*/
public XjxSerdes() {
this(new SaxParser(), new PathWriterIndexFactory());
}

/**
* Reads XML data and deserializes it into an object of the specified class.
*
* @param data The XML data to read.
* @param clazz The class type to deserialize the XML data into.
* @param <T> The generic type of the class.
* @return The deserialized object.
*/
public <T> T read(String data, Class<T> clazz) {
return read(new StringReader(data), clazz);
}

/**
* Reads XML data from a reader and deserializes it into an object of the specified class.
*
* @param data The reader containing XML data to read.
* @param clazz The class type to deserialize the XML data into.
* @param <T> The generic type of the class.
* @return The deserialized object.
*/
public <T> T read(Reader data, Class<T> clazz) {
PathBasedSaxHandler<T> saxHandler = new PathBasedSaxHandler<>((rootTag) -> pathWriterIndexFactory.createIndexForType(clazz, rootTag));
saxParser.parse(data, saxHandler);
return saxHandler.instance();
}


/**
* Reads XML data and deserializes it into a map with specified key and value types.
*
* @param data The XML data to read.
* @param mapOf The MapOf instance specifying key and value types.
* @param <K> The type of map keys (only supports String).
* @param <V> The type of map values.
* @return The deserialized map.
* @throws XjxDeserializationException If the map key type is not supported (only supports String).
* <p>
* Example usage:
* <pre>{@code
* MapOf<String, Object> mapOf = new MapOf<String, Object>() {};
* Map<String, Object> deserializedMap = new XjxSerdes().read("<xml>...</xml>", mapOf);
* }</pre>
*/
public <K, V> Map<K, V> read(String data, MapOf<K, V> mapOf) {
return read(new StringReader(data), mapOf);
}
Expand Down

0 comments on commit dc570fc

Please sign in to comment.