diff --git a/src/main/java/blue/endless/jankson/api/Jankson.java b/src/main/java/blue/endless/jankson/api/Jankson.java index 5a77ee3..573f8f0 100644 --- a/src/main/java/blue/endless/jankson/api/Jankson.java +++ b/src/main/java/blue/endless/jankson/api/Jankson.java @@ -33,7 +33,6 @@ import java.io.Writer; import java.lang.reflect.Type; import java.nio.charset.StandardCharsets; -import java.util.function.Consumer; import blue.endless.jankson.api.document.ObjectElement; import blue.endless.jankson.api.document.ValueElement; @@ -43,7 +42,6 @@ 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.StructuredData; import blue.endless.jankson.api.io.StructuredDataReader; import blue.endless.jankson.api.io.ValueElementWriter; @@ -240,23 +238,18 @@ public static T readJson(Reader r, JsonReaderOptions opts, Class clazz) t return writer.toObject(); } - public static void writeJson(Object obj, Writer writer) throws IOException { + public static void writeJson(Object obj, Writer writer) throws SyntaxError, 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 = factory.getReader(obj); - JsonWriter w = new JsonWriter(writer, options); - r.transferTo(w); - writer.flush(); - } catch (Throwable t) { - // IOException, MarshallerException, SyntaxError -> just IOException - throw new IOException(t); - } + public static void writeJson(Object obj, ObjectReaderFactory factory, Writer writer, JsonWriterOptions options) throws SyntaxError, IOException { + StructuredDataReader r = factory.getReader(obj); + JsonWriter w = new JsonWriter(writer, options); + r.transferTo(w); + writer.flush(); } - public static String writeJsonString(Object obj, ObjectReaderFactory factory, JsonWriterOptions options) throws IOException { + public static String writeJsonString(Object obj, ObjectReaderFactory factory, JsonWriterOptions options) throws SyntaxError, IOException { try(StringWriter sw = new StringWriter()) { StructuredDataReader r = factory.getReader(obj); JsonWriter w = new JsonWriter(sw, options); @@ -266,12 +259,12 @@ public static String writeJsonString(Object obj, ObjectReaderFactory factory, Js } } - public static void writeJson(ValueElement elem, Writer writer) throws IOException { + public static void writeJson(ValueElement elem, Writer writer) throws SyntaxError, IOException { JsonWriter out = new JsonWriter(writer); elem.write(out); } - public static String toJsonString(ValueElement elem, JsonWriterOptions options) throws IOException { + public static String toJsonString(ValueElement elem, JsonWriterOptions options) throws SyntaxError, IOException { try(StringWriter sw = new StringWriter()) { JsonWriter out = new JsonWriter(sw, options); elem.write(out); diff --git a/src/main/java/blue/endless/jankson/api/codec/CodecHolder.java b/src/main/java/blue/endless/jankson/api/codec/CodecHolder.java new file mode 100644 index 0000000..8ce21b9 --- /dev/null +++ b/src/main/java/blue/endless/jankson/api/codec/CodecHolder.java @@ -0,0 +1,29 @@ +/* + * 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; + +public class CodecHolder implements CodecManager { + +} diff --git a/src/main/java/blue/endless/jankson/api/codec/CodecManager.java b/src/main/java/blue/endless/jankson/api/codec/CodecManager.java new file mode 100644 index 0000000..a396078 --- /dev/null +++ b/src/main/java/blue/endless/jankson/api/codec/CodecManager.java @@ -0,0 +1,29 @@ +/* + * 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; + +public interface CodecManager { + +} diff --git a/src/main/java/blue/endless/jankson/api/codec/StructuredDataCodec.java b/src/main/java/blue/endless/jankson/api/codec/StructuredDataCodec.java new file mode 100644 index 0000000..44844e2 --- /dev/null +++ b/src/main/java/blue/endless/jankson/api/codec/StructuredDataCodec.java @@ -0,0 +1,66 @@ +/* + * 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.api.io.StructuredDataReader; +import blue.endless.jankson.impl.io.objectwriter.SingleValueFunction; + +public interface StructuredDataCodec { + /** + * Returns true if this codec can be used to create a StructuredData stream about the provided + * object. If you're inquiring about going from StructuredData TO an object, you should use + * {@link #appliesTo(Type)}. + * @param o The object that needs a codec + * @return true if this codec can process the supplied Object, otherwise false. + */ + public default boolean appliesTo(Object o) { + return appliesTo(o.getClass()); + } + + /** + * Returns true if this codec can be used to create and/or process objects of the given type. + * @param t The type that needs a codec + * @return true if this codec applies to objects of the provided type, otherwise false. + */ + public boolean appliesTo(Type t); + + /** + * Gets a StructuredDataReader which can produce a stream of data that represents the provided + * object. + * @param o The object to produce a data stream for + * @return A stream of StructuredData which represents the provided object + */ + public StructuredDataReader getReader(Object o); + + /** + * Gets a StructuredDataWriter that can consume a stream of structured data and produce an + * object of the kind that this codec manages. + * @param The type of the object this codec manages + * @return A StructuredDataWriter that can consume a stream for this type. + */ + public SingleValueFunction getWriter(); +} diff --git a/src/main/java/blue/endless/jankson/impl/io/value/StrictValueElementWriter.java b/src/main/java/blue/endless/jankson/api/codec/package-info.java similarity index 70% rename from src/main/java/blue/endless/jankson/impl/io/value/StrictValueElementWriter.java rename to src/main/java/blue/endless/jankson/api/codec/package-info.java index fd172b0..e3d1ea5 100644 --- a/src/main/java/blue/endless/jankson/impl/io/value/StrictValueElementWriter.java +++ b/src/main/java/blue/endless/jankson/api/codec/package-info.java @@ -22,16 +22,8 @@ * SOFTWARE. */ -package blue.endless.jankson.impl.io.value; - -import blue.endless.jankson.api.document.ValueElement; -import blue.endless.jankson.api.io.StructuredDataWriter; - /** - * Represents a StructuredDataWriter specialized for creating a single kind of ValueElement. - * May create and delegate to other StrictValueElementWriters to build a final object. + * This package contains classes related to obtaining appropriate readers and writers for java + * objects based on their types. */ -public interface StrictValueElementWriter extends StructuredDataWriter { - public abstract ValueElement getValue(); - public boolean isComplete(); -} +package blue.endless.jankson.api.codec; \ No newline at end of file diff --git a/src/main/java/blue/endless/jankson/api/document/ArrayElement.java b/src/main/java/blue/endless/jankson/api/document/ArrayElement.java index d06da30..d3bceba 100644 --- a/src/main/java/blue/endless/jankson/api/document/ArrayElement.java +++ b/src/main/java/blue/endless/jankson/api/document/ArrayElement.java @@ -33,6 +33,7 @@ import java.util.OptionalInt; import java.util.OptionalLong; +import blue.endless.jankson.api.SyntaxError; import blue.endless.jankson.api.io.StructuredData; import blue.endless.jankson.api.io.StructuredDataWriter; @@ -304,7 +305,7 @@ public void setDefault(boolean isDefault) { } @Override - public void write(StructuredDataWriter writer) throws IOException { + public void write(StructuredDataWriter writer) throws SyntaxError, IOException { for (NonValueElement elem : prologue) { elem.write(writer); } diff --git a/src/main/java/blue/endless/jankson/api/document/CommentElement.java b/src/main/java/blue/endless/jankson/api/document/CommentElement.java index 98add22..735c697 100644 --- a/src/main/java/blue/endless/jankson/api/document/CommentElement.java +++ b/src/main/java/blue/endless/jankson/api/document/CommentElement.java @@ -26,6 +26,7 @@ import java.io.IOException; +import blue.endless.jankson.api.SyntaxError; import blue.endless.jankson.api.io.StructuredData; import blue.endless.jankson.api.io.StructuredDataWriter; @@ -85,7 +86,7 @@ public void setDefault(boolean isDefault) { } @Override - public void write(StructuredDataWriter writer) throws IOException { + public void write(StructuredDataWriter writer) throws SyntaxError, IOException { writer.write(new StructuredData(StructuredData.Type.COMMENT, this)); } diff --git a/src/main/java/blue/endless/jankson/api/document/DocumentElement.java b/src/main/java/blue/endless/jankson/api/document/DocumentElement.java index 39ce04b..632dfeb 100644 --- a/src/main/java/blue/endless/jankson/api/document/DocumentElement.java +++ b/src/main/java/blue/endless/jankson/api/document/DocumentElement.java @@ -26,6 +26,7 @@ import java.io.IOException; +import blue.endless.jankson.api.SyntaxError; import blue.endless.jankson.api.io.StructuredDataWriter; /** @@ -64,5 +65,5 @@ public interface DocumentElement extends Cloneable { */ public void setDefault(boolean isDefault); - public void write(StructuredDataWriter writer) throws IOException; + public void write(StructuredDataWriter writer) throws SyntaxError, IOException; } diff --git a/src/main/java/blue/endless/jankson/api/document/FormattingElement.java b/src/main/java/blue/endless/jankson/api/document/FormattingElement.java index 327ec80..2840558 100644 --- a/src/main/java/blue/endless/jankson/api/document/FormattingElement.java +++ b/src/main/java/blue/endless/jankson/api/document/FormattingElement.java @@ -26,6 +26,7 @@ import java.io.IOException; +import blue.endless.jankson.api.SyntaxError; import blue.endless.jankson.api.io.StructuredData; import blue.endless.jankson.api.io.StructuredDataWriter; @@ -69,7 +70,7 @@ public void setDefault(boolean isDefault) { } @Override - public void write(StructuredDataWriter writer) throws IOException { + public void write(StructuredDataWriter writer) throws SyntaxError, IOException { if (this == NEWLINE) { writer.write(StructuredData.NEWLINE); } else { diff --git a/src/main/java/blue/endless/jankson/api/document/KeyValuePairElement.java b/src/main/java/blue/endless/jankson/api/document/KeyValuePairElement.java index c3afb6a..a0fab35 100644 --- a/src/main/java/blue/endless/jankson/api/document/KeyValuePairElement.java +++ b/src/main/java/blue/endless/jankson/api/document/KeyValuePairElement.java @@ -29,6 +29,7 @@ import java.util.List; import java.util.Map; +import blue.endless.jankson.api.SyntaxError; import blue.endless.jankson.api.io.StructuredData; import blue.endless.jankson.api.io.StructuredDataWriter; @@ -116,7 +117,7 @@ public void setDefault(boolean isDefault) { } @Override - public void write(StructuredDataWriter writer) throws IOException { + public void write(StructuredDataWriter writer) throws SyntaxError, IOException { for(NonValueElement elem : prologue) elem.write(writer); writer.write(StructuredData.objectKey(key)); diff --git a/src/main/java/blue/endless/jankson/api/document/ObjectElement.java b/src/main/java/blue/endless/jankson/api/document/ObjectElement.java index 0ce0277..fda3278 100644 --- a/src/main/java/blue/endless/jankson/api/document/ObjectElement.java +++ b/src/main/java/blue/endless/jankson/api/document/ObjectElement.java @@ -37,6 +37,7 @@ import javax.annotation.Nullable; +import blue.endless.jankson.api.SyntaxError; import blue.endless.jankson.api.io.JsonWriter; import blue.endless.jankson.api.io.StructuredData; import blue.endless.jankson.api.io.StructuredDataWriter; @@ -139,7 +140,7 @@ public void setDefault(boolean isDefault) { this.isDefault = isDefault; } - public void write(StructuredDataWriter writer) throws IOException { + public void write(StructuredDataWriter writer) throws SyntaxError, IOException { for(NonValueElement elem : prologue) elem.write(writer); writer.write(StructuredData.OBJECT_START); @@ -159,7 +160,7 @@ public String toString() { try { this.write(v); - } catch (IOException e) { + } catch (SyntaxError | IOException e) { throw new RuntimeException(e); } diff --git a/src/main/java/blue/endless/jankson/api/io/IniReader.java b/src/main/java/blue/endless/jankson/api/io/IniReader.java index 14c8691..364422b 100644 --- a/src/main/java/blue/endless/jankson/api/io/IniReader.java +++ b/src/main/java/blue/endless/jankson/api/io/IniReader.java @@ -33,13 +33,10 @@ import java.util.Optional; import java.util.OptionalDouble; import java.util.OptionalInt; -import java.util.regex.Matcher; import java.util.regex.Pattern; -import javax.naming.spi.DirStateFactory.Result; import blue.endless.jankson.api.SyntaxError; -import blue.endless.jankson.api.document.PrimitiveElement; import blue.endless.jankson.impl.io.AbstractStructuredDataReader; import blue.endless.jankson.impl.io.LookaheadCodePointReader; import blue.endless.jankson.impl.io.context.StringValueParser; @@ -79,7 +76,7 @@ private String grabHeading() throws IOException { } @Override - protected void readNext() throws IOException { + protected void readNext() throws SyntaxError, IOException { while(Character.isWhitespace(src.peek())) src.read(); // Skip line breaks, etc. if (src.peek() == -1) { diff --git a/src/main/java/blue/endless/jankson/api/io/ObjectWriter.java b/src/main/java/blue/endless/jankson/api/io/ObjectWriter.java index 31c7399..dbcac2a 100644 --- a/src/main/java/blue/endless/jankson/api/io/ObjectWriter.java +++ b/src/main/java/blue/endless/jankson/api/io/ObjectWriter.java @@ -255,7 +255,7 @@ public void write(StructuredData data) throws IOException { try { if(delegate != null) { - delegate.accept(data); + delegate.write(data); if (delegate.isComplete()) { commitResult(); } @@ -269,7 +269,7 @@ public void write(StructuredData data) throws IOException { StructuredDataFunction function = getObjectWriter(type, data, subject); if (function != null) { delegate = (StructuredDataFunction) function; - delegate.accept(data); + delegate.write(data); } } } diff --git a/src/main/java/blue/endless/jankson/api/io/ObjectWriterFactory.java b/src/main/java/blue/endless/jankson/api/io/ObjectWriterFactory.java index 3471a50..5cac703 100644 --- a/src/main/java/blue/endless/jankson/api/io/ObjectWriterFactory.java +++ b/src/main/java/blue/endless/jankson/api/io/ObjectWriterFactory.java @@ -29,6 +29,7 @@ import java.util.HashMap; import java.util.Map; +import blue.endless.jankson.api.SyntaxError; import blue.endless.jankson.api.document.ValueElement; import blue.endless.jankson.api.function.CheckedFunction; @@ -59,7 +60,11 @@ public static interface ReaderDeserializer extends CheckedFunction ReaderDeserializer of(ValueDeserializer function) { return (reader) -> { ValueElementWriter writer = new ValueElementWriter(); - reader.transferTo(writer); + try { + reader.transferTo(writer); + } catch (SyntaxError e) { + throw new IOException(e); + } return function.apply(writer.toValueElement()); }; } diff --git a/src/main/java/blue/endless/jankson/api/io/StructuredDataReader.java b/src/main/java/blue/endless/jankson/api/io/StructuredDataReader.java index df277c2..2daf029 100644 --- a/src/main/java/blue/endless/jankson/api/io/StructuredDataReader.java +++ b/src/main/java/blue/endless/jankson/api/io/StructuredDataReader.java @@ -26,6 +26,8 @@ import java.io.IOException; +import blue.endless.jankson.api.SyntaxError; + public interface StructuredDataReader { /** @@ -33,15 +35,15 @@ public interface StructuredDataReader { * traversal of the object tree (starting at the root, descend to leaves in order) just like if you read the json * document from start to finish. * @return An ElementType corresponding to where we are in the object tree. + * @throws SyntaxError */ - public StructuredData next() throws IOException; + public StructuredData next() throws SyntaxError, IOException; public boolean hasNext(); - public default void transferTo(StructuredDataWriter writer) throws IOException { + public default void transferTo(StructuredDataWriter writer) throws SyntaxError, IOException { while(hasNext()) { var d = next(); - //System.out.println(d); writer.write(d); } } diff --git a/src/main/java/blue/endless/jankson/api/io/StructuredDataWriter.java b/src/main/java/blue/endless/jankson/api/io/StructuredDataWriter.java index 4b1833d..41d3233 100644 --- a/src/main/java/blue/endless/jankson/api/io/StructuredDataWriter.java +++ b/src/main/java/blue/endless/jankson/api/io/StructuredDataWriter.java @@ -26,7 +26,9 @@ import java.io.IOException; +import blue.endless.jankson.api.SyntaxError; + public interface StructuredDataWriter { - public void write(StructuredData data) throws IOException; + public void write(StructuredData data) throws SyntaxError, IOException; } diff --git a/src/main/java/blue/endless/jankson/api/io/TomlReader.java b/src/main/java/blue/endless/jankson/api/io/TomlReader.java index 47e896f..75c5c09 100644 --- a/src/main/java/blue/endless/jankson/api/io/TomlReader.java +++ b/src/main/java/blue/endless/jankson/api/io/TomlReader.java @@ -145,7 +145,7 @@ public TomlReader(Reader src) { } @Override - protected void readNext() throws IOException { + protected void readNext() throws SyntaxError, IOException { if (hasReadInData) { readQueue.push(StructuredData.EOF); return; diff --git a/src/main/java/blue/endless/jankson/api/io/ValueElementWriter.java b/src/main/java/blue/endless/jankson/api/io/ValueElementWriter.java index 066de20..97f983f 100644 --- a/src/main/java/blue/endless/jankson/api/io/ValueElementWriter.java +++ b/src/main/java/blue/endless/jankson/api/io/ValueElementWriter.java @@ -27,12 +27,13 @@ import java.io.IOException; import java.util.ArrayList; +import blue.endless.jankson.api.SyntaxError; import blue.endless.jankson.api.document.NonValueElement; import blue.endless.jankson.api.document.ValueElement; +import blue.endless.jankson.impl.io.objectwriter.StructuredDataFunction; import blue.endless.jankson.impl.io.value.ArrayElementWriter; import blue.endless.jankson.impl.io.value.ObjectElementWriter; import blue.endless.jankson.impl.io.value.PrimitiveElementWriter; -import blue.endless.jankson.impl.io.value.StrictValueElementWriter; /** * StructuredDataWriter that assembles a ValueElement. Much like StringWriter, this "captures" data @@ -40,24 +41,24 @@ */ public class ValueElementWriter implements StructuredDataWriter { - private StrictValueElementWriter delegate = null; + private StructuredDataFunction delegate = null; private ValueElement result = null; private ArrayList bufferedComments = new ArrayList<>(); @Override - public void write(StructuredData data) throws IOException { + public void write(StructuredData data) throws SyntaxError, IOException { if (delegate != null && !delegate.isComplete()) { // After we've completed our data, we could potentially consume a trailer delegate.write(data); if (delegate.isComplete()) { - result = delegate.getValue(); + result = delegate.getResult(); result.getPrologue().addAll(bufferedComments); bufferedComments.clear(); delegate = null; } } else { if (delegate != null && delegate.isComplete()) { - result = delegate.getValue(); + result = delegate.getResult(); delegate = null; if (data.type() == StructuredData.Type.EOF) return; diff --git a/src/main/java/blue/endless/jankson/impl/document/BooleanElementImpl.java b/src/main/java/blue/endless/jankson/impl/document/BooleanElementImpl.java index 89f09d8..a045a9a 100644 --- a/src/main/java/blue/endless/jankson/impl/document/BooleanElementImpl.java +++ b/src/main/java/blue/endless/jankson/impl/document/BooleanElementImpl.java @@ -33,6 +33,7 @@ import java.util.OptionalLong; import java.util.function.Function; +import blue.endless.jankson.api.SyntaxError; import blue.endless.jankson.api.document.NonValueElement; import blue.endless.jankson.api.document.PrimitiveElement; import blue.endless.jankson.api.document.ValueElement; @@ -54,7 +55,7 @@ public ValueElement clone() { } @Override - public void write(StructuredDataWriter writer) throws IOException { + public void write(StructuredDataWriter writer) throws SyntaxError, IOException { for(NonValueElement elem : prologue) elem.write(writer); writer.write(StructuredData.primitive(this)); for(NonValueElement elem : epilogue) elem.write(writer); diff --git a/src/main/java/blue/endless/jankson/impl/document/DoubleElementImpl.java b/src/main/java/blue/endless/jankson/impl/document/DoubleElementImpl.java index 294eb62..fd2d56f 100644 --- a/src/main/java/blue/endless/jankson/impl/document/DoubleElementImpl.java +++ b/src/main/java/blue/endless/jankson/impl/document/DoubleElementImpl.java @@ -33,6 +33,7 @@ import java.util.OptionalLong; import java.util.function.DoubleFunction; +import blue.endless.jankson.api.SyntaxError; import blue.endless.jankson.api.document.NonValueElement; import blue.endless.jankson.api.document.PrimitiveElement; import blue.endless.jankson.api.document.ValueElement; @@ -54,7 +55,7 @@ public ValueElement clone() { } @Override - public void write(StructuredDataWriter writer) throws IOException { + public void write(StructuredDataWriter writer) throws SyntaxError, IOException { for(NonValueElement elem : prologue) elem.write(writer); writer.write(StructuredData.primitive(this)); for(NonValueElement elem : epilogue) elem.write(writer); diff --git a/src/main/java/blue/endless/jankson/impl/document/LongElementImpl.java b/src/main/java/blue/endless/jankson/impl/document/LongElementImpl.java index e82c6f5..87b16a2 100644 --- a/src/main/java/blue/endless/jankson/impl/document/LongElementImpl.java +++ b/src/main/java/blue/endless/jankson/impl/document/LongElementImpl.java @@ -33,6 +33,7 @@ import java.util.OptionalLong; import java.util.function.LongFunction; +import blue.endless.jankson.api.SyntaxError; import blue.endless.jankson.api.document.NonValueElement; import blue.endless.jankson.api.document.PrimitiveElement; import blue.endless.jankson.api.document.ValueElement; @@ -54,7 +55,7 @@ public ValueElement clone() { } @Override - public void write(StructuredDataWriter writer) throws IOException { + public void write(StructuredDataWriter writer) throws SyntaxError, IOException { for(NonValueElement elem : prologue) elem.write(writer); writer.write(StructuredData.primitive(this)); for(NonValueElement elem : epilogue) elem.write(writer); diff --git a/src/main/java/blue/endless/jankson/impl/document/NullElementImpl.java b/src/main/java/blue/endless/jankson/impl/document/NullElementImpl.java index d446580..c5f2d5a 100644 --- a/src/main/java/blue/endless/jankson/impl/document/NullElementImpl.java +++ b/src/main/java/blue/endless/jankson/impl/document/NullElementImpl.java @@ -35,6 +35,7 @@ import java.util.function.IntFunction; import java.util.function.LongFunction; +import blue.endless.jankson.api.SyntaxError; import blue.endless.jankson.api.document.NonValueElement; import blue.endless.jankson.api.document.PrimitiveElement; import blue.endless.jankson.api.document.ValueElement; @@ -112,7 +113,7 @@ public Optional asBigDecimal() { } @Override - public void write(StructuredDataWriter writer) throws IOException { + public void write(StructuredDataWriter writer) throws SyntaxError, IOException { for(NonValueElement elem : prologue) elem.write(writer); writer.write(StructuredData.NULL); for(NonValueElement elem : epilogue) elem.write(writer); diff --git a/src/main/java/blue/endless/jankson/impl/document/StringElementImpl.java b/src/main/java/blue/endless/jankson/impl/document/StringElementImpl.java index 39ba564..d802596 100644 --- a/src/main/java/blue/endless/jankson/impl/document/StringElementImpl.java +++ b/src/main/java/blue/endless/jankson/impl/document/StringElementImpl.java @@ -33,6 +33,7 @@ import java.util.OptionalLong; import java.util.function.Function; +import blue.endless.jankson.api.SyntaxError; import blue.endless.jankson.api.document.NonValueElement; import blue.endless.jankson.api.document.PrimitiveElement; import blue.endless.jankson.api.io.StructuredData; @@ -54,7 +55,7 @@ public StringElementImpl clone() { } @Override - public void write(StructuredDataWriter writer) throws IOException { + public void write(StructuredDataWriter writer) throws SyntaxError, IOException { for(NonValueElement elem : prologue) elem.write(writer); writer.write(StructuredData.primitive(this)); for(NonValueElement elem : epilogue) elem.write(writer); diff --git a/src/main/java/blue/endless/jankson/impl/io/AbstractStructuredDataReader.java b/src/main/java/blue/endless/jankson/impl/io/AbstractStructuredDataReader.java index 3f6d67e..44947b8 100644 --- a/src/main/java/blue/endless/jankson/impl/io/AbstractStructuredDataReader.java +++ b/src/main/java/blue/endless/jankson/impl/io/AbstractStructuredDataReader.java @@ -29,6 +29,7 @@ import java.util.ArrayDeque; import java.util.Deque; +import blue.endless.jankson.api.SyntaxError; import blue.endless.jankson.api.io.StructuredData; import blue.endless.jankson.api.io.StructuredDataReader; import blue.endless.jankson.impl.io.context.ParserContext; @@ -62,7 +63,7 @@ protected void enqueueOutput(StructuredData value) { readQueue.push(value); } - protected void skipNonBreakingWhitespace() throws IOException { + protected void skipNonBreakingWhitespace() throws SyntaxError, IOException { while(true) { int ch = src.peek(); if (ch==-1) return; @@ -72,7 +73,7 @@ protected void skipNonBreakingWhitespace() throws IOException { } } - protected abstract void readNext() throws IOException; + protected abstract void readNext() throws SyntaxError, IOException; @Override public boolean hasNext() { @@ -82,7 +83,7 @@ public boolean hasNext() { } @Override - public StructuredData next() throws IOException { + public StructuredData next() throws SyntaxError, IOException { while(readQueue.isEmpty()) { readNext(); } diff --git a/src/main/java/blue/endless/jankson/impl/io/objectreader/DelegatingStructuredDataReader.java b/src/main/java/blue/endless/jankson/impl/io/objectreader/DelegatingStructuredDataReader.java index 6a09efa..37bbcac 100644 --- a/src/main/java/blue/endless/jankson/impl/io/objectreader/DelegatingStructuredDataReader.java +++ b/src/main/java/blue/endless/jankson/impl/io/objectreader/DelegatingStructuredDataReader.java @@ -26,6 +26,7 @@ import java.io.IOException; +import blue.endless.jankson.api.SyntaxError; import blue.endless.jankson.api.io.StructuredData; import blue.endless.jankson.api.io.StructuredDataReader; import blue.endless.jankson.impl.io.StructuredDataBuffer; @@ -49,7 +50,7 @@ public boolean hasNext() { } @Override - public StructuredData next() throws IOException { + public StructuredData next() throws SyntaxError, IOException { if (buffer.isEmpty()) { //Usually this will happen on the first run. Prime our first element loadNextElem(); @@ -84,7 +85,7 @@ protected void setDelegate(StructuredDataReader reader) { *

One way or another, guarantees !pipe.isEmpty() at return * @throws IOException if a delegate encountered a problem reading data. */ - private void loadNextElem() throws IOException { + private void loadNextElem() throws SyntaxError, IOException { // If we're already fulfilled, NOP out. if (!buffer.isEmpty()) return; diff --git a/src/main/java/blue/endless/jankson/impl/io/objectwriter/ArrayFunction.java b/src/main/java/blue/endless/jankson/impl/io/objectwriter/ArrayFunction.java index 8d64eb7..650aefb 100644 --- a/src/main/java/blue/endless/jankson/impl/io/objectwriter/ArrayFunction.java +++ b/src/main/java/blue/endless/jankson/impl/io/objectwriter/ArrayFunction.java @@ -24,6 +24,7 @@ package blue.endless.jankson.impl.io.objectwriter; +import java.io.IOException; import java.lang.reflect.Array; import java.lang.reflect.Type; import java.util.ArrayList; @@ -68,9 +69,9 @@ private void checkDelegate() { @SuppressWarnings("unchecked") @Override - protected void process(StructuredData data) throws SyntaxError { + protected void process(StructuredData data) throws SyntaxError, IOException { if (delegate != null) { - delegate.accept(data); + delegate.write(data); checkDelegate(); return; } @@ -95,7 +96,7 @@ protected void process(StructuredData data) throws SyntaxError { default -> { if (!data.type().isSemantic()) return; delegate = (StructuredDataFunction) ObjectWriter.getObjectWriter(elementType, data, null); - delegate.accept(data); + delegate.write(data); checkDelegate(); } } diff --git a/src/main/java/blue/endless/jankson/impl/io/objectwriter/CollectionFunction.java b/src/main/java/blue/endless/jankson/impl/io/objectwriter/CollectionFunction.java index fe67b6a..4d048e8 100644 --- a/src/main/java/blue/endless/jankson/impl/io/objectwriter/CollectionFunction.java +++ b/src/main/java/blue/endless/jankson/impl/io/objectwriter/CollectionFunction.java @@ -24,6 +24,7 @@ package blue.endless.jankson.impl.io.objectwriter; +import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.Type; import java.util.Collection; @@ -67,9 +68,9 @@ private void checkDelegate() { @SuppressWarnings("unchecked") @Override - protected void process(StructuredData data) throws SyntaxError { + protected void process(StructuredData data) throws SyntaxError, IOException { if (delegate != null) { - delegate.accept(data); + delegate.write(data); checkDelegate(); return; } @@ -93,7 +94,7 @@ protected void process(StructuredData data) throws SyntaxError { default -> { delegate = (StructuredDataFunction) ObjectWriter.getObjectWriter(memberType, data, null); - delegate.accept(data); + delegate.write(data); checkDelegate(); } } diff --git a/src/main/java/blue/endless/jankson/impl/io/objectwriter/MapFunction.java b/src/main/java/blue/endless/jankson/impl/io/objectwriter/MapFunction.java index a1bfd34..43672ab 100644 --- a/src/main/java/blue/endless/jankson/impl/io/objectwriter/MapFunction.java +++ b/src/main/java/blue/endless/jankson/impl/io/objectwriter/MapFunction.java @@ -24,6 +24,7 @@ package blue.endless.jankson.impl.io.objectwriter; +import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.Type; import java.util.HashMap; @@ -114,9 +115,9 @@ private void checkDelegate() { @SuppressWarnings("unchecked") @Override - protected void process(StructuredData data) throws SyntaxError { + protected void process(StructuredData data) throws SyntaxError, IOException { if (delegate != null) { - delegate.accept(data); + delegate.write(data); checkDelegate(); return; } @@ -152,7 +153,7 @@ protected void process(StructuredData data) throws SyntaxError { default -> { delegate = (StructuredDataFunction) ObjectWriter.getObjectWriter(valueType, data, null); - delegate.accept(data); + delegate.write(data); checkDelegate(); } } diff --git a/src/main/java/blue/endless/jankson/impl/io/objectwriter/ObjectFunction.java b/src/main/java/blue/endless/jankson/impl/io/objectwriter/ObjectFunction.java index ff5c903..d214720 100644 --- a/src/main/java/blue/endless/jankson/impl/io/objectwriter/ObjectFunction.java +++ b/src/main/java/blue/endless/jankson/impl/io/objectwriter/ObjectFunction.java @@ -24,14 +24,8 @@ package blue.endless.jankson.impl.io.objectwriter; -import java.lang.reflect.Constructor; -import java.lang.reflect.Field; -import java.lang.reflect.RecordComponent; +import java.io.IOException; import java.lang.reflect.Type; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; import blue.endless.jankson.api.SyntaxError; import blue.endless.jankson.api.io.ObjectWriter; @@ -88,9 +82,9 @@ public T getResult() { @SuppressWarnings("unchecked") @Override - protected void process(StructuredData data) throws SyntaxError { + protected void process(StructuredData data) throws SyntaxError, IOException { if (delegate != null) { - delegate.accept(data); + delegate.write(data); checkDelegate(); return; } @@ -131,7 +125,7 @@ protected void process(StructuredData data) throws SyntaxError { Type fieldType = wrapper.getType(delegateKey); if (fieldType == null) { delegate = SingleValueFunction.discard(); - delegate.accept(data); + delegate.write(data); checkDelegate(); return; } @@ -139,7 +133,7 @@ protected void process(StructuredData data) throws SyntaxError { // TODO: Hand over the instance to start with delegate = (StructuredDataFunction) ObjectWriter.getObjectWriter(fieldType, data, null); if (delegate != null) { - delegate.accept(data); + delegate.write(data); } else { throw new IllegalStateException("Oops"); } diff --git a/src/main/java/blue/endless/jankson/impl/io/objectwriter/RecordFunction.java b/src/main/java/blue/endless/jankson/impl/io/objectwriter/RecordFunction.java index 976dc80..d6cf075 100644 --- a/src/main/java/blue/endless/jankson/impl/io/objectwriter/RecordFunction.java +++ b/src/main/java/blue/endless/jankson/impl/io/objectwriter/RecordFunction.java @@ -24,6 +24,7 @@ package blue.endless.jankson.impl.io.objectwriter; +import java.io.IOException; import java.lang.reflect.AnnotatedType; import java.lang.reflect.Constructor; import java.lang.reflect.RecordComponent; @@ -98,7 +99,6 @@ private Constructor getCanonicalConstructor() throws NoSuchMethodException, S private void checkDelegate() throws SyntaxError { if (delegate != null && delegate.isComplete()) { - Object o = delegate.getResult(); String fieldName = serializedNameToFieldName.get(delegateKey); if (fieldName != null) { values.put(fieldName, delegate.getResult()); @@ -133,10 +133,10 @@ private void checkDelegate() throws SyntaxError { @SuppressWarnings("unchecked") @Override - protected void process(StructuredData data) throws SyntaxError { + protected void process(StructuredData data) throws SyntaxError, IOException { checkDelegate(); if(delegate != null && !delegate.isComplete()) { - delegate.accept(data); + delegate.write(data); checkDelegate(); return; } @@ -162,7 +162,7 @@ protected void process(StructuredData data) throws SyntaxError { String fieldName = serializedNameToFieldName.get(delegateKey); if (fieldName == null) { delegate = SingleValueFunction.discard(); - delegate.accept(data); + delegate.write(data); checkDelegate(); return; } @@ -178,12 +178,12 @@ protected void process(StructuredData data) throws SyntaxError { delegate = (StructuredDataFunction) ObjectWriter.getObjectWriter(fieldType, data, null); if (delegate != null) { - delegate.accept(data); + delegate.write(data); } } else { // This key doesn't correspond to anything recognizeable in the record. delegate = SingleValueFunction.discard(); - delegate.accept(data); + delegate.write(data); } } } else { diff --git a/src/main/java/blue/endless/jankson/impl/io/objectwriter/SingleValueFunction.java b/src/main/java/blue/endless/jankson/impl/io/objectwriter/SingleValueFunction.java index ec60508..82ade66 100644 --- a/src/main/java/blue/endless/jankson/impl/io/objectwriter/SingleValueFunction.java +++ b/src/main/java/blue/endless/jankson/impl/io/objectwriter/SingleValueFunction.java @@ -27,6 +27,8 @@ import blue.endless.jankson.api.io.StructuredData; import static blue.endless.jankson.api.io.StructuredData.Type.*; +import java.io.IOException; + import blue.endless.jankson.api.SyntaxError; public abstract class SingleValueFunction implements StructuredDataFunction { @@ -39,7 +41,7 @@ public boolean isComplete() { } @Override - public void accept(StructuredData data) throws SyntaxError { + public void write(StructuredData data) throws SyntaxError, IOException { if (complete) return; process(data); @@ -66,7 +68,7 @@ public void accept(StructuredData data) throws SyntaxError { } } - protected abstract void process(StructuredData data) throws SyntaxError; + protected abstract void process(StructuredData data) throws SyntaxError, IOException; public static SingleValueFunction discard() { return new Discard(); diff --git a/src/main/java/blue/endless/jankson/impl/io/objectwriter/StructuredDataFunction.java b/src/main/java/blue/endless/jankson/impl/io/objectwriter/StructuredDataFunction.java index 923beb3..4a3cb47 100644 --- a/src/main/java/blue/endless/jankson/impl/io/objectwriter/StructuredDataFunction.java +++ b/src/main/java/blue/endless/jankson/impl/io/objectwriter/StructuredDataFunction.java @@ -24,20 +24,22 @@ package blue.endless.jankson.impl.io.objectwriter; +import java.io.IOException; import java.util.Optional; import java.util.function.Function; import blue.endless.jankson.api.SyntaxError; import blue.endless.jankson.api.io.StructuredData; +import blue.endless.jankson.api.io.StructuredDataWriter; /** * A StructuredDataFunction is a job that consumes StructuredData over time, and produces a value * when complete. * @param The return type of the function. */ -public interface StructuredDataFunction { +public interface StructuredDataFunction extends StructuredDataWriter { boolean isComplete(); - void accept(StructuredData data) throws SyntaxError; + void write(StructuredData data) throws SyntaxError, IOException; T getResult(); public class Mapper implements StructuredDataFunction { @@ -55,8 +57,8 @@ public boolean isComplete() { } @Override - public void accept(StructuredData data) throws SyntaxError { - function.accept(data); + public void write(StructuredData data) throws SyntaxError, IOException { + function.write(data); } @Override diff --git a/src/main/java/blue/endless/jankson/impl/io/value/ArrayElementWriter.java b/src/main/java/blue/endless/jankson/impl/io/value/ArrayElementWriter.java index 6a77a8b..1aba400 100644 --- a/src/main/java/blue/endless/jankson/impl/io/value/ArrayElementWriter.java +++ b/src/main/java/blue/endless/jankson/impl/io/value/ArrayElementWriter.java @@ -27,6 +27,7 @@ import java.io.IOException; import java.util.ArrayList; +import blue.endless.jankson.api.SyntaxError; import blue.endless.jankson.api.document.ArrayElement; import blue.endless.jankson.api.document.CommentElement; import blue.endless.jankson.api.document.CommentType; @@ -34,17 +35,18 @@ import blue.endless.jankson.api.document.NonValueElement; import blue.endless.jankson.api.document.ValueElement; import blue.endless.jankson.api.io.StructuredData; +import blue.endless.jankson.impl.io.objectwriter.StructuredDataFunction; -public class ArrayElementWriter implements StrictValueElementWriter { +public class ArrayElementWriter implements StructuredDataFunction { private boolean initialBracketFound = false; private boolean finalBracketFound = false; private ArrayElement value = new ArrayElement(); - private StrictValueElementWriter delegate; + private StructuredDataFunction delegate; private ArrayList bufferedValuePrologue = new ArrayList<>(); @Override - public void write(StructuredData data) throws IOException { + public void write(StructuredData data) throws SyntaxError, IOException { if (!initialBracketFound) { // There is only one valid type, ARRAY_START if (data.type() == StructuredData.Type.ARRAY_START) { @@ -52,10 +54,10 @@ public void write(StructuredData data) throws IOException { } else if (data.isComment()) { value.getPrologue().add(data.asComment()); } else { - throw new IOException("Expected array start, found "+data.type().name()); + throw new SyntaxError("Expected array start, found "+data.type().name()); } } else if (finalBracketFound) { - if (data.type().isSemantic()) throw new IOException("Anomalous "+data.type().name()+" found after closing bracket of array"); + if (data.type().isSemantic()) throw new SyntaxError("Anomalous "+data.type().name()+" found after closing bracket of array"); } else { // Anything we receive here is an array element @@ -102,15 +104,15 @@ public void write(StructuredData data) throws IOException { bufferedValuePrologue.add(comment); } } - case OBJECT_KEY -> throw new IOException("Expected array element, but found an object key"); - case OBJECT_END -> throw new IOException("Found an object ending brace, but we're inside an array!"); + case OBJECT_KEY -> throw new SyntaxError("Expected array element, but found an object key"); + case OBJECT_END -> throw new SyntaxError("Found an object ending brace, but we're inside an array!"); } } } private void checkSubordinate() throws IOException { if (delegate != null && delegate.isComplete()) { - ValueElement result = delegate.getValue(); + ValueElement result = delegate.getResult(); result.getPrologue().addAll(bufferedValuePrologue); bufferedValuePrologue.clear(); value.add(result); @@ -120,7 +122,7 @@ private void checkSubordinate() throws IOException { } @Override - public ArrayElement getValue() { + public ArrayElement getResult() { return value; } diff --git a/src/main/java/blue/endless/jankson/impl/io/value/ObjectElementWriter.java b/src/main/java/blue/endless/jankson/impl/io/value/ObjectElementWriter.java index 9c10cfc..c7ed026 100644 --- a/src/main/java/blue/endless/jankson/impl/io/value/ObjectElementWriter.java +++ b/src/main/java/blue/endless/jankson/impl/io/value/ObjectElementWriter.java @@ -28,6 +28,7 @@ import java.util.ArrayList; import java.util.List; +import blue.endless.jankson.api.SyntaxError; import blue.endless.jankson.api.document.CommentElement; import blue.endless.jankson.api.document.CommentType; import blue.endless.jankson.api.document.FormattingElement; @@ -36,20 +37,21 @@ import blue.endless.jankson.api.document.ObjectElement; import blue.endless.jankson.api.document.ValueElement; import blue.endless.jankson.api.io.StructuredData; +import blue.endless.jankson.impl.io.objectwriter.StructuredDataFunction; -public class ObjectElementWriter implements StrictValueElementWriter { +public class ObjectElementWriter implements StructuredDataFunction { private boolean initialBraceFound = false; private boolean finalBraceFound = false; private ObjectElement value = new ObjectElement(); - private StrictValueElementWriter subordinate; + private StructuredDataFunction subordinate; private List bufferedKeyPreamble = new ArrayList<>(); private List bufferedValuePreamble = new ArrayList<>(); private String bufferedKey; @Override - public void write(StructuredData data) throws IOException { + public void write(StructuredData data) throws SyntaxError, IOException { if (subordinate != null) { subordinate.write(data); @@ -61,7 +63,7 @@ public void write(StructuredData data) throws IOException { System.out.println("Dealing with comment"); value.getPrologue().add(data.asComment()); } else { - if (data.type() != StructuredData.Type.OBJECT_START) throw new IOException("Required to start an object: OBJECT_START. Found: "+data.type().name()); + if (data.type() != StructuredData.Type.OBJECT_START) throw new SyntaxError("Required to start an object: OBJECT_START. Found: "+data.type().name()); initialBraceFound = true; } } else if (!finalBraceFound) { @@ -87,7 +89,7 @@ public void write(StructuredData data) throws IOException { default -> { if (data.type().isSemantic()) { - throw new IOException("Expected object key but found "+data.type().name()); + throw new SyntaxError("Expected object key but found "+data.type().name()); } } } @@ -123,16 +125,16 @@ public void write(StructuredData data) throws IOException { // bufferedValuePreamble.add(new FormattingElement(data.value().toString())); } - case OBJECT_KEY -> throw new IOException("Found two object keys in a row!"); - case OBJECT_END -> throw new IOException("Found anomalous object end"); - case ARRAY_END -> throw new IOException("Found anomalous array end"); - case EOF -> throw new IOException("Stream ended before object was closed!"); + case OBJECT_KEY -> throw new SyntaxError("Found two object keys in a row!"); + case OBJECT_END -> throw new SyntaxError("Found anomalous object end"); + case ARRAY_END -> throw new SyntaxError("Found anomalous array end"); + case EOF -> throw new SyntaxError("Stream ended before object was closed!"); } } } else { // No semantic data is allowed after the ending brace if (data.type().isSemantic()) { - throw new IOException("Illegal "+data.type().name()+" found after end of object body"); + throw new SyntaxError("Illegal "+data.type().name()+" found after end of object body"); } else { if (data.isComment()) { value.getEpilogue().add(data.asComment()); @@ -147,10 +149,10 @@ public void write(StructuredData data) throws IOException { } - private void checkSubordinate() throws IOException { + private void checkSubordinate() throws SyntaxError, IOException { if (subordinate != null && subordinate.isComplete()) { - if (bufferedKey == null) throw new IOException("Invalid writer state: we don't have a key for an object value"); - ValueElement result = subordinate.getValue(); + if (bufferedKey == null) throw new SyntaxError("Invalid writer state: we don't have a key for an object value"); + ValueElement result = subordinate.getResult(); result.getPrologue().addAll(bufferedValuePreamble); bufferedValuePreamble.clear(); KeyValuePairElement kvPair = new KeyValuePairElement(bufferedKey, result); @@ -165,7 +167,7 @@ private void checkSubordinate() throws IOException { } @Override - public ObjectElement getValue() { + public ObjectElement getResult() { return value; } diff --git a/src/main/java/blue/endless/jankson/impl/io/value/PrimitiveElementWriter.java b/src/main/java/blue/endless/jankson/impl/io/value/PrimitiveElementWriter.java index 2f286fd..4ec4756 100644 --- a/src/main/java/blue/endless/jankson/impl/io/value/PrimitiveElementWriter.java +++ b/src/main/java/blue/endless/jankson/impl/io/value/PrimitiveElementWriter.java @@ -28,9 +28,11 @@ import java.util.Objects; import blue.endless.jankson.api.document.PrimitiveElement; +import blue.endless.jankson.api.document.ValueElement; import blue.endless.jankson.api.io.StructuredData; +import blue.endless.jankson.impl.io.objectwriter.StructuredDataFunction; -public class PrimitiveElementWriter implements StrictValueElementWriter { +public class PrimitiveElementWriter implements StructuredDataFunction { private boolean complete = false; private PrimitiveElement value = null; @@ -44,7 +46,7 @@ public void write(StructuredData data) throws IOException { } @Override - public PrimitiveElement getValue() { + public PrimitiveElement getResult() { return value; } diff --git a/src/test/java/blue/endless/jankson/IniReaderTests.java b/src/test/java/blue/endless/jankson/IniReaderTests.java index 0efa056..6be1b63 100644 --- a/src/test/java/blue/endless/jankson/IniReaderTests.java +++ b/src/test/java/blue/endless/jankson/IniReaderTests.java @@ -33,6 +33,7 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import blue.endless.jankson.api.SyntaxError; import blue.endless.jankson.api.io.IniReader; import blue.endless.jankson.api.io.JsonWriter; import blue.endless.jankson.api.io.JsonWriterOptions; @@ -70,7 +71,7 @@ public void testIntCast() { } @Test - public void testFileParse() throws IOException { + public void testFileParse() throws SyntaxError, IOException { String iniData = """ [foo] this format = a bunch of key-value pairs diff --git a/src/test/java/blue/endless/jankson/TestObjectWriter.java b/src/test/java/blue/endless/jankson/TestObjectWriter.java index 3e65181..4c43787 100644 --- a/src/test/java/blue/endless/jankson/TestObjectWriter.java +++ b/src/test/java/blue/endless/jankson/TestObjectWriter.java @@ -36,6 +36,7 @@ import org.junit.jupiter.api.Test; import blue.endless.jankson.api.Jankson; +import blue.endless.jankson.api.SyntaxError; import blue.endless.jankson.api.annotation.SerializedName; import blue.endless.jankson.api.io.JsonReader; import blue.endless.jankson.api.io.JsonWriterOptions; @@ -58,7 +59,7 @@ public class TestObjectWriter { */ @Test - public void testBareInt() throws IOException { + public void testBareInt() throws SyntaxError, IOException { String subject = "731"; JsonReader reader = new JsonReader(new StringReader(subject)); var writer = new ObjectWriter<>(Integer.class); @@ -68,7 +69,7 @@ public void testBareInt() throws IOException { } @Test - public void testBareString() throws IOException { + public void testBareString() throws SyntaxError, IOException { String subject = "\"This is a bare string.\""; JsonReader reader = new JsonReader(new StringReader(subject)); var writer = new ObjectWriter<>(String.class); @@ -78,7 +79,7 @@ public void testBareString() throws IOException { } @Test - public void testBareDouble() throws IOException { + public void testBareDouble() throws SyntaxError, IOException { String subject = "12.0"; JsonReader reader = new JsonReader(new StringReader(subject)); var writer = new ObjectWriter<>(Double.class); @@ -88,7 +89,7 @@ public void testBareDouble() throws IOException { } @Test - public void testBareFloat() throws IOException { + public void testBareFloat() throws SyntaxError, IOException { String subject = "13.0"; JsonReader reader = new JsonReader(new StringReader(subject)); var writer = new ObjectWriter<>(Float.class); @@ -98,7 +99,7 @@ public void testBareFloat() throws IOException { } @Test - public void testBareLong() throws IOException { + public void testBareLong() throws SyntaxError, IOException { String subject = "9223372036854775807"; // This happens to be Long.MAX_VALUE, but the important part is that it's big. JsonReader reader = new JsonReader(new StringReader(subject)); var writer = new ObjectWriter<>(Long.class); @@ -108,7 +109,7 @@ public void testBareLong() throws IOException { } @Test - public void testBareBoolean() throws IOException { + public void testBareBoolean() throws SyntaxError, IOException { String subject = "true"; // We don't use "false" here because that's the default value for boolean JsonReader reader = new JsonReader(new StringReader(subject)); var writer = new ObjectWriter<>(Boolean.class); @@ -123,7 +124,7 @@ public void testBareBoolean() throws IOException { */ @Test - public void testSimpleRecord() throws IOException { + public void testSimpleRecord() throws SyntaxError, IOException { record Point(double x, double y, double z) {}; String subject = @@ -188,7 +189,7 @@ record P(Map> value) {}; @Test - public void testCollection() throws IOException { + public void testCollection() throws SyntaxError, IOException { String subject = """ @@ -208,7 +209,7 @@ record P(List value) {} } @Test - public void testNestedCollection() throws IOException { + public void testNestedCollection() throws SyntaxError, IOException { String subject = """ { @@ -228,7 +229,7 @@ record P(List value) {} } @Test - public void testMap() throws IOException { + public void testMap() throws SyntaxError, IOException { String subject = """ @@ -263,7 +264,7 @@ public static class PojoConfig { } @Test - public void testConfigPojo() throws IOException { + public void testConfigPojo() throws SyntaxError, IOException { String subject = """ { @@ -287,7 +288,7 @@ public static class LightConfounder { } @Test - public void testConfigWithEmbeddedListMap() throws IOException { + public void testConfigWithEmbeddedListMap() throws SyntaxError, IOException { String subject = """ { @@ -307,7 +308,7 @@ public void testConfigWithEmbeddedListMap() throws IOException { } @Test - public void testArrays() throws IOException { + public void testArrays() throws SyntaxError, IOException { String subject = """ [ 96, 112, 24] @@ -327,7 +328,7 @@ public void testArrays() throws IOException { */ @Test - public void testFull() throws IOException { + public void testFull() throws SyntaxError, IOException { String expected = """ { diff --git a/src/test/java/blue/endless/jankson/TestSerializer.java b/src/test/java/blue/endless/jankson/TestSerializer.java index dc5e268..cc18744 100644 --- a/src/test/java/blue/endless/jankson/TestSerializer.java +++ b/src/test/java/blue/endless/jankson/TestSerializer.java @@ -50,19 +50,19 @@ public class TestSerializer { @Test - public void testPrimitiveSerialization() throws IOException { + public void testPrimitiveSerialization() throws SyntaxError, IOException { String actual = Jankson.writeJsonString(2, new ObjectReaderFactory(), JsonWriterOptions.ONE_LINE); Assertions.assertEquals("2", actual); } @Test - public void testArraySerialization() throws IOException { + public void testArraySerialization() throws SyntaxError, IOException { String actual = Jankson.writeJsonString(new String[] { "foo", "bar" }, new ObjectReaderFactory(), JsonWriterOptions.ONE_LINE); Assertions.assertEquals("[ \"foo\", \"bar\" ]", actual); } @Test - public void testCollectionSerialization() throws IOException { + public void testCollectionSerialization() throws SyntaxError, IOException { String listActual = Jankson.writeJsonString(List.of(1, 2, 3), new ObjectReaderFactory(), JsonWriterOptions.ONE_LINE); Assertions.assertEquals("[ 1, 2, 3 ]", listActual); diff --git a/src/test/java/blue/endless/jankson/TestTomlReader.java b/src/test/java/blue/endless/jankson/TestTomlReader.java index 2dfd574..873a98b 100644 --- a/src/test/java/blue/endless/jankson/TestTomlReader.java +++ b/src/test/java/blue/endless/jankson/TestTomlReader.java @@ -31,6 +31,7 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import blue.endless.jankson.api.SyntaxError; import blue.endless.jankson.api.io.JsonWriter; import blue.endless.jankson.api.io.JsonWriterOptions; import blue.endless.jankson.api.io.TomlReader; @@ -41,7 +42,7 @@ public class TestTomlReader { private static final JsonWriterOptions STRICT_ONE_LINE = new JsonWriterOptions(Hint.WRITE_WHITESPACE); @Test - public void testKeys() throws IOException { + public void testKeys() throws SyntaxError, IOException { String tomlExample = """ name = "Orange" physical.color = "orange" @@ -72,7 +73,7 @@ public void testKeys() throws IOException { } @Test - public void testBadlyConceivedKeys() throws IOException { + public void testBadlyConceivedKeys() throws SyntaxError, IOException { String tomlExample = """ 3.14159 = "pi" """; @@ -91,7 +92,7 @@ public void testBadlyConceivedKeys() throws IOException { } @Test - public void testTable() throws IOException { + public void testTable() throws SyntaxError, IOException { String tomlExample = """ [dog."tater.man"] type.name = "pug" @@ -114,7 +115,7 @@ public void testTable() throws IOException { * Jankson explicitly allows this, in deviation of the spec. */ @Test - public void testAllowDuplicateTables() throws IOException { + public void testAllowDuplicateTables() throws SyntaxError, IOException { String tomlExample = """ # DO NOT DO THIS (unless you're using jankson) @@ -158,7 +159,7 @@ public void testForbidRedefiningTables() { } @Test - public void testTableArrays() throws IOException { + public void testTableArrays() throws SyntaxError, IOException { String tomlExample = """ [[products]] name = "Hammer" @@ -201,7 +202,7 @@ public void testTableArrays() throws IOException { } @Test - public void testNestedTableArrays() throws IOException { + public void testNestedTableArrays() throws SyntaxError, IOException { String tomlExample = """ [[fruits]] name = "apple" @@ -264,7 +265,7 @@ public void testNestedTableArrays() throws IOException { } @Test - public void testInlineArrays() throws IOException { + public void testInlineArrays() throws SyntaxError, IOException { String tomlExample = """ foo = [ 1, 2, 3, true, "foo" ] """; @@ -283,7 +284,7 @@ public void testInlineArrays() throws IOException { } @Test - public void testArrayOfInlineTables() throws IOException { + public void testArrayOfInlineTables() throws SyntaxError, IOException { String tomlExample = """ points = [ { x = 1, y = 2, z = 3 }, { x = 7, y = 8, z = 9 }, @@ -322,7 +323,7 @@ public void testArrayOfInlineTables() throws IOException { } @Test - public void testDateValues() throws IOException { + public void testDateValues() throws SyntaxError, IOException { String tomlExample = """ odt1 = 1979-05-27T07:32:00Z odt2 = 1979-05-27T00:32:00-07:00