dblevins commented on code in PR #84: URL: https://github.com/apache/johnzon/pull/84#discussion_r858239080
########## 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: Though not my preference, I implemented this in the spirit of compromise on the constructors/static method -- 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]
