-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename StructuredDataPipe to StructuredDataBuffer, implement ValueEle…
…mentReader
- Loading branch information
Showing
17 changed files
with
223 additions
and
1,022 deletions.
There are no files selected for viewing
43 changes: 0 additions & 43 deletions
43
src/main/java/blue/endless/jankson/api/DeserializerFunction.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
src/main/java/blue/endless/jankson/api/io/ValueElementReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
src/main/java/blue/endless/jankson/api/serializer/ObjectReaderFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.