rmannibucau commented on code in PR #84:
URL: https://github.com/apache/johnzon/pull/84#discussion_r857320207


##########
johnzon-core/src/main/java/org/apache/johnzon/core/Snippet.java:
##########
@@ -0,0 +1,435 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.johnzon.core;
+
+import javax.json.JsonArray;
+import javax.json.JsonObject;
+import javax.json.JsonValue;
+import javax.json.stream.JsonGenerator;
+import javax.json.stream.JsonGeneratorFactory;
+import java.io.ByteArrayOutputStream;
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.UncheckedIOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import static 
org.apache.johnzon.core.JsonGeneratorFactoryImpl.GENERATOR_BUFFER_LENGTH;
+
+/**
+ * Constructs short snippets of serialized JSON text representations of
+ * JsonValue instances in a way that is ideal for error messages.
+ *
+ * Instances of Snippet are thread-safe, reusable and memory-safe.  Snippet
+ * serializes only enough of the json to fill the desired snippet size and
+ * is therefore safe to use regardless of the size of the JsonValue.
+ */
+public class Snippet {
+
+    private final int max;
+    private final JsonGeneratorFactory generatorFactory;
+
+    /**
+     * This constructor should be used only in static or other scenarios were
+     * there is no JsonGeneratorFactory instance in scope.
+     *
+     * @param max the maximum length of the serialized json produced via of()
+     */
+    public Snippet(final int max) {
+        this(max, new JsonGeneratorFactoryImpl(new HashMap<String, Object>() {
+            {
+                this.put(GENERATOR_BUFFER_LENGTH, max);
+            }
+        }));
+    }
+
+    /**
+     * This is the preferred approach to using Snippet in any context where
+     * there is an existing JsonGeneratorFactory in scope.
+     *
+     * @param max the maximum length of the serialized json produced via of()
+     * @param generatorFactory the JsonGeneratorFactory created by the user
+     */
+    public Snippet(final int max, final JsonGeneratorFactory generatorFactory) 
{
+        this.max = max;
+        this.generatorFactory = generatorFactory;
+    }
+
+    /**
+     * Create a serialized json representation of the supplied
+     * JsonValue, truncating the value to the specified max length.
+     * Truncated text appears with a suffix of "..."
+     *
+     * This method is thread safe.
+     * 
+     * @param value the JsonValue to be serialized as json text
+     * @return a potentially truncated json text
+     */
+    public String of(final JsonValue value) {
+        switch (value.getValueType()) {
+            case TRUE: return "true";

Review Comment:
   still think we should handle primitve accordingly the configuration for 
consistency. One impact is when you have some configuration and do some pattern 
matching, if you configure a max size of 3 (dont ask me why but if we don't 
want that we should forbid size < 5 which is fine too) then you expect to be 
able to grok the output with this configuration IMHO so no particular case IMHO.



##########
johnzon-core/src/main/java/org/apache/johnzon/core/Snippet.java:
##########
@@ -0,0 +1,435 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.johnzon.core;
+
+import javax.json.JsonArray;
+import javax.json.JsonObject;
+import javax.json.JsonValue;
+import javax.json.stream.JsonGenerator;
+import javax.json.stream.JsonGeneratorFactory;
+import java.io.ByteArrayOutputStream;
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.UncheckedIOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import static 
org.apache.johnzon.core.JsonGeneratorFactoryImpl.GENERATOR_BUFFER_LENGTH;
+
+/**
+ * Constructs short snippets of serialized JSON text representations of
+ * JsonValue instances in a way that is ideal for error messages.
+ *
+ * Instances of Snippet are thread-safe, reusable and memory-safe.  Snippet
+ * serializes only enough of the json to fill the desired snippet size and
+ * is therefore safe to use regardless of the size of the JsonValue.
+ */
+public class Snippet {
+
+    private final int max;
+    private final JsonGeneratorFactory generatorFactory;
+
+    /**
+     * This constructor should be used only in static or other scenarios were
+     * there is no JsonGeneratorFactory instance in scope.
+     *
+     * @param max the maximum length of the serialized json produced via of()
+     */
+    public Snippet(final int max) {

Review Comment:
   I'd really drop this one since minimum would be to require to copy the 
config (except the buffer size maybe) from the mapper one which is likely 
equivalent to use the mapper one as we do in the other constructor.
   Also note that the buffer size is not important since before using any 
output we should flush it as any "output stream" so this one should drop IMHO.



##########
johnzon-core/src/main/java/org/apache/johnzon/core/Snippet.java:
##########
@@ -0,0 +1,435 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.johnzon.core;
+
+import javax.json.JsonArray;
+import javax.json.JsonObject;
+import javax.json.JsonValue;
+import javax.json.stream.JsonGenerator;
+import javax.json.stream.JsonGeneratorFactory;
+import java.io.ByteArrayOutputStream;
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.UncheckedIOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import static 
org.apache.johnzon.core.JsonGeneratorFactoryImpl.GENERATOR_BUFFER_LENGTH;
+
+/**
+ * Constructs short snippets of serialized JSON text representations of
+ * JsonValue instances in a way that is ideal for error messages.
+ *
+ * Instances of Snippet are thread-safe, reusable and memory-safe.  Snippet
+ * serializes only enough of the json to fill the desired snippet size and
+ * is therefore safe to use regardless of the size of the JsonValue.
+ */
+public class Snippet {
+
+    private final int max;
+    private final JsonGeneratorFactory generatorFactory;
+
+    /**
+     * This constructor should be used only in static or other scenarios were
+     * there is no JsonGeneratorFactory instance in scope.
+     *
+     * @param max the maximum length of the serialized json produced via of()
+     */
+    public Snippet(final int max) {
+        this(max, new JsonGeneratorFactoryImpl(new HashMap<String, Object>() {
+            {
+                this.put(GENERATOR_BUFFER_LENGTH, max);
+            }
+        }));
+    }
+
+    /**
+     * This is the preferred approach to using Snippet in any context where
+     * there is an existing JsonGeneratorFactory in scope.
+     *
+     * @param max the maximum length of the serialized json produced via of()
+     * @param generatorFactory the JsonGeneratorFactory created by the user
+     */
+    public Snippet(final int max, final JsonGeneratorFactory generatorFactory) 
{
+        this.max = max;
+        this.generatorFactory = generatorFactory;
+    }
+
+    /**
+     * Create a serialized json representation of the supplied
+     * JsonValue, truncating the value to the specified max length.
+     * Truncated text appears with a suffix of "..."
+     *
+     * This method is thread safe.
+     * 
+     * @param value the JsonValue to be serialized as json text
+     * @return a potentially truncated json text
+     */
+    public String of(final JsonValue value) {
+        switch (value.getValueType()) {
+            case TRUE: return "true";
+            case FALSE: return "false";
+            case NULL: return "null";
+            default: {
+                try (final Buffer buffer = new Buffer()) {
+                    buffer.write(value);
+                    return buffer.get();
+                }
+            }
+        }
+    }
+
+    /**
+     * Create a serialized json representation of the supplied
+     * JsonValue, truncating the value to the specified max length.
+     * Truncated text appears with a suffix of "..."
+     *
+     * This method is thread safe.
+     *
+     * Avoid using this method in any context where there already
+     * is a JsonGeneratorFactory instance in scope. For those scenarios
+     * use the constructor that accepts a JsonGeneratorFactory instead.
+     *
+     * @param value the JsonValue to be serialized as json text
+     * @param max the maximum length of the serialized json text
+     * @return a potentially truncated json text
+     */
+    public static String of(final JsonValue value, final int max) {
+        return new Snippet(max).of(value);
+    }
+
+    /**
+     * There are several buffers involved in the creation of a json string.
+     * This class carefully manages them all.
+     *
+     * JsonGeneratorImpl with a 64k buffer (by default)
+     * ObjectStreamWriter with an 8k buffer
+     * SnippetOutputStream with a buffer of maxSnippetLength
+     *
+     * As we create json via calling the JsonGenerator it is critical we
+     * flush the work in progress all the way through these buffers and into
+     * the final SnippetOutputStream buffer.
+     *
+     * If we do not, we risk creating up to 64k of json when we may only
+     * need 50 bytes.  We could potentially optimize this code so the
+     * buffer held by JsonGeneratorImpl is also the maxSnippetLength.
+     */
+    class Buffer implements Closeable {
+        private final JsonGenerator generator;
+        private final SnippetOutputStream snippet;
+
+        private Buffer() {
+            this.snippet = new SnippetOutputStream(max);
+            this.generator = generatorFactory.createGenerator(snippet);
+        }
+
+        private void write(final JsonValue value) {
+            if (snippet.terminate()) {
+                return;
+            }
+
+            switch (value.getValueType()) {
+                case ARRAY: {
+                    write(value.asJsonArray());
+                    break;
+                }
+                case OBJECT: {
+                    write(value.asJsonObject());
+                    break;
+                }
+                default: {
+                    generator.write(value);
+                    generator.flush();
+                }
+            }
+        }
+
+        private void write(final JsonArray array) {
+            if (snippet.terminate()) {
+                return;
+            }
+
+            if (array.isEmpty()) {
+                generator.write(array);
+                generator.flush();
+                return;
+            }
+
+            generator.writeStartArray();
+            generator.flush();
+            for (final JsonValue jsonValue : array) {
+                if (snippet.terminate()) {
+                    break;
+                }
+                write(jsonValue);
+            }
+            generator.writeEnd();
+            generator.flush();
+        }
+
+        private void write(final JsonObject object) {
+            if (snippet.terminate()) {
+                return;
+            }
+
+            if (object.isEmpty()) {
+                generator.write(object);
+                generator.flush();
+                return;
+            }
+
+            generator.writeStartObject();
+            generator.flush();
+            for (final Map.Entry<String, JsonValue> entry : object.entrySet()) 
{
+                if (snippet.terminate()) {
+                    break;
+                }
+                write(entry.getKey(), entry.getValue());
+            }
+            generator.writeEnd();
+            generator.flush();
+        }
+
+        private void write(final String name, final JsonValue value) {
+            if (snippet.terminate()) {
+                return;
+            }
+
+            switch (value.getValueType()) {
+                case ARRAY:
+                    generator.writeStartArray(name);
+                    generator.flush();
+                    final JsonArray array = value.asJsonArray();
+                    for (final JsonValue jsonValue : array) {
+                        if (snippet.terminate()) {
+                            break;
+                        }
+                        write(jsonValue);
+                    }
+                    generator.writeEnd();
+                    generator.flush();
+
+                    break;
+                case OBJECT:
+                    generator.writeStartObject(name);
+                    generator.flush();
+                    final JsonObject object = value.asJsonObject();
+                    for (final Map.Entry<String, JsonValue> keyval : 
object.entrySet()) {
+                        if (snippet.terminate()) {
+                            break;
+                        }
+                        write(keyval.getKey(), keyval.getValue());
+                    }
+                    generator.writeEnd();
+                    generator.flush();
+
+                    break;
+                default: {
+                    generator.write(name, value);
+                    generator.flush();
+                }
+            }
+        }
+
+        private String get() {
+            generator.flush();
+            return snippet.isTruncated() ? snippet.get() + "..." : 
snippet.get();
+        }
+
+        @Override
+        public void close() {
+            generator.close();
+        }
+    }
+
+    /**
+     * Specialized OutputStream with three internal states:
+     * Writing, Completed, Truncated.
+     *
+     * When there is still space left for more json, the
+     * state will be Writing
+     *
+     * If the last write brought is exactly to the end of
+     * the max length, the state will be Completed.
+     *
+     * If the last write brought us over the max length, the
+     * state will be Truncated.
+     */
+    static class SnippetOutputStream extends OutputStream {
+
+        private final ByteArrayOutputStream buffer;
+        private OutputStream mode;
+
+        public SnippetOutputStream(final int max) {
+            this.buffer = new ByteArrayOutputStream(Math.min(max, 8192));
+            this.mode = new Writing(max, buffer);
+        }
+
+        public String get() {
+            return buffer.toString();
+        }
+
+        /**
+         * Calling this method implies the need to continue
+         * writing and a question on if that is ok.
+         *
+         * It impacts internal state in the same way as
+         * calling a write method.
+         *
+         * @return true if no more writes are possible
+         */
+        public boolean terminate() {
+            if (mode instanceof Truncated) {
+                return true;
+            }
+
+            if (mode instanceof Completed) {
+                mode = new Truncated();
+                return true;
+            }
+
+            return false;
+        }
+
+        public boolean isTruncated() {
+            return mode instanceof Truncated;
+        }
+
+        @Override
+        public void write(final int b) throws IOException {
+            mode.write(b);
+        }
+
+        @Override
+        public void write(final byte[] b) throws IOException {
+            mode.write(b);
+        }
+
+        @Override
+        public void write(final byte[] b, final int off, final int len) throws 
IOException {
+            mode.write(b, off, len);
+        }
+
+        @Override
+        public void flush() throws IOException {
+            mode.flush();
+        }
+
+        @Override
+        public void close() throws IOException {
+            mode.close();
+        }
+
+        public void print(final String string) {

Review Comment:
   unused? let's drop it?
   
   side note: we shouldn't call getBytes() without the encoding anywhere there 
so since it is the single location doing it, dropping it solves it ;)



##########
johnzon-core/src/main/java/org/apache/johnzon/core/Snippet.java:
##########
@@ -0,0 +1,435 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.johnzon.core;
+
+import javax.json.JsonArray;
+import javax.json.JsonObject;
+import javax.json.JsonValue;
+import javax.json.stream.JsonGenerator;
+import javax.json.stream.JsonGeneratorFactory;
+import java.io.ByteArrayOutputStream;
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.UncheckedIOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import static 
org.apache.johnzon.core.JsonGeneratorFactoryImpl.GENERATOR_BUFFER_LENGTH;
+
+/**
+ * Constructs short snippets of serialized JSON text representations of
+ * JsonValue instances in a way that is ideal for error messages.
+ *
+ * Instances of Snippet are thread-safe, reusable and memory-safe.  Snippet
+ * serializes only enough of the json to fill the desired snippet size and
+ * is therefore safe to use regardless of the size of the JsonValue.
+ */
+public class Snippet {
+
+    private final int max;
+    private final JsonGeneratorFactory generatorFactory;
+
+    /**
+     * This constructor should be used only in static or other scenarios were
+     * there is no JsonGeneratorFactory instance in scope.
+     *
+     * @param max the maximum length of the serialized json produced via of()
+     */
+    public Snippet(final int max) {
+        this(max, new JsonGeneratorFactoryImpl(new HashMap<String, Object>() {
+            {
+                this.put(GENERATOR_BUFFER_LENGTH, max);
+            }
+        }));
+    }
+
+    /**
+     * This is the preferred approach to using Snippet in any context where
+     * there is an existing JsonGeneratorFactory in scope.
+     *
+     * @param max the maximum length of the serialized json produced via of()
+     * @param generatorFactory the JsonGeneratorFactory created by the user
+     */
+    public Snippet(final int max, final JsonGeneratorFactory generatorFactory) 
{
+        this.max = max;
+        this.generatorFactory = generatorFactory;
+    }
+
+    /**
+     * Create a serialized json representation of the supplied
+     * JsonValue, truncating the value to the specified max length.
+     * Truncated text appears with a suffix of "..."
+     *
+     * This method is thread safe.
+     * 
+     * @param value the JsonValue to be serialized as json text
+     * @return a potentially truncated json text
+     */
+    public String of(final JsonValue value) {
+        switch (value.getValueType()) {
+            case TRUE: return "true";
+            case FALSE: return "false";
+            case NULL: return "null";
+            default: {
+                try (final Buffer buffer = new Buffer()) {
+                    buffer.write(value);
+                    return buffer.get();
+                }
+            }
+        }
+    }
+
+    /**
+     * Create a serialized json representation of the supplied
+     * JsonValue, truncating the value to the specified max length.
+     * Truncated text appears with a suffix of "..."
+     *
+     * This method is thread safe.
+     *
+     * Avoid using this method in any context where there already
+     * is a JsonGeneratorFactory instance in scope. For those scenarios
+     * use the constructor that accepts a JsonGeneratorFactory instead.
+     *
+     * @param value the JsonValue to be serialized as json text
+     * @param max the maximum length of the serialized json text
+     * @return a potentially truncated json text
+     */
+    public static String of(final JsonValue value, final int max) {
+        return new Snippet(max).of(value);
+    }
+
+    /**
+     * There are several buffers involved in the creation of a json string.
+     * This class carefully manages them all.
+     *
+     * JsonGeneratorImpl with a 64k buffer (by default)
+     * ObjectStreamWriter with an 8k buffer
+     * SnippetOutputStream with a buffer of maxSnippetLength
+     *
+     * As we create json via calling the JsonGenerator it is critical we
+     * flush the work in progress all the way through these buffers and into
+     * the final SnippetOutputStream buffer.
+     *
+     * If we do not, we risk creating up to 64k of json when we may only
+     * need 50 bytes.  We could potentially optimize this code so the
+     * buffer held by JsonGeneratorImpl is also the maxSnippetLength.
+     */
+    class Buffer implements Closeable {
+        private final JsonGenerator generator;
+        private final SnippetOutputStream snippet;
+
+        private Buffer() {
+            this.snippet = new SnippetOutputStream(max);
+            this.generator = generatorFactory.createGenerator(snippet);
+        }
+
+        private void write(final JsonValue value) {
+            if (snippet.terminate()) {
+                return;
+            }
+
+            switch (value.getValueType()) {
+                case ARRAY: {
+                    write(value.asJsonArray());
+                    break;
+                }
+                case OBJECT: {
+                    write(value.asJsonObject());
+                    break;
+                }
+                default: {
+                    generator.write(value);
+                    generator.flush();
+                }
+            }
+        }
+
+        private void write(final JsonArray array) {
+            if (snippet.terminate()) {
+                return;
+            }
+
+            if (array.isEmpty()) {
+                generator.write(array);
+                generator.flush();
+                return;
+            }
+
+            generator.writeStartArray();
+            generator.flush();
+            for (final JsonValue jsonValue : array) {
+                if (snippet.terminate()) {
+                    break;
+                }
+                write(jsonValue);
+            }
+            generator.writeEnd();
+            generator.flush();
+        }
+
+        private void write(final JsonObject object) {
+            if (snippet.terminate()) {
+                return;
+            }
+
+            if (object.isEmpty()) {
+                generator.write(object);
+                generator.flush();
+                return;
+            }
+
+            generator.writeStartObject();
+            generator.flush();
+            for (final Map.Entry<String, JsonValue> entry : object.entrySet()) 
{
+                if (snippet.terminate()) {
+                    break;
+                }
+                write(entry.getKey(), entry.getValue());
+            }
+            generator.writeEnd();
+            generator.flush();
+        }
+
+        private void write(final String name, final JsonValue value) {
+            if (snippet.terminate()) {
+                return;
+            }
+
+            switch (value.getValueType()) {
+                case ARRAY:
+                    generator.writeStartArray(name);
+                    generator.flush();
+                    final JsonArray array = value.asJsonArray();
+                    for (final JsonValue jsonValue : array) {
+                        if (snippet.terminate()) {
+                            break;
+                        }
+                        write(jsonValue);
+                    }
+                    generator.writeEnd();
+                    generator.flush();
+
+                    break;
+                case OBJECT:
+                    generator.writeStartObject(name);
+                    generator.flush();
+                    final JsonObject object = value.asJsonObject();
+                    for (final Map.Entry<String, JsonValue> keyval : 
object.entrySet()) {
+                        if (snippet.terminate()) {
+                            break;
+                        }
+                        write(keyval.getKey(), keyval.getValue());
+                    }
+                    generator.writeEnd();
+                    generator.flush();
+
+                    break;
+                default: {
+                    generator.write(name, value);
+                    generator.flush();
+                }
+            }
+        }
+
+        private String get() {
+            generator.flush();
+            return snippet.isTruncated() ? snippet.get() + "..." : 
snippet.get();
+        }
+
+        @Override
+        public void close() {
+            generator.close();
+        }
+    }
+
+    /**
+     * Specialized OutputStream with three internal states:
+     * Writing, Completed, Truncated.
+     *
+     * When there is still space left for more json, the
+     * state will be Writing
+     *
+     * If the last write brought is exactly to the end of
+     * the max length, the state will be Completed.
+     *
+     * If the last write brought us over the max length, the
+     * state will be Truncated.
+     */
+    static class SnippetOutputStream extends OutputStream {
+
+        private final ByteArrayOutputStream buffer;
+        private OutputStream mode;
+
+        public SnippetOutputStream(final int max) {
+            this.buffer = new ByteArrayOutputStream(Math.min(max, 8192));
+            this.mode = new Writing(max, buffer);
+        }
+
+        public String get() {
+            return buffer.toString();
+        }
+
+        /**
+         * Calling this method implies the need to continue
+         * writing and a question on if that is ok.
+         *
+         * It impacts internal state in the same way as
+         * calling a write method.
+         *
+         * @return true if no more writes are possible
+         */
+        public boolean terminate() {

Review Comment:
   Like this rework, way more explicit!



##########
johnzon-core/src/main/java/org/apache/johnzon/core/Snippet.java:
##########
@@ -0,0 +1,435 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.johnzon.core;
+
+import javax.json.JsonArray;
+import javax.json.JsonObject;
+import javax.json.JsonValue;
+import javax.json.stream.JsonGenerator;
+import javax.json.stream.JsonGeneratorFactory;
+import java.io.ByteArrayOutputStream;
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.UncheckedIOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import static 
org.apache.johnzon.core.JsonGeneratorFactoryImpl.GENERATOR_BUFFER_LENGTH;
+
+/**
+ * Constructs short snippets of serialized JSON text representations of
+ * JsonValue instances in a way that is ideal for error messages.
+ *
+ * Instances of Snippet are thread-safe, reusable and memory-safe.  Snippet
+ * serializes only enough of the json to fill the desired snippet size and
+ * is therefore safe to use regardless of the size of the JsonValue.
+ */
+public class Snippet {
+
+    private final int max;
+    private final JsonGeneratorFactory generatorFactory;
+
+    /**
+     * This constructor should be used only in static or other scenarios were
+     * there is no JsonGeneratorFactory instance in scope.
+     *
+     * @param max the maximum length of the serialized json produced via of()
+     */
+    public Snippet(final int max) {
+        this(max, new JsonGeneratorFactoryImpl(new HashMap<String, Object>() {
+            {
+                this.put(GENERATOR_BUFFER_LENGTH, max);
+            }
+        }));
+    }
+
+    /**
+     * This is the preferred approach to using Snippet in any context where
+     * there is an existing JsonGeneratorFactory in scope.
+     *
+     * @param max the maximum length of the serialized json produced via of()
+     * @param generatorFactory the JsonGeneratorFactory created by the user
+     */
+    public Snippet(final int max, final JsonGeneratorFactory generatorFactory) 
{
+        this.max = max;
+        this.generatorFactory = generatorFactory;
+    }
+
+    /**
+     * Create a serialized json representation of the supplied
+     * JsonValue, truncating the value to the specified max length.
+     * Truncated text appears with a suffix of "..."
+     *
+     * This method is thread safe.
+     * 
+     * @param value the JsonValue to be serialized as json text
+     * @return a potentially truncated json text
+     */
+    public String of(final JsonValue value) {
+        switch (value.getValueType()) {
+            case TRUE: return "true";
+            case FALSE: return "false";
+            case NULL: return "null";
+            default: {
+                try (final Buffer buffer = new Buffer()) {
+                    buffer.write(value);
+                    return buffer.get();
+                }
+            }
+        }
+    }
+
+    /**
+     * Create a serialized json representation of the supplied
+     * JsonValue, truncating the value to the specified max length.
+     * Truncated text appears with a suffix of "..."
+     *
+     * This method is thread safe.
+     *
+     * Avoid using this method in any context where there already
+     * is a JsonGeneratorFactory instance in scope. For those scenarios
+     * use the constructor that accepts a JsonGeneratorFactory instead.
+     *
+     * @param value the JsonValue to be serialized as json text
+     * @param max the maximum length of the serialized json text
+     * @return a potentially truncated json text
+     */
+    public static String of(final JsonValue value, final int max) {
+        return new Snippet(max).of(value);
+    }
+
+    /**
+     * There are several buffers involved in the creation of a json string.
+     * This class carefully manages them all.
+     *
+     * JsonGeneratorImpl with a 64k buffer (by default)
+     * ObjectStreamWriter with an 8k buffer
+     * SnippetOutputStream with a buffer of maxSnippetLength
+     *
+     * As we create json via calling the JsonGenerator it is critical we
+     * flush the work in progress all the way through these buffers and into
+     * the final SnippetOutputStream buffer.
+     *
+     * If we do not, we risk creating up to 64k of json when we may only
+     * need 50 bytes.  We could potentially optimize this code so the
+     * buffer held by JsonGeneratorImpl is also the maxSnippetLength.
+     */
+    class Buffer implements Closeable {
+        private final JsonGenerator generator;
+        private final SnippetOutputStream snippet;
+
+        private Buffer() {
+            this.snippet = new SnippetOutputStream(max);
+            this.generator = generatorFactory.createGenerator(snippet);
+        }
+
+        private void write(final JsonValue value) {
+            if (snippet.terminate()) {
+                return;
+            }
+
+            switch (value.getValueType()) {
+                case ARRAY: {
+                    write(value.asJsonArray());
+                    break;
+                }
+                case OBJECT: {
+                    write(value.asJsonObject());
+                    break;
+                }
+                default: {
+                    generator.write(value);
+                    generator.flush();
+                }
+            }
+        }
+
+        private void write(final JsonArray array) {
+            if (snippet.terminate()) {
+                return;
+            }
+
+            if (array.isEmpty()) {
+                generator.write(array);
+                generator.flush();
+                return;
+            }
+
+            generator.writeStartArray();
+            generator.flush();

Review Comment:
   no need to flush *anywhere* (will not copy the comment on each line since it 
seems you got gluttonny ;))



##########
johnzon-core/src/main/java/org/apache/johnzon/core/Snippet.java:
##########
@@ -0,0 +1,435 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.johnzon.core;
+
+import javax.json.JsonArray;
+import javax.json.JsonObject;
+import javax.json.JsonValue;
+import javax.json.stream.JsonGenerator;
+import javax.json.stream.JsonGeneratorFactory;
+import java.io.ByteArrayOutputStream;
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.UncheckedIOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import static 
org.apache.johnzon.core.JsonGeneratorFactoryImpl.GENERATOR_BUFFER_LENGTH;
+
+/**
+ * Constructs short snippets of serialized JSON text representations of
+ * JsonValue instances in a way that is ideal for error messages.
+ *
+ * Instances of Snippet are thread-safe, reusable and memory-safe.  Snippet
+ * serializes only enough of the json to fill the desired snippet size and
+ * is therefore safe to use regardless of the size of the JsonValue.
+ */
+public class Snippet {
+
+    private final int max;
+    private final JsonGeneratorFactory generatorFactory;
+
+    /**
+     * This constructor should be used only in static or other scenarios were
+     * there is no JsonGeneratorFactory instance in scope.
+     *
+     * @param max the maximum length of the serialized json produced via of()
+     */
+    public Snippet(final int max) {
+        this(max, new JsonGeneratorFactoryImpl(new HashMap<String, Object>() {
+            {
+                this.put(GENERATOR_BUFFER_LENGTH, max);
+            }
+        }));
+    }
+
+    /**
+     * This is the preferred approach to using Snippet in any context where
+     * there is an existing JsonGeneratorFactory in scope.
+     *
+     * @param max the maximum length of the serialized json produced via of()
+     * @param generatorFactory the JsonGeneratorFactory created by the user
+     */
+    public Snippet(final int max, final JsonGeneratorFactory generatorFactory) 
{
+        this.max = max;
+        this.generatorFactory = generatorFactory;
+    }
+
+    /**
+     * Create a serialized json representation of the supplied
+     * JsonValue, truncating the value to the specified max length.
+     * Truncated text appears with a suffix of "..."
+     *
+     * This method is thread safe.
+     * 
+     * @param value the JsonValue to be serialized as json text
+     * @return a potentially truncated json text
+     */
+    public String of(final JsonValue value) {
+        switch (value.getValueType()) {
+            case TRUE: return "true";
+            case FALSE: return "false";
+            case NULL: return "null";
+            default: {
+                try (final Buffer buffer = new Buffer()) {
+                    buffer.write(value);
+                    return buffer.get();
+                }
+            }
+        }
+    }
+
+    /**
+     * Create a serialized json representation of the supplied
+     * JsonValue, truncating the value to the specified max length.
+     * Truncated text appears with a suffix of "..."
+     *
+     * This method is thread safe.
+     *
+     * Avoid using this method in any context where there already
+     * is a JsonGeneratorFactory instance in scope. For those scenarios
+     * use the constructor that accepts a JsonGeneratorFactory instead.
+     *
+     * @param value the JsonValue to be serialized as json text
+     * @param max the maximum length of the serialized json text
+     * @return a potentially truncated json text
+     */
+    public static String of(final JsonValue value, final int max) {
+        return new Snippet(max).of(value);
+    }
+
+    /**
+     * There are several buffers involved in the creation of a json string.
+     * This class carefully manages them all.
+     *
+     * JsonGeneratorImpl with a 64k buffer (by default)
+     * ObjectStreamWriter with an 8k buffer
+     * SnippetOutputStream with a buffer of maxSnippetLength
+     *
+     * As we create json via calling the JsonGenerator it is critical we
+     * flush the work in progress all the way through these buffers and into
+     * the final SnippetOutputStream buffer.
+     *
+     * If we do not, we risk creating up to 64k of json when we may only
+     * need 50 bytes.  We could potentially optimize this code so the
+     * buffer held by JsonGeneratorImpl is also the maxSnippetLength.
+     */
+    class Buffer implements Closeable {
+        private final JsonGenerator generator;
+        private final SnippetOutputStream snippet;
+
+        private Buffer() {
+            this.snippet = new SnippetOutputStream(max);
+            this.generator = generatorFactory.createGenerator(snippet);
+        }
+
+        private void write(final JsonValue value) {
+            if (snippet.terminate()) {
+                return;
+            }
+
+            switch (value.getValueType()) {
+                case ARRAY: {
+                    write(value.asJsonArray());
+                    break;
+                }
+                case OBJECT: {
+                    write(value.asJsonObject());
+                    break;
+                }
+                default: {
+                    generator.write(value);
+                    generator.flush();
+                }
+            }
+        }
+
+        private void write(final JsonArray array) {
+            if (snippet.terminate()) {
+                return;
+            }
+
+            if (array.isEmpty()) {
+                generator.write(array);
+                generator.flush();
+                return;
+            }
+
+            generator.writeStartArray();
+            generator.flush();
+            for (final JsonValue jsonValue : array) {
+                if (snippet.terminate()) {
+                    break;
+                }
+                write(jsonValue);
+            }
+            generator.writeEnd();
+            generator.flush();
+        }
+
+        private void write(final JsonObject object) {
+            if (snippet.terminate()) {
+                return;
+            }
+
+            if (object.isEmpty()) {
+                generator.write(object);
+                generator.flush();
+                return;
+            }
+
+            generator.writeStartObject();
+            generator.flush();
+            for (final Map.Entry<String, JsonValue> entry : object.entrySet()) 
{
+                if (snippet.terminate()) {
+                    break;
+                }
+                write(entry.getKey(), entry.getValue());
+            }
+            generator.writeEnd();
+            generator.flush();
+        }
+
+        private void write(final String name, final JsonValue value) {
+            if (snippet.terminate()) {
+                return;
+            }
+
+            switch (value.getValueType()) {
+                case ARRAY:
+                    generator.writeStartArray(name);
+                    generator.flush();
+                    final JsonArray array = value.asJsonArray();
+                    for (final JsonValue jsonValue : array) {
+                        if (snippet.terminate()) {
+                            break;
+                        }
+                        write(jsonValue);
+                    }
+                    generator.writeEnd();
+                    generator.flush();
+
+                    break;
+                case OBJECT:
+                    generator.writeStartObject(name);
+                    generator.flush();
+                    final JsonObject object = value.asJsonObject();
+                    for (final Map.Entry<String, JsonValue> keyval : 
object.entrySet()) {
+                        if (snippet.terminate()) {
+                            break;
+                        }
+                        write(keyval.getKey(), keyval.getValue());
+                    }
+                    generator.writeEnd();
+                    generator.flush();
+
+                    break;
+                default: {
+                    generator.write(name, value);
+                    generator.flush();
+                }
+            }
+        }
+
+        private String get() {

Review Comment:
   your flush issue comes from there, let's drop the flush and call get after 
the auto-close in 
https://github.com/apache/johnzon/pull/84/files#diff-f8878dc73ad8ecae625a2bf13a77b5718505c77f547c31cefc2454e42454138aR113
 which should be written as:
   
   ```
       public static String of(final JsonValue value, final int max) {
           final Snippet snippet = new Snippet(max);
           try (final Snippet s  = snippet) { // use the style you want but the 
important part is to close before using the in mem stream
                s.of(value)
           }
           return s.get();
       }
   ```
   
   side note: without that, the generator.close is not called and we leak as 
mentionned in early comments



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to