Skip to content

Commit

Permalink
Rename StructuredDataPipe to StructuredDataBuffer, implement ValueEle…
Browse files Browse the repository at this point in the history
…mentReader
  • Loading branch information
falkreon committed Jul 24, 2024
1 parent e3fe13f commit c055eb0
Show file tree
Hide file tree
Showing 17 changed files with 223 additions and 1,022 deletions.
43 changes: 0 additions & 43 deletions src/main/java/blue/endless/jankson/api/DeserializerFunction.java

This file was deleted.

14 changes: 0 additions & 14 deletions src/main/java/blue/endless/jankson/api/io/JsonReaderOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@

import java.util.EnumSet;

import blue.endless.jankson.api.Marshaller;
import blue.endless.jankson.impl.MarshallerImpl;

@SuppressWarnings("deprecation")
public class JsonReaderOptions {
/**
* This is the set of options configured when there are no options specified. Effectively this is the "default
Expand All @@ -38,25 +34,15 @@ public class JsonReaderOptions {
public static final JsonReaderOptions UNSPECIFIED = new JsonReaderOptions(Hint.ALLOW_UNQUOTED_KEYS);

private final EnumSet<Hint> hints = EnumSet.noneOf(Hint.class);
private final Marshaller marshaller;

public JsonReaderOptions(Hint... hints) {
this.marshaller = MarshallerImpl.getFallback();
}

public JsonReaderOptions(Marshaller marshaller, Hint... hints) {
for(Hint hint : hints) this.hints.add(hint);
this.marshaller = marshaller;
}

public boolean hasHint(Hint hint) {
return hints.contains(hint);
}

public Marshaller getMarshaller() {
return this.marshaller;
}

public enum Hint {
/** Allow the root object of a document to omit its delimiters / braces */
ALLOW_BARE_ROOT_OBJECT,
Expand Down
10 changes: 2 additions & 8 deletions src/main/java/blue/endless/jankson/api/io/JsonWriterOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@

import java.util.EnumSet;

import blue.endless.jankson.api.Marshaller;
import blue.endless.jankson.impl.MarshallerImpl;

@SuppressWarnings("deprecation")
public class JsonWriterOptions {
public static JsonWriterOptions DEFAULTS = new JsonWriterOptions(Hint.UNQUOTED_KEYS, Hint.WRITE_COMMENTS, Hint.WRITE_NEWLINES, Hint.WRITE_WHITESPACE);
public static JsonWriterOptions ONE_LINE = new JsonWriterOptions(Hint.UNQUOTED_KEYS, Hint.WRITE_COMMENTS, Hint.WRITE_WHITESPACE);
Expand All @@ -38,15 +34,13 @@ public class JsonWriterOptions {

private final EnumSet<Hint> hints = EnumSet.noneOf(Hint.class);
private final String indentString;
private final Marshaller marshaller;

public JsonWriterOptions(Hint... hints) {
this("\t", MarshallerImpl.getFallback(), hints);
this("\t", hints);
}

public JsonWriterOptions(String indentString, Marshaller marshaller, Hint... hints) {
public JsonWriterOptions(String indentString, Hint... hints) {
for(Hint hint : hints) this.hints.add(hint);
this.marshaller = marshaller;
this.indentString = indentString;
}

Expand Down
93 changes: 93 additions & 0 deletions src/main/java/blue/endless/jankson/api/io/ValueElementReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* MIT License
*
* Copyright (c) 2018-2024 Falkreon (Isaac Ellingson)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package blue.endless.jankson.api.io;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

import blue.endless.jankson.api.document.ArrayElement;
import blue.endless.jankson.api.document.ObjectElement;
import blue.endless.jankson.api.document.PrimitiveElement;
import blue.endless.jankson.api.document.ValueElement;
import blue.endless.jankson.impl.io.StructuredDataBuffer;
import blue.endless.jankson.impl.io.objectreader.DelegatingStructuredDataReader;

public class ValueElementReader {

private ValueElementReader() {}

public static StructuredDataReader of(ValueElement val) {
if (val instanceof PrimitiveElement primitive) {
StructuredDataBuffer buf = new StructuredDataBuffer();
buf.write(StructuredData.primitive(primitive));
return buf;
} else if (val instanceof ArrayElement array) {
return new ArrayValueReader(array);
} else if (val instanceof ObjectElement object) {
return new ObjectValueReader(object);
} else {
throw new IllegalArgumentException("Unknown element type");
}
}

private static class ArrayValueReader extends DelegatingStructuredDataReader {
private final Iterator<ValueElement> iterator;

public ArrayValueReader(ArrayElement value) {
this.iterator = value.iterator();
}

@Override
protected void onDelegateEmpty() throws IOException {
if (iterator.hasNext()) {
ValueElement cur = iterator.next();
setDelegate(of(cur));
} else {
buffer(StructuredData.EOF);
}
}
}

private static class ObjectValueReader extends DelegatingStructuredDataReader {
private final Iterator<Map.Entry<String, ValueElement>> iterator;

public ObjectValueReader(ObjectElement value) {
iterator = value.entrySet().iterator();
}

@Override
protected void onDelegateEmpty() throws IOException {
if (iterator.hasNext()) {
Map.Entry<String, ValueElement> entry = iterator.next();
buffer(StructuredData.objectKey(entry.getKey()));
setDelegate(ValueElementReader.of(entry.getValue()));
} else {
buffer(StructuredData.EOF);
}
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* MIT License
*
* Copyright (c) 2018-2024 Falkreon (Isaac Ellingson)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package blue.endless.jankson.api.serializer;

import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
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;

public class ObjectReaderFactory {
private Map<Type, Function<Object, StructuredDataReader>> functionMap = new HashMap<>();

/**
* Registers a "classic" serializer for the specified type.
* @param <T> The type the serializer will apply to
* @param type The class the serializer will apply to
* @param function A function that will receive an object of the specified type, and produce a
* 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);
}

/**
* Registers a "classic" serializer for the specified type
* @param type The type to specify a serializer for
* @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) {
Function<Object, StructuredDataReader> supplier = (Object obj) -> ValueElementReader.of(function.apply(obj));
functionMap.put(type, supplier);
}



/**
* 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
* @return A StructuredDataReader which will provide data representing the object
*/
public <T> StructuredDataReader getReader(final Class<T> type, final T objectOfType) {
return getReader((Type) type, objectOfType);
}

/**
* 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
*/
public StructuredDataReader getReader(Type type, final Object objectOfType) {
// Strip annotations - we don't want to differentiate between String and @Nullable String.
if (type instanceof AnnotatedType annoType) {
type = annoType.getType();
}

Function<Object, StructuredDataReader> function = functionMap.get(type);
return (function == null) ?
ObjectStructuredDataReader.of(objectOfType) :
function.apply(objectOfType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,4 @@
* SOFTWARE.
*/

package blue.endless.jankson.impl.serializer;

import blue.endless.jankson.api.Marshaller;
import blue.endless.jankson.api.io.JsonIOException;

@FunctionalInterface
public interface InternalDeserializerFunction<B> {
public B deserialize(Object a, Marshaller m) throws JsonIOException;
}
package blue.endless.jankson.api.serializer;
Loading

0 comments on commit c055eb0

Please sign in to comment.