diff --git a/jacline-lib-common/src/main/jacline/key-decoder.js b/jacline-lib-common/src/main/jacline/key-decoder.js index f7a9bbda..b59de0e9 100644 --- a/jacline-lib-common/src/main/jacline/key-decoder.js +++ b/jacline-lib-common/src/main/jacline/key-decoder.js @@ -1,6 +1,6 @@ goog.module("kohlschutter.coding.KeyDecoder"); -const DecodingException = goog.require("com.kohlschutter.jacline.lib.coding.DecodingException"); +const CodingException = goog.require("com.kohlschutter.jacline.lib.coding.CodingException"); const JsCloseable = goog.require('com.kohlschutter.jacline.lib.io.JsCloseable'); // FIXME type checking @@ -52,7 +52,7 @@ class KeyDecoder { static load(expectedType, o) { if (expectedType) { if (o["javaClass"] != expectedType) { - throw DecodingException.withUnexpectedType(expectedType, o["javaClass"]); + throw CodingException.withUnexpectedType(expectedType, o["javaClass"]); } } return new KeyDecoder(o); diff --git a/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/ArrayDecoder.java b/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/ArrayDecoder.java index 8c951e41..f4166573 100644 --- a/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/ArrayDecoder.java +++ b/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/ArrayDecoder.java @@ -33,7 +33,7 @@ public interface ArrayDecoder { * * @param serialized The serialized representation. * @return The array. - * @throws DecodingException on error. + * @throws CodingException on error. */ - T[] decode(Object serialized) throws DecodingException; + T[] decode(Object serialized) throws CodingException; } diff --git a/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/ArrayEncoder.java b/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/ArrayEncoder.java index 06977ac1..1150b1d5 100644 --- a/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/ArrayEncoder.java +++ b/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/ArrayEncoder.java @@ -33,6 +33,7 @@ public interface ArrayEncoder { * * @param array The array to encode. * @return The encoded object. + * @throws CodingException on error. */ - Object encode(Object[] array); + Object encode(Object[] array) throws CodingException; } diff --git a/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/Codable.java b/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/Codable.java index bcdb24c8..5c2c7fea 100644 --- a/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/Codable.java +++ b/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/Codable.java @@ -40,5 +40,5 @@ public interface Codable { * * @return The encoded representation. */ - Object encode(KeyEncoderProvider provider); + Object encode(KeyEncoderProvider provider) throws CodingException; } diff --git a/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/DecodingException.java b/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/CodingException.java similarity index 63% rename from jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/DecodingException.java rename to jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/CodingException.java index f0eec622..1e05240b 100644 --- a/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/DecodingException.java +++ b/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/CodingException.java @@ -19,32 +19,32 @@ import jsinterop.annotations.JsMethod; -public class DecodingException extends Exception { +public class CodingException extends Exception { private static final long serialVersionUID = 1L; - public DecodingException() { + public CodingException() { super(); } - public DecodingException(String message, Throwable cause) { + public CodingException(String message, Throwable cause) { super(message, cause); } - public DecodingException(String message) { + public CodingException(String message) { super(message); } - public DecodingException(Throwable cause) { + public CodingException(Throwable cause) { super(cause); } @JsMethod - public static DecodingException withMessage(String message) { - return new DecodingException(message); + public static CodingException withMessage(String message) { + return new CodingException(message); } @JsMethod - public static DecodingException withUnexpectedType(String expected, String found) { - return new DecodingException("Unexpected type: " + found + "; expected: " + expected); + public static CodingException withUnexpectedType(String expected, String found) { + return new CodingException("Unexpected type: " + found + "; expected: " + expected); } } diff --git a/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/CodingProviders.java b/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/CodingProviders.java index cf396a9b..e4431bf2 100644 --- a/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/CodingProviders.java +++ b/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/CodingProviders.java @@ -31,8 +31,10 @@ private CodingProviders() { * * @param provider The original {@link KeyEncoderProvider}, or {@code null} (for default). * @return Some {@link KeyEncoderProvider}. + * @throws CodingException on error. */ - public static KeyEncoderProvider decorateEncoderProvider(KeyEncoderProvider provider) { + public static KeyEncoderProvider decorateEncoderProvider(KeyEncoderProvider provider) + throws CodingException { if (provider == null) { return KeyEncoder::begin; } else { @@ -45,17 +47,14 @@ public static KeyEncoderProvider decorateEncoderProvider(KeyEncoderProvider prov * * @param provider The original {@link KeyDecoderProvider}, or {@code null} (for default). * @return Some {@link KeyDecoderProvider}. + * @throws CodingException on error. */ - public static KeyDecoderProvider decorateDecoderProvider(KeyDecoderProvider provider) { + public static KeyDecoderProvider decorateDecoderProvider(KeyDecoderProvider provider) + throws CodingException { if (provider == null) { return KeyDecoder::load; } else { return provider; } } - - public static String getTypeFromEncoded(Object obj) { - // TODO Auto-generated method stub - return null; - } } diff --git a/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/CodingServiceProvider.java b/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/CodingServiceProvider.java index 893cfc83..466941e1 100644 --- a/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/CodingServiceProvider.java +++ b/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/CodingServiceProvider.java @@ -38,11 +38,11 @@ private static CodingServiceProvider findDefault() { return first.isPresent() ? Objects.requireNonNull(first.get()) : null; } - KeyDecoder keyDecoder(String expectedCodedType, Object encoded) throws DecodingException; + KeyDecoder keyDecoder(String expectedCodedType, Object encoded) throws CodingException; - KeyEncoder keyEncoder(String type); + KeyEncoder keyEncoder(String type) throws CodingException; - SequenceDecoder sequenceDecoder(Object encoded) throws DecodingException; + SequenceDecoder sequenceDecoder(Object encoded) throws CodingException; - SequenceEncoder sequenceEncoder(); + SequenceEncoder sequenceEncoder() throws CodingException; } diff --git a/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/Decoder.java b/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/Decoder.java index af17b4fb..0e677fe7 100644 --- a/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/Decoder.java +++ b/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/Decoder.java @@ -22,5 +22,5 @@ @JsFunction @FunctionalInterface public interface Decoder { - T decode(KeyDecoderProvider decoderProvider, Object serialized) throws DecodingException; + T decode(KeyDecoderProvider decoderProvider, Object serialized) throws CodingException; } diff --git a/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/KeyDecoder.java b/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/KeyDecoder.java index 0c14d49a..e3b57b37 100644 --- a/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/KeyDecoder.java +++ b/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/KeyDecoder.java @@ -24,20 +24,20 @@ @JsType(isNative = true, namespace = "kohlschutter.coding", name = "KeyDecoder") public interface KeyDecoder extends JsCloseable { - String stringForKey(String key); + String stringForKey(String key) throws CodingException; - Boolean booleanForKey(String key); + Boolean booleanForKey(String key) throws CodingException; - Number numberForKey(String key); + Number numberForKey(String key) throws CodingException; - T[] arrayForKey(String key, ArrayDecoder decoder) throws DecodingException; + T[] arrayForKey(String key, ArrayDecoder decoder) throws CodingException; - T objectForKey(String key, ObjectDecoder decoder) throws DecodingException; + T objectForKey(String key, ObjectDecoder decoder) throws CodingException; - boolean hasKey(String key); + boolean hasKey(String key) throws CodingException; @JsImplementationProvidedSeparately - static KeyDecoder load(String expectedCodedType, Object encoded) throws DecodingException { + static KeyDecoder load(String expectedCodedType, Object encoded) throws CodingException { return CodingServiceProvider.getDefault().keyDecoder(expectedCodedType, encoded); } } diff --git a/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/KeyDecoderProvider.java b/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/KeyDecoderProvider.java index cf226762..3fc9e09a 100644 --- a/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/KeyDecoderProvider.java +++ b/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/KeyDecoderProvider.java @@ -22,5 +22,5 @@ @JsFunction @FunctionalInterface public interface KeyDecoderProvider { - KeyDecoder load(String expectedCodedType, Object encoded) throws DecodingException; + KeyDecoder load(String expectedCodedType, Object encoded) throws CodingException; } diff --git a/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/KeyEncoder.java b/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/KeyEncoder.java index 46f48bd3..88b41751 100644 --- a/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/KeyEncoder.java +++ b/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/KeyEncoder.java @@ -30,8 +30,9 @@ public interface KeyEncoder { * @param key The key, or {@code null} * @param value The value. * @return This encoder. + * @throws CodingException on error. */ - KeyEncoder encodeString(String key, String value); + KeyEncoder encodeString(String key, String value) throws CodingException; /** * Encodes a boolean (or {@code null}) for the given key. @@ -39,8 +40,9 @@ public interface KeyEncoder { * @param key The key, or {@code null} * @param value The value. * @return This encoder. + * @throws CodingException on error. */ - KeyEncoder encodeBoolean(String key, Boolean value); + KeyEncoder encodeBoolean(String key, Boolean value) throws CodingException; /** * Encodes a number (or {@code null}) for the given key. @@ -48,8 +50,9 @@ public interface KeyEncoder { * @param key The key, or {@code null} * @param value The value. * @return This encoder. + * @throws CodingException on error. */ - KeyEncoder encodeNumber(String key, Number value); + KeyEncoder encodeNumber(String key, Number value) throws CodingException; /** * Encodes an array (or {@code null}) for the given key. @@ -58,8 +61,9 @@ public interface KeyEncoder { * @param encoder The encoder * @param array The array. * @return This encoder. + * @throws CodingException on error. */ - KeyEncoder encodeArray(String key, ArrayEncoder encoder, Object[] array); + KeyEncoder encodeArray(String key, ArrayEncoder encoder, Object[] array) throws CodingException; /** * Returns a new encoder that can encode the object stored under the given key. @@ -67,26 +71,29 @@ public interface KeyEncoder { * @param key The key to store the object under. * @param type The encoded type. * @return The sub-encoder. + * @throws CodingException on error. */ - KeyEncoder beginEncodeObject(String key, String type); + KeyEncoder beginEncodeObject(String key, String type) throws CodingException; /** * Ends any {@link #beginEncodeObject(String, String)} block, returning the parent encoder, or the * same encoder if it's the root encoder. * * @return The parent or this encoder. + * @throws CodingException on error. */ - KeyEncoder end(); + KeyEncoder end() throws CodingException; /** * Returns the encoded representation for the data encoded by this encoder. * * @return The object. + * @throws CodingException on error. */ - Object getEncoded(); + Object getEncoded() throws CodingException; @JsImplementationProvidedSeparately - static KeyEncoder begin(String type) { + static KeyEncoder begin(String type) throws CodingException { return CodingServiceProvider.getDefault().keyEncoder(type); } } diff --git a/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/KeyEncoderProvider.java b/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/KeyEncoderProvider.java index d062c96f..7074b679 100644 --- a/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/KeyEncoderProvider.java +++ b/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/KeyEncoderProvider.java @@ -22,5 +22,5 @@ @JsFunction @FunctionalInterface public interface KeyEncoderProvider { - KeyEncoder begin(String type); + KeyEncoder begin(String type) throws CodingException; } diff --git a/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/ObjectDecoder.java b/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/ObjectDecoder.java index 2477d96b..6d30e212 100644 --- a/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/ObjectDecoder.java +++ b/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/ObjectDecoder.java @@ -22,5 +22,5 @@ @JsFunction @FunctionalInterface public interface ObjectDecoder { - T decode(Object serialized) throws DecodingException; + T decode(Object serialized) throws CodingException; } diff --git a/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/ObjectEncoder.java b/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/ObjectEncoder.java index 9b036d16..3bc6d751 100644 --- a/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/ObjectEncoder.java +++ b/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/ObjectEncoder.java @@ -22,5 +22,5 @@ @JsFunction @FunctionalInterface public interface ObjectEncoder { - Object encode(Object obj); + Object encode(Object obj) throws CodingException; } diff --git a/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/SequenceDecoder.java b/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/SequenceDecoder.java index 788d5aff..c3f929bd 100644 --- a/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/SequenceDecoder.java +++ b/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/SequenceDecoder.java @@ -29,27 +29,27 @@ public interface SequenceDecoder extends JsCloseable { @FunctionalInterface @JsFunction interface SequenceConsumer { - void consume(T t) throws DecodingException; + void consume(T t) throws CodingException; } int size(); int position(); - SequenceDecoder strings(int count, SequenceConsumer forEach) throws DecodingException; + SequenceDecoder strings(int count, SequenceConsumer forEach) throws CodingException; - SequenceDecoder booleans(int count, SequenceConsumer forEach) throws DecodingException; + SequenceDecoder booleans(int count, SequenceConsumer forEach) throws CodingException; - SequenceDecoder numbers(int count, SequenceConsumer forEach) throws DecodingException; + SequenceDecoder numbers(int count, SequenceConsumer forEach) throws CodingException; SequenceDecoder arrays(int count, ArrayDecoder decoder, SequenceConsumer forEach) - throws DecodingException; + throws CodingException; SequenceDecoder objects(int count, ObjectDecoder decoder, SequenceConsumer forEach) - throws DecodingException; + throws CodingException; @JsImplementationProvidedSeparately - static SequenceDecoder load(Object encoded) throws DecodingException { + static SequenceDecoder load(Object encoded) throws CodingException { return CodingServiceProvider.getDefault().sequenceDecoder(encoded); } } diff --git a/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/SequenceEncoder.java b/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/SequenceEncoder.java index a33cd080..8b9b90a9 100644 --- a/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/SequenceEncoder.java +++ b/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/SequenceEncoder.java @@ -28,31 +28,35 @@ public interface SequenceEncoder { * * @param values The values. * @return This encoder. + * @throws CodingException on error. */ - SequenceEncoder encodeStrings(String... values); + SequenceEncoder encodeStrings(String... values) throws CodingException; /** * Encodes a boolean (or {@code null}). * * @param values The values. * @return This encoder. + * @throws CodingException on error. */ - SequenceEncoder encodeBooleans(Boolean... values); + SequenceEncoder encodeBooleans(Boolean... values) throws CodingException; /** * Encodes a number (or {@code null}). * * @param values The values. * @return This encoder. + * @throws CodingException on error. */ - SequenceEncoder encodeNumbers(Number... values); + SequenceEncoder encodeNumbers(Number... values) throws CodingException; /** * Encodes an array. * * @return A new sub-encoder. + * @throws CodingException on error. */ - SequenceEncoder beginEncodeArray(); + SequenceEncoder beginEncodeArray() throws CodingException; /** * Returns a new encoder that can encode the object stored under the given key. @@ -60,26 +64,29 @@ public interface SequenceEncoder { * @param encoder The object encoder * @param objects The objects * @return This encoder. + * @throws CodingException on error. */ - SequenceEncoder encodeObjects(ObjectEncoder encoder, Object... objects); + SequenceEncoder encodeObjects(ObjectEncoder encoder, Object... objects) throws CodingException; /** * Ends any {@link #beginEncodeArray()} block, returning the parent encoder, or the same encoder * if it's the root encoder. * * @return The parent or this encoder. + * @throws CodingException on error. */ - SequenceEncoder end(); + SequenceEncoder end() throws CodingException; /** * Returns the encoded representation for the data encoded by this encoder. * * @return The object. + * @throws CodingException on error. */ - Object getEncoded(); + Object getEncoded() throws CodingException; @JsImplementationProvidedSeparately - static SequenceEncoder begin() { + static SequenceEncoder begin() throws CodingException { return CodingServiceProvider.getDefault().sequenceEncoder(); } } diff --git a/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/StandardArrayDecoders.java b/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/StandardArrayDecoders.java index a418a5e2..8e8e3994 100644 --- a/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/StandardArrayDecoders.java +++ b/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/StandardArrayDecoders.java @@ -23,69 +23,69 @@ @JsType public class StandardArrayDecoders { - public static Object[] objects(Object serialized) throws DecodingException { + public static Object[] objects(Object serialized) throws CodingException { return objectsWithDecoder(serialized, null, (l) -> new Object[l]); } public static T[] objectsWithDecoder(Object serialized, ObjectDecoder decoder, - ArrayCreator arrayCreator) throws DecodingException { + ArrayCreator arrayCreator) throws CodingException { try (SequenceDecoder dec = SequenceDecoder.load(serialized)) { T[] array = arrayCreator.newArray(dec.size()); dec.objects(array.length, decoder, (e) -> array[dec.position()] = e); assert (dec.position() == dec.size()); return array; } catch (IOException e) { - throw new DecodingException(e); + throw new CodingException(e); } } - public static String[] strings(Object serialized) throws DecodingException { + public static String[] strings(Object serialized) throws CodingException { try (SequenceDecoder dec = SequenceDecoder.load(serialized)) { String[] array = new String[dec.size()]; dec.strings(array.length, (e) -> array[dec.position()] = e); assert (dec.position() == dec.size()); return array; } catch (IOException e) { - throw new DecodingException(e); + throw new CodingException(e); } } - public static Boolean[] booleans(Object serialized) throws DecodingException { + public static Boolean[] booleans(Object serialized) throws CodingException { try (SequenceDecoder dec = SequenceDecoder.load(serialized)) { Boolean[] array = new Boolean[dec.size()]; dec.booleans(array.length, (e) -> array[dec.position()] = e); assert (dec.position() == dec.size()); return array; } catch (IOException e) { - throw new DecodingException(e); + throw new CodingException(e); } } - public static Number[] numbers(Object serialized) throws DecodingException { + public static Number[] numbers(Object serialized) throws CodingException { try (SequenceDecoder dec = SequenceDecoder.load(serialized)) { Number[] array = new Number[dec.size()]; dec.numbers(array.length, (e) -> array[dec.position()] = e); assert (dec.position() == dec.size()); return array; } catch (IOException e) { - throw new DecodingException(e); + throw new CodingException(e); } } - public static Object[][] arrays(Object serialized) throws DecodingException { + public static Object[][] arrays(Object serialized) throws CodingException { return arraysWithDecoder(serialized, null, (l) -> new Object[l][]); } public static T[][] arraysWithDecoder(Object serialized, ArrayDecoder decoder, - ArrayCreator arrayCreator) throws DecodingException { + ArrayCreator arrayCreator) throws CodingException { try (SequenceDecoder dec = SequenceDecoder.load(serialized)) { T[][] array = arrayCreator.newArray(dec.size()); dec.arrays(array.length, decoder, (e) -> array[dec.position()] = e); assert (dec.position() == dec.size()); return array; } catch (IOException e) { - throw new DecodingException(e); + throw new CodingException(e); } } } diff --git a/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/StandardArrayEncoders.java b/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/StandardArrayEncoders.java index 0f0db1ac..6b1355a0 100644 --- a/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/StandardArrayEncoders.java +++ b/jacline-lib-common/src/main/java/com/kohlschutter/jacline/lib/coding/StandardArrayEncoders.java @@ -21,7 +21,7 @@ @JsType public class StandardArrayEncoders { - public static Object strings(Object[] array) { + public static Object strings(Object[] array) throws CodingException { SequenceEncoder sec = SequenceEncoder.begin(); for (Object obj : array) { sec.encodeStrings(obj == null ? null : obj.toString()); diff --git a/jacline-lib-common/src/test/java/com/kohlschutter/jacline/lib/coding/HelloWorld.java b/jacline-lib-common/src/test/java/com/kohlschutter/jacline/lib/coding/HelloWorld.java index 567c2a19..feeb44ea 100644 --- a/jacline-lib-common/src/test/java/com/kohlschutter/jacline/lib/coding/HelloWorld.java +++ b/jacline-lib-common/src/test/java/com/kohlschutter/jacline/lib/coding/HelloWorld.java @@ -72,7 +72,7 @@ public void setMessage(String message) { @Override @JsExport - public Object encode(KeyEncoderProvider provider) { + public Object encode(KeyEncoderProvider provider) throws CodingException { KeyEncoder enc = CodingProviders.decorateEncoderProvider(provider).begin(CODED_TYPE); enc.encodeString("message", message); enc.beginEncodeObject("obj", "SomeObjectType").encodeBoolean("indiana", false).encodeNumber( @@ -87,11 +87,11 @@ public Object encode(KeyEncoderProvider provider) { * @param provider The decoder provider, or {@code null} for default. * @param encoded The encoded object. * @return A new {@link HelloWorld} instance. - * @throws DecodingException on error. + * @throws CodingException on error. */ @JsExport public static HelloWorld decode(KeyDecoderProvider provider, Object encoded) - throws DecodingException { + throws CodingException { try (KeyDecoder dec = CodingProviders.decorateDecoderProvider(provider).load(CODED_TYPE, encoded)) { checkSanity(dec); @@ -103,7 +103,7 @@ public static HelloWorld decode(KeyDecoderProvider provider, Object encoded) return hw; } catch (IOException e) { - throw new DecodingException(e); + throw new CodingException(e); } } @@ -113,9 +113,9 @@ public static HelloWorld decode(KeyDecoderProvider provider, Object encoded) * * @param encoded The encoded object. * @return A new {@link HelloWorld} instance. - * @throws DecodingException on error. + * @throws CodingException on error. */ - public static HelloWorld decodeDefault(Object encoded) throws DecodingException { + public static HelloWorld decodeDefault(Object encoded) throws CodingException { return decode(null, encoded); } @@ -124,9 +124,9 @@ public static HelloWorld decodeDefault(Object encoded) throws DecodingException * the target object. * * @param dec The {@link KeyDecoder} instance. - * @throws DecodingException on error. + * @throws CodingException on error. */ - private static void checkSanity(KeyDecoder dec) throws DecodingException { + private static void checkSanity(KeyDecoder dec) throws CodingException { dec.objectForKey("obj", (encoded) -> { KeyDecoder objectDecoder = KeyDecoder.load("SomeObjectType", encoded); @@ -134,9 +134,9 @@ private static void checkSanity(KeyDecoder dec) throws DecodingException { if (Math.abs(3.141 - pi.floatValue()) > 0.001f) { if (objectDecoder.booleanForKey("indiana")) { - throw new DecodingException("Not again, Indiana!"); + throw new CodingException("Not again, Indiana!"); } else { - throw new DecodingException("Not my reality"); + throw new CodingException("Not my reality"); } } return pi.floatValue(); // return value is not used diff --git a/jacline-lib-common/src/test/java/com/kohlschutter/jacline/lib/coding/KeyEncoderTest.java b/jacline-lib-common/src/test/java/com/kohlschutter/jacline/lib/coding/KeyEncoderTest.java index 3ea48fee..e7516b8d 100644 --- a/jacline-lib-common/src/test/java/com/kohlschutter/jacline/lib/coding/KeyEncoderTest.java +++ b/jacline-lib-common/src/test/java/com/kohlschutter/jacline/lib/coding/KeyEncoderTest.java @@ -27,7 +27,7 @@ public class KeyEncoderTest { @Test - public void testEncoder() { + public void testEncoder() throws Exception { KeyEncoder enc = KeyEncoder.begin("Dummy"); enc.encodeString("hello", "world"); enc.beginEncodeObject("obj", null).encodeBoolean("indiana", true).encodeNumber("pi", 4).end() @@ -84,6 +84,6 @@ public void testDecodingError() throws Exception { + "\"javaClass\":\"SomeObjectType\"," + "\"indiana\":true," + "\"pi\":4" + "}," + "\"stringArray\":[\"one\",\"two\",\"mississippi\"]" + "}"; - assertThrows(DecodingException.class, () -> HelloWorld.decodeDefault(json)); + assertThrows(CodingException.class, () -> HelloWorld.decodeDefault(json)); } } diff --git a/jacline-lib-common/src/vanilla/java/com/kohlschutter/jacline/lib/coding/CodingSupportProviderJsonImpl.java b/jacline-lib-common/src/vanilla/java/com/kohlschutter/jacline/lib/coding/CodingSupportProviderJsonImpl.java index 75b09ad8..6717dd3d 100644 --- a/jacline-lib-common/src/vanilla/java/com/kohlschutter/jacline/lib/coding/CodingSupportProviderJsonImpl.java +++ b/jacline-lib-common/src/vanilla/java/com/kohlschutter/jacline/lib/coding/CodingSupportProviderJsonImpl.java @@ -6,7 +6,7 @@ public class CodingSupportProviderJsonImpl implements CodingServiceProvider { @Override - public KeyDecoder keyDecoder(String expectedCodedType, Object encoded) throws DecodingException { + public KeyDecoder keyDecoder(String expectedCodedType, Object encoded) throws CodingException { return new JsonKeyDecoder(expectedCodedType, encoded); } @@ -16,7 +16,7 @@ public KeyEncoder keyEncoder(String type) { } @Override - public SequenceDecoder sequenceDecoder(Object encoded) throws DecodingException { + public SequenceDecoder sequenceDecoder(Object encoded) throws CodingException { return new JsonSequenceDecoder(encoded); } diff --git a/jacline-lib-common/src/vanilla/java/com/kohlschutter/jacline/lib/coding/JsonKeyDecoder.java b/jacline-lib-common/src/vanilla/java/com/kohlschutter/jacline/lib/coding/JsonKeyDecoder.java index 6ef70dca..ab71cab3 100644 --- a/jacline-lib-common/src/vanilla/java/com/kohlschutter/jacline/lib/coding/JsonKeyDecoder.java +++ b/jacline-lib-common/src/vanilla/java/com/kohlschutter/jacline/lib/coding/JsonKeyDecoder.java @@ -35,7 +35,7 @@ public final class JsonKeyDecoder implements KeyDecoder { private final JsonParser parser; private final JsonObject object; - public JsonKeyDecoder(String expectedCodedType, Object encoded) throws DecodingException { + public JsonKeyDecoder(String expectedCodedType, Object encoded) throws CodingException { if (encoded == null) { parser = null; object = null; @@ -51,7 +51,7 @@ public JsonKeyDecoder(String expectedCodedType, Object encoded) throws DecodingE if (parser.hasNext()) { Event next = parser.next(); if (next != Event.START_OBJECT) { - throw new DecodingException("Not an object"); + throw new CodingException("Not an object"); } object = parser.getObject(); } else { @@ -62,7 +62,7 @@ public JsonKeyDecoder(String expectedCodedType, Object encoded) throws DecodingE if (expectedCodedType != null && object != null) { String javaClass = stringForKey("javaClass"); if (!expectedCodedType.equals(javaClass)) { - throw DecodingException.withUnexpectedType(expectedCodedType, javaClass); + throw CodingException.withUnexpectedType(expectedCodedType, javaClass); } } } @@ -88,7 +88,7 @@ public Number numberForKey(String key) { @Override @SuppressWarnings("PMD.ReturnEmptyCollectionRatherThanNull") - public T[] arrayForKey(String key, ArrayDecoder decoder) throws DecodingException { + public T[] arrayForKey(String key, ArrayDecoder decoder) throws CodingException { if (isNull(key)) { return null; } @@ -98,7 +98,7 @@ public T[] arrayForKey(String key, ArrayDecoder decoder) throws DecodingE @SuppressWarnings("null") @Override - public T objectForKey(String key, ObjectDecoder decoder) throws DecodingException { + public T objectForKey(String key, ObjectDecoder decoder) throws CodingException { if (isNull(key)) { return null; } diff --git a/jacline-lib-common/src/vanilla/java/com/kohlschutter/jacline/lib/coding/JsonKeyEncoder.java b/jacline-lib-common/src/vanilla/java/com/kohlschutter/jacline/lib/coding/JsonKeyEncoder.java index 2cd60ecb..dae71973 100644 --- a/jacline-lib-common/src/vanilla/java/com/kohlschutter/jacline/lib/coding/JsonKeyEncoder.java +++ b/jacline-lib-common/src/vanilla/java/com/kohlschutter/jacline/lib/coding/JsonKeyEncoder.java @@ -94,7 +94,8 @@ public KeyEncoder encodeNumber(String key, Number value) { } @Override - public KeyEncoder encodeArray(String key, ArrayEncoder encoder, Object[] array) { + public KeyEncoder encodeArray(String key, ArrayEncoder encoder, Object[] array) + throws CodingException { if (array == null) { builder.addNull(key); } else { diff --git a/jacline-lib-common/src/vanilla/java/com/kohlschutter/jacline/lib/coding/JsonSequenceDecoder.java b/jacline-lib-common/src/vanilla/java/com/kohlschutter/jacline/lib/coding/JsonSequenceDecoder.java index 0136ec7f..4cc55acc 100644 --- a/jacline-lib-common/src/vanilla/java/com/kohlschutter/jacline/lib/coding/JsonSequenceDecoder.java +++ b/jacline-lib-common/src/vanilla/java/com/kohlschutter/jacline/lib/coding/JsonSequenceDecoder.java @@ -42,7 +42,7 @@ public final class JsonSequenceDecoder implements SequenceDecoder { private final int length; private final Iterator iterator; - public JsonSequenceDecoder(Object encoded) throws DecodingException { + public JsonSequenceDecoder(Object encoded) throws CodingException { Stream stream; if (encoded instanceof JsonArray) { @@ -58,7 +58,7 @@ public JsonSequenceDecoder(Object encoded) throws DecodingException { } if (parser.hasNext()) { if (parser.next() != Event.START_ARRAY) { - throw new DecodingException("Not an array"); + throw new CodingException("Not an array"); } JsonArray array = parser.getArray(); length = array.size(); @@ -91,7 +91,7 @@ private int checkCount(int count) { @Override public SequenceDecoder strings(int count, SequenceConsumer forEach) - throws DecodingException { + throws CodingException { count = checkCount(count); for (int i = 0; i < count; i++, pos++) { JsonValue next = iterator.next(); @@ -105,7 +105,7 @@ public SequenceDecoder strings(int count, SequenceConsumer forEach) val = ((JsonString) next).getString(); break; default: - throw new DecodingException("Wrong type: " + next.getValueType() + throw new CodingException("Wrong type: " + next.getValueType() + "; expected array, but got: " + next); } @@ -117,7 +117,7 @@ public SequenceDecoder strings(int count, SequenceConsumer forEach) @Override public SequenceDecoder booleans(int count, SequenceConsumer forEach) - throws DecodingException { + throws CodingException { count = checkCount(count); for (int i = 0; i < count; i++, pos++) { JsonValue next = iterator.next(); @@ -134,7 +134,7 @@ public SequenceDecoder booleans(int count, SequenceConsumer forEach) val = true; break; default: - throw new DecodingException("Wrong type: " + next.getValueType() + throw new CodingException("Wrong type: " + next.getValueType() + "; expected array, but got: " + next); } @@ -146,7 +146,7 @@ public SequenceDecoder booleans(int count, SequenceConsumer forEach) @Override public SequenceDecoder numbers(int count, SequenceConsumer forEach) - throws DecodingException { + throws CodingException { count = checkCount(count); for (int i = 0; i < count; i++, pos++) { JsonValue next = iterator.next(); @@ -160,7 +160,7 @@ public SequenceDecoder numbers(int count, SequenceConsumer forEach) val = ((JsonNumber) next).numberValue(); break; default: - throw new DecodingException("Wrong type: " + next.getValueType() + throw new CodingException("Wrong type: " + next.getValueType() + "; expected array, but got: " + next); } @@ -172,7 +172,7 @@ public SequenceDecoder numbers(int count, SequenceConsumer forEach) @Override public SequenceDecoder arrays(int count, ArrayDecoder decoder, - SequenceConsumer forEach) throws DecodingException { + SequenceConsumer forEach) throws CodingException { count = checkCount(count); for (int i = 0; i < count; i++, pos++) { JsonValue next = iterator.next(); @@ -188,7 +188,7 @@ public SequenceDecoder arrays(int count, ArrayDecoder decoder, break; } default: - throw new DecodingException("Wrong type: " + next.getValueType() + throw new CodingException("Wrong type: " + next.getValueType() + "; expected array, but got: " + next); } @@ -201,7 +201,7 @@ public SequenceDecoder arrays(int count, ArrayDecoder decoder, @SuppressWarnings("null") @Override public SequenceDecoder objects(int count, ObjectDecoder decoder, - SequenceConsumer forEach) throws DecodingException { + SequenceConsumer forEach) throws CodingException { count = checkCount(count); for (int i = 0; i < count; i++, pos++) { JsonValue next = iterator.next(); @@ -217,7 +217,7 @@ public SequenceDecoder objects(int count, ObjectDecoder decoder, break; } default: - throw new DecodingException("Wrong type: " + next.getValueType() + throw new CodingException("Wrong type: " + next.getValueType() + "; expected array, but got: " + next); } diff --git a/jacline-lib-common/src/vanilla/java/com/kohlschutter/jacline/lib/json/VanillaJSON.java b/jacline-lib-common/src/vanilla/java/com/kohlschutter/jacline/lib/json/VanillaJSON.java index 5e3a2a72..8b48f987 100644 --- a/jacline-lib-common/src/vanilla/java/com/kohlschutter/jacline/lib/json/VanillaJSON.java +++ b/jacline-lib-common/src/vanilla/java/com/kohlschutter/jacline/lib/json/VanillaJSON.java @@ -9,6 +9,7 @@ import java.util.Map; import com.kohlschutter.jacline.lib.coding.Codable; +import com.kohlschutter.jacline.lib.coding.CodingException; import com.kohlschutter.jacline.lib.coding.CodingSupportProviderJsonImpl; import com.kohlschutter.jacline.lib.exception.JsException; @@ -49,7 +50,11 @@ public static String stringify(Object obj) { } else if (obj.getClass().isArray()) { return buildArray(toCollection(obj)).toString(); } else if (obj instanceof Codable) { - return ((Codable) obj).encode(JSON_CODING::keyEncoder).toString(); + try { + return ((Codable) obj).encode(JSON_CODING::keyEncoder).toString(); + } catch (CodingException e) { + return obj.toString(); + } } else if (obj instanceof Map) { Map outMap = new HashMap<>(); for (Map.Entry en : ((Map) obj).entrySet()) { diff --git a/samples/json/src/main/java/com/kohlschutter/jacline/samples/json/HelloWorld.java b/samples/json/src/main/java/com/kohlschutter/jacline/samples/json/HelloWorld.java index 254b63b7..6e84ec4f 100644 --- a/samples/json/src/main/java/com/kohlschutter/jacline/samples/json/HelloWorld.java +++ b/samples/json/src/main/java/com/kohlschutter/jacline/samples/json/HelloWorld.java @@ -23,9 +23,9 @@ import com.kohlschutter.jacline.annotations.JsExport; import com.kohlschutter.jacline.annotations.JsServiceProvider; import com.kohlschutter.jacline.lib.coding.Codable; +import com.kohlschutter.jacline.lib.coding.CodingException; import com.kohlschutter.jacline.lib.coding.CodingProviders; import com.kohlschutter.jacline.lib.coding.Decodables; -import com.kohlschutter.jacline.lib.coding.DecodingException; import com.kohlschutter.jacline.lib.coding.KeyDecoder; import com.kohlschutter.jacline.lib.coding.KeyDecoderProvider; import com.kohlschutter.jacline.lib.coding.KeyEncoder; @@ -82,7 +82,7 @@ public void setMessage(String message) { @Override @JsExport - public Object encode(KeyEncoderProvider provider) { + public Object encode(KeyEncoderProvider provider) throws CodingException { KeyEncoder enc = CodingProviders.decorateEncoderProvider(provider).begin(CODED_TYPE); enc.encodeString("message", message); enc.beginEncodeObject("obj", "SomeObjectType").encodeBoolean("indiana", false).encodeNumber( @@ -97,11 +97,10 @@ public Object encode(KeyEncoderProvider provider) { * @param provider The {@link KeyDecoderProvider}, or {@code null} for default. * @param obj The encoded object. * @return A new {@link HelloWorld} instance. - * @throws DecodingException on error. + * @throws CodingException on error. */ @JsExport - public static HelloWorld decode(KeyDecoderProvider provider, Object obj) - throws DecodingException { + public static HelloWorld decode(KeyDecoderProvider provider, Object obj) throws CodingException { try (KeyDecoder dec = CodingProviders.decorateDecoderProvider(provider).load(CODED_TYPE, obj)) { checkSanity(dec); @@ -112,7 +111,7 @@ public static HelloWorld decode(KeyDecoderProvider provider, Object obj) return hw; } catch (IOException e) { - throw new DecodingException(e); + throw new CodingException(e); } } @@ -122,9 +121,9 @@ public static HelloWorld decode(KeyDecoderProvider provider, Object obj) * * @param encoded The encoded object. * @return A new {@link HelloWorld} instance. - * @throws DecodingException on error. + * @throws CodingException on error. */ - public static HelloWorld decodeDefault(Object encoded) throws DecodingException { + public static HelloWorld decodeDefault(Object encoded) throws CodingException { return decode(KeyDecoder::load, encoded); } @@ -133,9 +132,9 @@ public static HelloWorld decodeDefault(Object encoded) throws DecodingException * the target object. * * @param dec The {@link KeyDecoder} instance. - * @throws DecodingException on error. + * @throws CodingException on error. */ - private static void checkSanity(KeyDecoder dec) throws DecodingException { + private static void checkSanity(KeyDecoder dec) throws CodingException { dec.objectForKey("obj", (encoded) -> { KeyDecoder objectDecoder = KeyDecoder.load("SomeObjectType", encoded); @@ -143,9 +142,9 @@ private static void checkSanity(KeyDecoder dec) throws DecodingException { if (Math.abs(3.141 - pi.floatValue()) > 0.001f) { if (objectDecoder.booleanForKey("indiana")) { - throw new DecodingException("Not again, Indiana!"); + throw new CodingException("Not again, Indiana!"); } else { - throw new DecodingException("Not my reality"); + throw new CodingException("Not my reality"); } } return pi.floatValue(); // return value is not used diff --git a/samples/json/src/test/java/com/kohlschutter/jacline/samples/json/HelloWorldTest.java b/samples/json/src/test/java/com/kohlschutter/jacline/samples/json/HelloWorldTest.java index cd6bd1b5..2d878919 100644 --- a/samples/json/src/test/java/com/kohlschutter/jacline/samples/json/HelloWorldTest.java +++ b/samples/json/src/test/java/com/kohlschutter/jacline/samples/json/HelloWorldTest.java @@ -24,7 +24,7 @@ import org.junit.jupiter.api.Test; -import com.kohlschutter.jacline.lib.coding.DecodingException; +import com.kohlschutter.jacline.lib.coding.CodingException; public class HelloWorldTest { @Test @@ -73,6 +73,6 @@ public void testDecodingError() throws Exception { + "\"javaClass\":\"SomeObjectType\"," + "\"indiana\":true," + "\"pi\":4" + "}," + "\"stringArray\":[\"one\",\"two\",\"mississippi\"]" + "}"; - assertThrows(DecodingException.class, () -> HelloWorld.decodeDefault(json)); + assertThrows(CodingException.class, () -> HelloWorld.decodeDefault(json)); } }