Skip to content

Commit

Permalink
Add SyntaxError to possible writer exceptions, remove duplicate class
Browse files Browse the repository at this point in the history
  • Loading branch information
falkreon committed Jul 31, 2024
1 parent 506d137 commit 3c31978
Show file tree
Hide file tree
Showing 39 changed files with 284 additions and 145 deletions.
25 changes: 9 additions & 16 deletions src/main/java/blue/endless/jankson/api/Jankson.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -240,23 +238,18 @@ public static <T> T readJson(Reader r, JsonReaderOptions opts, Class<T> 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);
Expand All @@ -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);
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/blue/endless/jankson/api/codec/CodecHolder.java
Original file line number Diff line number Diff line change
@@ -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 {

}
29 changes: 29 additions & 0 deletions src/main/java/blue/endless/jankson/api/codec/CodecManager.java
Original file line number Diff line number Diff line change
@@ -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 {

}
Original file line number Diff line number Diff line change
@@ -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 <T> The type of the object this codec manages
* @return A StructuredDataWriter that can consume a stream for this type.
*/
public <T> SingleValueFunction<T> getWriter();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import java.io.IOException;

import blue.endless.jankson.api.SyntaxError;
import blue.endless.jankson.api.io.StructuredDataWriter;

/**
Expand Down Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -159,7 +160,7 @@ public String toString() {

try {
this.write(v);
} catch (IOException e) {
} catch (SyntaxError | IOException e) {
throw new RuntimeException(e);
}

Expand Down
5 changes: 1 addition & 4 deletions src/main/java/blue/endless/jankson/api/io/IniReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/blue/endless/jankson/api/io/ObjectWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -269,7 +269,7 @@ public void write(StructuredData data) throws IOException {
StructuredDataFunction<?> function = getObjectWriter(type, data, subject);
if (function != null) {
delegate = (StructuredDataFunction<Object>) function;
delegate.accept(data);
delegate.write(data);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -59,7 +60,11 @@ public static interface ReaderDeserializer<T> extends CheckedFunction<Structured
public static <T> ReaderDeserializer<T> of(ValueDeserializer<T> 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());
};
}
Expand Down
Loading

0 comments on commit 3c31978

Please sign in to comment.