-
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.
- Loading branch information
Showing
6 changed files
with
305 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
src/main/java/blue/endless/jankson/api/codec/ClassTargetCodec.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,40 @@ | ||
/* | ||
* 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.codec; | ||
|
||
import java.lang.reflect.Type; | ||
|
||
import blue.endless.jankson.impl.magic.ClassHierarchy; | ||
|
||
public abstract interface ClassTargetCodec extends StructuredDataCodec { | ||
|
||
public abstract Class<?> getTargetClass(); | ||
|
||
@Override | ||
public default boolean appliesTo(Type t) { | ||
Class<?> clazz = ClassHierarchy.getErasedClass(t); | ||
return getTargetClass().isAssignableFrom(clazz); | ||
} | ||
} |
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
60 changes: 60 additions & 0 deletions
60
src/main/java/blue/endless/jankson/api/codec/FullCodec.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,60 @@ | ||
/* | ||
* 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.codec; | ||
|
||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
|
||
import blue.endless.jankson.api.io.StructuredDataReader; | ||
import blue.endless.jankson.impl.io.objectwriter.SingleValueFunction; | ||
|
||
public class FullCodec implements ClassTargetCodec { | ||
private final Class<?> targetClass; | ||
private final Function<Object, StructuredDataReader> serializer; | ||
private final Supplier<SingleValueFunction<Object>> deserializer; | ||
|
||
@SuppressWarnings("unchecked") | ||
public <T> FullCodec(Class<T> targetClass, Function<T, StructuredDataReader> serializer, Supplier<SingleValueFunction<T>> deserializer) { | ||
this.targetClass = targetClass; | ||
this.serializer = (Function<Object, StructuredDataReader>) serializer; | ||
this.deserializer = () -> (SingleValueFunction<Object>) deserializer.get(); | ||
} | ||
|
||
@Override | ||
public StructuredDataReader getReader(Object o) { | ||
return serializer.apply(o); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public <T> SingleValueFunction<T> getWriter() { | ||
return (SingleValueFunction<T>) deserializer.get(); | ||
} | ||
|
||
@Override | ||
public Class<?> getTargetClass() { | ||
return targetClass; | ||
} | ||
} |
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
111 changes: 111 additions & 0 deletions
111
src/main/java/blue/endless/jankson/api/codec/ValueElementCodec.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,111 @@ | ||
/* | ||
* 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.codec; | ||
|
||
import java.io.IOException; | ||
import java.util.function.Function; | ||
|
||
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.api.function.CheckedFunction; | ||
import blue.endless.jankson.api.io.StructuredDataReader; | ||
import blue.endless.jankson.api.io.ValueElementReader; | ||
import blue.endless.jankson.impl.io.objectwriter.SingleValueFunction; | ||
|
||
/** | ||
* Simple codec that converts an object to and from at-rest json data. This is similar to Jankson | ||
* 1.2-era serializers and deserializers. | ||
*/ | ||
public class ValueElementCodec implements ClassTargetCodec { | ||
private final Class<?> targetClass; | ||
private final Function<Object, ValueElement> serializer; | ||
private final CheckedFunction<ValueElement, Object, IOException> deserializer; | ||
|
||
@SuppressWarnings("unchecked") | ||
public <T> ValueElementCodec(Class<T> targetClass, Function<T, ValueElement> serializer, CheckedFunction<ValueElement, T, IOException> deserializer) { | ||
this.targetClass = targetClass; | ||
this.serializer = (Function<Object, ValueElement>) serializer; | ||
this.deserializer = (CheckedFunction<ValueElement, Object, IOException>) deserializer; | ||
} | ||
|
||
@Override | ||
public Class<?> getTargetClass() { | ||
return targetClass; | ||
} | ||
|
||
@Override | ||
public StructuredDataReader getReader(Object o) { | ||
return ValueElementReader.of(serializer.apply(o)); | ||
} | ||
|
||
@Override | ||
public <T> SingleValueFunction<T> getWriter() { | ||
return null; //TODO: Implement | ||
} | ||
|
||
public <T> ValueElementCodec requiringObjects(Class<T> targetClass, Function<T, ObjectElement> serializer, Function<ObjectElement, T> deserializer) { | ||
Function<T, ValueElement> shimmedSerializer = serializer::apply; | ||
|
||
CheckedFunction<ValueElement, T, IOException> shimmedDeserializer = (elem) -> { | ||
if (elem instanceof ObjectElement object) { | ||
return deserializer.apply(object); | ||
} else { | ||
throw new IOException("Required ObjectElement but found "+elem.getClass().getSimpleName()); | ||
} | ||
}; | ||
|
||
return new ValueElementCodec(targetClass, shimmedSerializer, shimmedDeserializer); | ||
} | ||
|
||
public <T> ValueElementCodec requiringArrays(Class<T> targetClass, Function<T, ArrayElement> serializer, Function<ArrayElement, T> deserializer) { | ||
Function<T, ValueElement> shimmedSerializer = serializer::apply; | ||
|
||
CheckedFunction<ValueElement, T, IOException> shimmedDeserializer = (elem) -> { | ||
if (elem instanceof ArrayElement array) { | ||
return deserializer.apply(array); | ||
} else { | ||
throw new IOException("Required ArrayElement but found "+elem.getClass().getSimpleName()); | ||
} | ||
}; | ||
|
||
return new ValueElementCodec(targetClass, shimmedSerializer, shimmedDeserializer); | ||
} | ||
|
||
public <T> ValueElementCodec requiringPrimitives(Class<T> targetClass, Function<T, PrimitiveElement> serializer, Function<PrimitiveElement, T> deserializer) { | ||
Function<T, ValueElement> shimmedSerializer = serializer::apply; | ||
|
||
CheckedFunction<ValueElement, T, IOException> shimmedDeserializer = (elem) -> { | ||
if (elem instanceof PrimitiveElement primitive) { | ||
return deserializer.apply(primitive); | ||
} else { | ||
throw new IOException("Required PrimitiveElement but found "+elem.getClass().getSimpleName()); | ||
} | ||
}; | ||
|
||
return new ValueElementCodec(targetClass, shimmedSerializer, shimmedDeserializer); | ||
} | ||
} |