Skip to content

Commit

Permalink
Capture missed changes
Browse files Browse the repository at this point in the history
  • Loading branch information
falkreon committed Jul 26, 2024
1 parent 3894afb commit b9fd634
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 507 deletions.
16 changes: 10 additions & 6 deletions src/main/java/blue/endless/jankson/api/Jankson.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@
import blue.endless.jankson.api.io.JsonReaderOptions;
import blue.endless.jankson.api.io.JsonWriter;
import blue.endless.jankson.api.io.JsonWriterOptions;
import blue.endless.jankson.api.io.ObjectReaderFactory;
import blue.endless.jankson.api.io.ObjectWriter;
import blue.endless.jankson.api.io.StructuredDataReader;
import blue.endless.jankson.api.io.ValueElementWriter;
import blue.endless.jankson.impl.io.objectreader.ObjectStructuredDataReader;


public class Jankson {
Expand Down Expand Up @@ -229,7 +229,7 @@ public static <T> T readJson(Reader r, JsonReaderOptions opts, Type type) throws
* @throws SyntaxError if there was a problem with the json data, or if there was a problem creating the object
*
* @see #readJsonObject(Reader, JsonReaderOptions)
* @see #writeJson(Object, Writer, JsonWriterOptions)
* @see #writeJson(Object, Writer)
*/
public static <T> T readJson(Reader r, JsonReaderOptions opts, Class<T> clazz) throws IOException, SyntaxError {
JsonReader reader = new JsonReader(r);
Expand All @@ -238,9 +238,13 @@ public static <T> T readJson(Reader r, JsonReaderOptions opts, Class<T> clazz) t
return writer.toObject();
}

public static void writeJson(Object obj, Writer writer, JsonWriterOptions options) throws IOException {
public static void writeJson(Object obj, Writer writer) throws IOException {
writeJson(obj, new ObjectReaderFactory(), writer, JsonWriterOptions.STRICT);
}

public static void writeJson(Object obj, ObjectReaderFactory factory, Writer writer, JsonWriterOptions options) throws IOException {
try {
StructuredDataReader r = ObjectStructuredDataReader.of(obj);
StructuredDataReader r = factory.getReader(obj);
JsonWriter w = new JsonWriter(writer, options);
r.transferTo(w);
writer.flush();
Expand All @@ -250,9 +254,9 @@ public static void writeJson(Object obj, Writer writer, JsonWriterOptions option
}
}

public static String writeJsonString(Object obj, JsonWriterOptions options) throws IOException {
public static String writeJsonString(Object obj, ObjectReaderFactory factory, JsonWriterOptions options) throws IOException {
try(StringWriter sw = new StringWriter()) {
StructuredDataReader r = ObjectStructuredDataReader.of(obj);
StructuredDataReader r = factory.getReader(obj);
JsonWriter w = new JsonWriter(sw, options);
r.transferTo(w);
sw.flush();
Expand Down
108 changes: 0 additions & 108 deletions src/main/java/blue/endless/jankson/api/io/ElementType.java

This file was deleted.

51 changes: 42 additions & 9 deletions src/main/java/blue/endless/jankson/api/io/ObjectReaderFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* SOFTWARE.
*/

package blue.endless.jankson.api.serializer;
package blue.endless.jankson.api.io;

import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Type;
Expand All @@ -31,10 +31,13 @@
import java.util.function.Function;

import blue.endless.jankson.api.document.ValueElement;
import blue.endless.jankson.api.io.StructuredDataReader;
import blue.endless.jankson.api.io.ValueElementReader;
import blue.endless.jankson.impl.io.objectreader.ObjectStructuredDataReader;

/**
* This class manages reading arbitrary Java objects as StructuredData. Because not all classes can
* be recognized or annotated from within, factories may be registered to produce appropriate
* readers externally.
*/
public class ObjectReaderFactory {
private Map<Type, Function<Object, StructuredDataReader>> functionMap = new HashMap<>();

Expand All @@ -46,8 +49,8 @@ public class ObjectReaderFactory {
* ValueElement representing it.
*/
@SuppressWarnings("unchecked")
public <T> void register(final Class<T> type, final Function<T, ValueElement> function) {
register((Type) type, (Function<Object, ValueElement>) function);
public <T> void registerSerializer(final Class<T> type, final Function<T, ValueElement> function) {
registerSerializer((Type) type, (Function<Object, ValueElement>) function);
}

/**
Expand All @@ -56,15 +59,35 @@ public <T> void register(final Class<T> type, final Function<T, ValueElement> fu
* @param function A function which will receive an object of the specified type, and produce a
* ValueElement representing it.
*/
public void register(final Type type, final Function<Object, ValueElement> function) {
public void registerSerializer(final Type type, final Function<Object, ValueElement> function) {
Function<Object, StructuredDataReader> supplier = (Object obj) -> ValueElementReader.of(function.apply(obj));
functionMap.put(type, supplier);
}

/**
* Registers a reader factory function for the specified type
* @param <T> The type the factory will apply to
* @param type The class the factory will apply to
* @param function A reader factory function which will receive an object of the specified type,
* and return a StructuredDataReader representation of it.
*/
@SuppressWarnings("unchecked")
public <T> void register(final Class<T> type, final Function<T, StructuredDataReader> function) {
register((Type) type, (Function<Object, StructuredDataReader>) function);
}

/**
* Registers a reader factory function for the specified type
* @param type The type the factory will apply to
* @param function A reader factory function which will receive an object of the specified type,
* and return a StructuredDataReader representation of it.
*/
public void register(final Type type, final Function<Object, StructuredDataReader> function) {
functionMap.put(type, function);
}

/**
* Gets a reader which will provide a StructuredData representation of the provided object
* Gets a reader which will provide a StructuredData representation of the provided Object
* @param <T> The type of the object being serialized / read
* @param type The class of the object being serialized / read
* @param objectOfType The object being serialized / read
Expand All @@ -75,7 +98,7 @@ public <T> StructuredDataReader getReader(final Class<T> type, final T objectOfT
}

/**
* Gets a reader which will provide a StructuredData representation of the provided object
* Gets a reader which will provide a StructuredData representation of the provided Object
* @param type The type of the object being serialized / read
* @param objectOfType The object being serialized / read
* @return A StructuredDataReader which will provide data representing the object
Expand All @@ -88,7 +111,17 @@ public StructuredDataReader getReader(Type type, final Object objectOfType) {

Function<Object, StructuredDataReader> function = functionMap.get(type);
return (function == null) ?
ObjectStructuredDataReader.of(objectOfType) :
ObjectStructuredDataReader.of(objectOfType, this) :
function.apply(objectOfType);
}

/**
* Quick form of {@link #getReader(Type, Object)}. Gets a reader which will provide a
* StructuredData representation of the provided Object.
* @param object The object being serialized / read
* @return A StructuredDataReader which will provide data representing the object
*/
public StructuredDataReader getReader(Object object) {
return getReader(object.getClass(), object);
}
}

This file was deleted.

Loading

0 comments on commit b9fd634

Please sign in to comment.