diff --git a/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java b/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java index 18c5d6000a..b6a718c368 100644 --- a/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java +++ b/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java @@ -3,6 +3,7 @@ import java.io.*; import java.lang.reflect.Type; import java.net.URL; +import java.nio.file.Path; import java.text.DateFormat; import java.util.Collection; import java.util.List; @@ -583,6 +584,20 @@ public JsonParser createParser(File src) throws JacksonException { return ctxt.assignAndReturnParser(_streamFactory.createParser(ctxt, src)); } + /** + * Factory method for constructing {@link JsonParser} that is properly + * wired to allow callbacks for deserialization: basically + * constructs a {@link ObjectReadContext} and then calls + * {@link TokenStreamFactory#createParser(ObjectReadContext,Path)}. + * + * @since 3.0 + */ + public JsonParser createParser(Path src) throws JacksonException { + _assertNotNull("src", src); + DeserializationContextExt ctxt = _deserializationContext(); + return ctxt.assignAndReturnParser(_streamFactory.createParser(ctxt, src)); + } + /** * Factory method for constructing {@link JsonParser} that is properly * wired to allow callbacks for deserialization: basically @@ -781,6 +796,19 @@ public JsonGenerator createGenerator(File f, JsonEncoding enc) throws JacksonExc return _streamFactory.createGenerator(_serializerProvider(), f, enc); } + /** + * Factory method for constructing {@link JsonGenerator} that is properly + * wired to allow callbacks for serialization: basically + * constructs a {@link ObjectWriteContext} and then calls + * {@link TokenStreamFactory#createGenerator(ObjectWriteContext,Path,JsonEncoding)}. + * + * @since 3.0 + */ + public JsonGenerator createGenerator(Path p, JsonEncoding enc) throws JacksonException { + _assertNotNull("p", p); + return _streamFactory.createGenerator(_serializerProvider(), p, enc); + } + /** * Factory method for constructing {@link JsonGenerator} that is properly * wired to allow callbacks for serialization: basically @@ -1151,6 +1179,19 @@ public JsonNode readTree(File file) throws JacksonException return _readTreeAndClose(ctxt, _streamFactory.createParser(ctxt, file)); } + /** + * Same as {@link #readTree(InputStream)} except content read from + * passed-in {@link Path}. + * + * @since 3.0 + */ + public JsonNode readTree(Path path) throws JacksonException + { + _assertNotNull("path", path); + DeserializationContextExt ctxt = _deserializationContext(); + return _readTreeAndClose(ctxt, _streamFactory.createParser(ctxt, path)); + } + /** * Same as {@link #readTree(InputStream)} except content read from * passed-in {@link URL}. @@ -1365,6 +1406,74 @@ public T readValue(File src, JavaType valueType) throws JacksonException return (T) _readMapAndClose(ctxt, _streamFactory.createParser(ctxt, src), valueType); } + /** + * Method to deserialize JSON content from given path into given Java type. + * + * @throws WrappedIOException if a low-level I/O problem (unexpected end-of-input, + * network error) occurs (passed through as-is without additional wrapping -- note + * that this is one case where {@link DeserializationFeature#WRAP_EXCEPTIONS} + * does NOT result in wrapping of exception even if enabled) + * @throws StreamReadException if underlying input contains invalid content + * of type {@link JsonParser} supports (JSON for default case) + * @throws DatabindException if the input JSON structure does not match structure + * expected for result type (or has other mismatch issues) + * + * @since 3.0 + */ + @SuppressWarnings("unchecked") + public T readValue(Path src, Class valueType) throws JacksonException + { + _assertNotNull("src", src); + DeserializationContextExt ctxt = _deserializationContext(); + return (T) _readMapAndClose(ctxt, _streamFactory.createParser(ctxt, src), + _typeFactory.constructType(valueType)); + } + + /** + * Method to deserialize JSON content from given path into given Java type. + * + * @throws WrappedIOException if a low-level I/O problem (unexpected end-of-input, + * network error) occurs (passed through as-is without additional wrapping -- note + * that this is one case where {@link DeserializationFeature#WRAP_EXCEPTIONS} + * does NOT result in wrapping of exception even if enabled) + * @throws StreamReadException if underlying input contains invalid content + * of type {@link JsonParser} supports (JSON for default case) + * @throws DatabindException if the input JSON structure does not match structure + * expected for result type (or has other mismatch issues) + * + * @since 3.0 + */ + @SuppressWarnings({ "unchecked" }) + public T readValue(Path src, TypeReference valueTypeRef) throws JacksonException + { + _assertNotNull("src", src); + DeserializationContextExt ctxt = _deserializationContext(); + return (T) _readMapAndClose(ctxt, _streamFactory.createParser(ctxt, src), + _typeFactory.constructType(valueTypeRef)); + } + + /** + * Method to deserialize JSON content from given path into given Java type. + * + * @throws WrappedIOException if a low-level I/O problem (unexpected end-of-input, + * network error) occurs (passed through as-is without additional wrapping -- note + * that this is one case where {@link DeserializationFeature#WRAP_EXCEPTIONS} + * does NOT result in wrapping of exception even if enabled) + * @throws StreamReadException if underlying input contains invalid content + * of type {@link JsonParser} supports (JSON for default case) + * @throws DatabindException if the input JSON structure does not match structure + * expected for result type (or has other mismatch issues) + * + * @since 3.0 + */ + @SuppressWarnings("unchecked") + public T readValue(Path src, JavaType valueType) throws JacksonException + { + _assertNotNull("src", src); + DeserializationContextExt ctxt = _deserializationContext(); + return (T) _readMapAndClose(ctxt, _streamFactory.createParser(ctxt, src), valueType); + } + /** * Method to deserialize JSON content from given resource into given Java type. *

@@ -1610,6 +1719,15 @@ public T readValue(DataInput src, JavaType valueType) throws JacksonExceptio _streamFactory.createParser(ctxt, src), valueType); } + @SuppressWarnings("unchecked") + public T readValue(DataInput src, TypeReference valueTypeRef) throws JacksonException + { + _assertNotNull("src", src); + DeserializationContextExt ctxt = _deserializationContext(); + return (T) _readMapAndClose(ctxt, + _streamFactory.createParser(ctxt, src), _typeFactory.constructType(valueTypeRef)); + } + /* /********************************************************************** /* Public API: serialization (mapping from Java types to external format) @@ -1628,6 +1746,20 @@ public void writeValue(File file, Object value) throws JacksonException _streamFactory.createGenerator(prov, file, JsonEncoding.UTF8), value); } + /** + * Method that can be used to serialize any Java value as + * JSON output, written to Path provided. + * + * @since 3.0 + */ + public void writeValue(Path path, Object value) throws JacksonException + { + _assertNotNull("path", path); + SerializationContextExt prov = _serializerProvider(); + _configAndWriteValue(prov, + _streamFactory.createGenerator(prov, path, JsonEncoding.UTF8), value); + } + /** * Method that can be used to serialize any Java value as * JSON output, using output stream provided (using encoding diff --git a/src/main/java/com/fasterxml/jackson/databind/ObjectReader.java b/src/main/java/com/fasterxml/jackson/databind/ObjectReader.java index f60a757d91..2e68aa84ef 100644 --- a/src/main/java/com/fasterxml/jackson/databind/ObjectReader.java +++ b/src/main/java/com/fasterxml/jackson/databind/ObjectReader.java @@ -2,6 +2,8 @@ import java.io.*; import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.*; import java.util.concurrent.ConcurrentHashMap; @@ -773,6 +775,20 @@ public JsonParser createParser(File src) throws JacksonException { return ctxt.assignAndReturnParser(_parserFactory.createParser(ctxt, src)); } + /** + * Factory method for constructing {@link JsonParser} that is properly + * wired to allow callbacks for deserialization: basically + * constructs a {@link ObjectReadContext} and then calls + * {@link TokenStreamFactory#createParser(ObjectReadContext,Path)}. + * + * @since 3.0 + */ + public JsonParser createParser(Path src) throws JacksonException { + _assertNotNull("src", src); + DeserializationContextExt ctxt = _deserializationContext(); + return ctxt.assignAndReturnParser(_parserFactory.createParser(ctxt, src)); + } + /** * Factory method for constructing {@link JsonParser} that is properly * wired to allow callbacks for deserialization: basically @@ -1202,6 +1218,25 @@ public T readValue(File f) throws JacksonException _considerFilter(_parserFactory.createParser(ctxt, f), false)); } + /** + * Method that binds content read from given {@link Path} + * using configuration of this reader. + * Value return is either newly constructed, or root value that + * was specified with {@link #withValueToUpdate(Object)}. + * + * @param p Path that contains content to read + * + * @since 3.0 + */ + @SuppressWarnings("unchecked") + public T readValue(Path p) throws JacksonException + { + _assertNotNull("p", p); + DeserializationContextExt ctxt = _deserializationContext(); + return (T) _bindAndClose(ctxt, + _considerFilter(_parserFactory.createParser(ctxt, p), false)); + } + /** * Method that binds content read from given input source, * using configuration of this reader. @@ -1456,6 +1491,19 @@ public MappingIterator readValues(File src) throws JacksonException _considerFilter(_parserFactory.createParser(ctxt, src), true)); } + /** + * Overloaded version of {@link #readValues(InputStream)}. + * + * @since 3.0 + */ + public MappingIterator readValues(Path src) throws JacksonException + { + _assertNotNull("src", src); + DeserializationContextExt ctxt = _deserializationContext(); + return _bindAndReadValues(ctxt, + _considerFilter(_parserFactory.createParser(ctxt, src), true)); + } + /** * Overloaded version of {@link #readValue(InputStream)}. *

@@ -1701,6 +1749,14 @@ protected InputStream _inputStream(File f) throws JacksonException { } } + protected InputStream _inputStream(Path p) throws JacksonException { + try { + return Files.newInputStream(p); + } catch (IOException e) { + throw WrappedIOException.construct(e); + } + } + protected final void _assertNotNull(String paramName, Object src) { if (src == null){ throw new IllegalArgumentException(String.format("argument \"%s\" is null", paramName)); diff --git a/src/main/java/com/fasterxml/jackson/databind/ObjectWriter.java b/src/main/java/com/fasterxml/jackson/databind/ObjectWriter.java index 48681388c5..c58a630401 100644 --- a/src/main/java/com/fasterxml/jackson/databind/ObjectWriter.java +++ b/src/main/java/com/fasterxml/jackson/databind/ObjectWriter.java @@ -1,6 +1,7 @@ package com.fasterxml.jackson.databind; import java.io.*; +import java.nio.file.Path; import java.text.*; import java.util.Locale; import java.util.Map; @@ -569,6 +570,19 @@ public JsonGenerator createGenerator(File target, JsonEncoding enc) { return _generatorFactory.createGenerator(_serializerProvider(), target, enc); } + /** + * Factory method for constructing {@link JsonGenerator} that is properly + * wired to allow callbacks for serialization: basically + * constructs a {@link ObjectWriteContext} and then calls + * {@link TokenStreamFactory#createGenerator(ObjectWriteContext,Path,JsonEncoding)}. + * + * @since 3.0 + */ + public JsonGenerator createGenerator(Path target, JsonEncoding enc) { + _assertNotNull("target", target); + return _generatorFactory.createGenerator(_serializerProvider(), target, enc); + } + /** * Factory method for constructing {@link JsonGenerator} that is properly * wired to allow callbacks for serialization: basically @@ -622,6 +636,28 @@ public SequenceWriter writeValues(File target) _generatorFactory.createGenerator(ctxt, target, JsonEncoding.UTF8), true); } + /** + * Method for creating a {@link SequenceWriter} to write a sequence of root + * values using configuration of this {@link ObjectWriter}. + * Sequence is not surrounded by JSON array; some backend types may not + * support writing of such sequences as root level. + * Resulting writer needs to be {@link SequenceWriter#close()}d after all + * values have been written to ensure closing of underlying generator and + * output stream. + * + * @param target Target path to write value sequence to. + * + * @since 3.0 + */ + public SequenceWriter writeValues(Path target) + throws JacksonException + { + _assertNotNull("target", target); + SerializationContextExt ctxt = _serializerProvider(); + return _newSequenceWriter(ctxt, false, + _generatorFactory.createGenerator(ctxt, target, JsonEncoding.UTF8), true); + } + /** * Method for creating a {@link SequenceWriter} to write a sequence of root * values using configuration of this {@link ObjectWriter}. @@ -707,6 +743,30 @@ public SequenceWriter writeValuesAsArray(File target) _generatorFactory.createGenerator(ctxt, target, JsonEncoding.UTF8), true); } + /** + * Method for creating a {@link SequenceWriter} to write an array of + * root-level values, using configuration of this {@link ObjectWriter}. + * Resulting writer needs to be {@link SequenceWriter#close()}d after all + * values have been written to ensure closing of underlying generator and + * output stream. + *

+ * Note that the type to use with {@link ObjectWriter#forType(Class)} needs to + * be type of individual values (elements) to write and NOT matching array + * or {@link java.util.Collection} type. + * + * @param target Path to write token stream to + * + * @since 3.0 + */ + public SequenceWriter writeValuesAsArray(Path target) + throws JacksonException + { + _assertNotNull("target", target); + SerializationContextExt ctxt = _serializerProvider(); + return _newSequenceWriter(ctxt, true, + _generatorFactory.createGenerator(ctxt, target, JsonEncoding.UTF8), true); + } + /** * Method for creating a {@link SequenceWriter} to write an array of * root-level values, using configuration of this {@link ObjectWriter}. @@ -891,6 +951,21 @@ public void writeValue(File target, Object value) _generatorFactory.createGenerator(ctxt, target, JsonEncoding.UTF8), value); } + /** + * Method that can be used to serialize any Java value as + * JSON output, written to Path provided. + * + * @since 3.0 + */ + public void writeValue(Path target, Object value) + throws JacksonException + { + _assertNotNull("target", target); + SerializationContextExt ctxt = _serializerProvider(); + _configAndWriteValue(ctxt, + _generatorFactory.createGenerator(ctxt, target, JsonEncoding.UTF8), value); + } + /** * Method that can be used to serialize any Java value as * JSON output, using output stream provided (using encoding diff --git a/src/test/java/com/fasterxml/jackson/databind/ObjectMapperTest.java b/src/test/java/com/fasterxml/jackson/databind/ObjectMapperTest.java index d4d813443d..276f1226af 100644 --- a/src/test/java/com/fasterxml/jackson/databind/ObjectMapperTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/ObjectMapperTest.java @@ -1,9 +1,14 @@ package com.fasterxml.jackson.databind; +import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.module.SimpleModule; import java.io.*; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.nio.file.*; import java.util.*; import java.util.stream.Collectors; +import java.util.zip.ZipOutputStream; import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; import com.fasterxml.jackson.annotation.JsonInclude; @@ -19,6 +24,7 @@ import com.fasterxml.jackson.databind.introspect.VisibilityChecker; import com.fasterxml.jackson.databind.json.JsonMapper; import com.fasterxml.jackson.databind.node.*; +import com.fasterxml.jackson.databind.type.SimpleType; public class ObjectMapperTest extends BaseMapTest { @@ -335,4 +341,522 @@ public void testHasExplicitTimeZone() throws Exception assertEquals(DEFAULT_TZ, w2.getConfig().getTimeZone()); } } + + public void test_createParser_InputStream() throws Exception + { + InputStream inputStream = new ByteArrayInputStream("\"value\"".getBytes(StandardCharsets.UTF_8)); + JsonParser jsonParser = new ObjectMapper().createParser(inputStream); + + assertEquals(jsonParser.nextTextValue(), "value"); + } + + public void test_createParser_File() throws Exception + { + Path path = Files.createTempFile("", ""); + Files.write(path, "\"value\"".getBytes(StandardCharsets.UTF_8)); + JsonParser jsonParser = new ObjectMapper().createParser(path.toFile()); + + assertEquals(jsonParser.nextTextValue(), "value"); + } + + public void test_createParser_Path() throws Exception + { + Path path = Files.createTempFile("", ""); + Files.write(path, "\"value\"".getBytes(StandardCharsets.UTF_8)); + JsonParser jsonParser = new ObjectMapper().createParser(path); + + assertEquals(jsonParser.nextTextValue(), "value"); + } + + public void test_createParser_Url() throws Exception + { + Path path = Files.createTempFile("", ""); + Files.write(path, "\"value\"".getBytes(StandardCharsets.UTF_8)); + JsonParser jsonParser = new ObjectMapper().createParser(path.toUri().toURL()); + + assertEquals(jsonParser.nextTextValue(), "value"); + } + + public void test_createParser_Reader() throws Exception + { + Reader reader = new StringReader("\"value\""); + JsonParser jsonParser = new ObjectMapper().createParser(reader); + + assertEquals(jsonParser.nextTextValue(), "value"); + } + + public void test_createParser_ByteArray() throws Exception + { + byte[] bytes = "\"value\"".getBytes(StandardCharsets.UTF_8); + JsonParser jsonParser = new ObjectMapper().createParser(bytes); + + assertEquals(jsonParser.nextTextValue(), "value"); + } + + public void test_createParser_String() throws Exception + { + String string = "\"value\""; + JsonParser jsonParser = new ObjectMapper().createParser(string); + + assertEquals(jsonParser.nextTextValue(), "value"); + } + + public void test_createParser_CharArray() throws Exception + { + char[] chars = "\"value\"".toCharArray(); + JsonParser jsonParser = new ObjectMapper().createParser(chars); + + assertEquals(jsonParser.nextTextValue(), "value"); + } + + public void test_createParser_DataInput() throws Exception + { + InputStream inputStream = new ByteArrayInputStream("\"value\"".getBytes(StandardCharsets.UTF_8)); + DataInput dataInput = new DataInputStream(inputStream); + JsonParser jsonParser = new ObjectMapper().createParser(dataInput); + + assertEquals(jsonParser.nextTextValue(), "value"); + } + + private static void test_method_failsIfArgumentIsNull(Runnable runnable) throws Exception + { + try { + runnable.run(); + fail("IllegalArgumentException expected."); + } catch (IllegalArgumentException expected) { + } + } + + public void test_createParser_failsIfArgumentIsNull() throws Exception + { + ObjectMapper objectMapper = new ObjectMapper(); + test_method_failsIfArgumentIsNull(() -> objectMapper.createParser((InputStream) null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.createParser((DataInput) null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.createParser((URL) null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.createParser((Path) null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.createParser((File) null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.createParser((Reader) null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.createParser((String) null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.createParser((byte[]) null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.createParser((byte[]) null, -1, -1)); + test_method_failsIfArgumentIsNull(() -> objectMapper.createParser((char[]) null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.createParser((char[]) null, -1, -1)); + } + + public void test_createGenerator_OutputStream() throws Exception + { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + JsonGenerator jsonGenerator = new ObjectMapper().createGenerator(outputStream); + + jsonGenerator.writeString("value"); + jsonGenerator.close(); + + assertEquals(new String(outputStream.toByteArray(), StandardCharsets.UTF_8), "\"value\""); + + // the stream has not been closed by close + outputStream.write(1); + } + + public void test_createGenerator_File() throws Exception + { + Path path = Files.createTempFile("", ""); + JsonGenerator jsonGenerator = new ObjectMapper().createGenerator(path.toFile(), JsonEncoding.UTF8); + + jsonGenerator.writeString("value"); + jsonGenerator.close(); + + assertEquals(new String(Files.readAllBytes(path), StandardCharsets.UTF_8), "\"value\""); + } + + public void test_createGenerator_Path() throws Exception + { + Path path = Files.createTempFile("", ""); + JsonGenerator jsonGenerator = new ObjectMapper().createGenerator(path, JsonEncoding.UTF8); + + jsonGenerator.writeString("value"); + jsonGenerator.close(); + + assertEquals(new String(Files.readAllBytes(path), StandardCharsets.UTF_8), "\"value\""); + } + + public void test_createGenerator_Writer() throws Exception + { + Writer writer = new StringWriter(); + JsonGenerator jsonGenerator = new ObjectMapper().createGenerator(writer); + + jsonGenerator.writeString("value"); + jsonGenerator.close(); + + assertEquals(writer.toString(), "\"value\""); + + // the writer has not been closed by close + writer.append('1'); + } + + public void test_createGenerator_DataOutput() throws Exception + { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + DataOutput dataOutput = new DataOutputStream(outputStream); + JsonGenerator jsonGenerator = new ObjectMapper().createGenerator(dataOutput); + + jsonGenerator.writeString("value"); + jsonGenerator.close(); + + assertEquals(new String(outputStream.toByteArray(), StandardCharsets.UTF_8), "\"value\""); + + // the data output has not been closed by close + dataOutput.write(1); + } + + public void test_createGenerator_failsIfArgumentIsNull() throws Exception + { + ObjectMapper objectMapper = new ObjectMapper(); + test_method_failsIfArgumentIsNull(() -> objectMapper.createGenerator((OutputStream) null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.createGenerator((OutputStream) null, null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.createGenerator((DataOutput) null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.createGenerator((Path) null, null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.createGenerator((File) null, null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.createGenerator((Writer) null)); + } + + public void test_readTree_InputStream() throws Exception + { + InputStream inputStream = new ByteArrayInputStream("\"value\"".getBytes(StandardCharsets.UTF_8)); + JsonNode jsonNode = new ObjectMapper().readTree(inputStream); + + assertEquals(jsonNode.textValue(), "value"); + } + + public void test_readTree_File() throws Exception + { + Path path = Files.createTempFile("", ""); + Files.write(path, "\"value\"".getBytes(StandardCharsets.UTF_8)); + JsonNode jsonNode = new ObjectMapper().readTree(path.toFile()); + + assertEquals(jsonNode.textValue(), "value"); + } + + public void test_readTree_Path() throws Exception + { + Path path = Files.createTempFile("", ""); + Files.write(path, "\"value\"".getBytes(StandardCharsets.UTF_8)); + JsonNode jsonNode = new ObjectMapper().readTree(path); + + assertEquals(jsonNode.textValue(), "value"); + } + + public void test_readTree_Url() throws Exception + { + Path path = Files.createTempFile("", ""); + Files.write(path, "\"value\"".getBytes(StandardCharsets.UTF_8)); + JsonNode jsonNode = new ObjectMapper().readTree(path.toUri().toURL()); + + assertEquals(jsonNode.textValue(), "value"); + } + + public void test_readTree_Reader() throws Exception + { + Reader reader = new StringReader("\"value\""); + JsonNode jsonNode = new ObjectMapper().readTree(reader); + + assertEquals(jsonNode.textValue(), "value"); + } + + public void test_readTree_ByteArray() throws Exception + { + // with offset and length + byte[] bytes = "\"value\"".getBytes(StandardCharsets.UTF_8); + JsonNode jsonNode1 = new ObjectMapper().readTree(bytes); + + assertEquals(jsonNode1.textValue(), "value"); + + // without offset and length + JsonNode jsonNode2 = new ObjectMapper().readTree(bytes, 0, bytes.length); + + assertEquals(jsonNode2.textValue(), "value"); + } + + public void test_readTree_String() throws Exception + { + String string = "\"value\""; + JsonNode jsonNode = new ObjectMapper().readTree(string); + + assertEquals(jsonNode.textValue(), "value"); + } + + public void test_readTree_failsIfArgumentIsNull() throws Exception + { + ObjectMapper objectMapper = new ObjectMapper(); + test_method_failsIfArgumentIsNull(() -> objectMapper.readTree((InputStream) null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.readTree((URL) null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.readTree((Path) null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.readTree((File) null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.readTree((Reader) null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.readTree((String) null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.readTree((byte[]) null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.readTree((byte[]) null, -1, -1)); + } + + public void test_readValue_InputStream() throws Exception + { + InputStream inputStream = new ByteArrayInputStream("\"value\"".getBytes(StandardCharsets.UTF_8)); + String result1 = new ObjectMapper().readValue(inputStream, String.class); + assertEquals(result1, "value"); + + inputStream.reset(); + String result2 = new ObjectMapper().readValue(inputStream, SimpleType.constructUnsafe(String.class)); + assertEquals(result2, "value"); + + inputStream.reset(); + String result3 = new ObjectMapper().readValue(inputStream, new TypeReference() {}); + assertEquals(result3, "value"); + } + + public void test_readValue_File() throws Exception + { + Path path = Files.createTempFile("", ""); + Files.write(path, "\"value\"".getBytes(StandardCharsets.UTF_8)); + String result1 = new ObjectMapper().readValue(path.toFile(), String.class); + assertEquals(result1, "value"); + String result2 = new ObjectMapper().readValue(path.toFile(), SimpleType.constructUnsafe(String.class)); + assertEquals(result2, "value"); + String result3 = new ObjectMapper().readValue(path.toFile(), new TypeReference() {}); + assertEquals(result3, "value"); + } + + public void test_readValue_Path() throws Exception + { + Path path = Files.createTempFile("", ""); + Files.write(path, "\"value\"".getBytes(StandardCharsets.UTF_8)); + String result1 = new ObjectMapper().readValue(path, String.class); + assertEquals(result1, "value"); + String result2 = new ObjectMapper().readValue(path, SimpleType.constructUnsafe(String.class)); + assertEquals(result2, "value"); + String result3 = new ObjectMapper().readValue(path, new TypeReference() {}); + assertEquals(result3, "value"); + } + + public void test_readValue_Url() throws Exception + { + Path path = Files.createTempFile("", ""); + Files.write(path, "\"value\"".getBytes(StandardCharsets.UTF_8)); + String result1 = new ObjectMapper().readValue(path.toUri().toURL(), String.class); + assertEquals(result1, "value"); + String result2 = new ObjectMapper().readValue(path.toUri().toURL(), SimpleType.constructUnsafe(String.class)); + assertEquals(result2, "value"); + String result3 = new ObjectMapper().readValue(path.toUri().toURL(), new TypeReference() {}); + assertEquals(result3, "value"); + } + + public void test_readValue_Reader() throws Exception + { + Reader reader1 = new StringReader("\"value\""); + String result1 = new ObjectMapper().readValue(reader1, String.class); + assertEquals(result1, "value"); + + Reader reader2 = new StringReader("\"value\""); + String result2 = new ObjectMapper().readValue(reader2, SimpleType.constructUnsafe(String.class)); + assertEquals(result2, "value"); + + Reader reader3 = new StringReader("\"value\""); + String result3 = new ObjectMapper().readValue(reader3, new TypeReference() {}); + assertEquals(result3, "value"); + } + + public void test_readValue_ByteArray() throws Exception + { + byte[] bytes = "\"value\"".getBytes(StandardCharsets.UTF_8); + String result1 = new ObjectMapper().readValue(bytes, String.class); + assertEquals(result1, "value"); + String result2 = new ObjectMapper().readValue(bytes, SimpleType.constructUnsafe(String.class)); + assertEquals(result2, "value"); + String result3 = new ObjectMapper().readValue(bytes, new TypeReference() {}); + assertEquals(result3, "value"); + String result4 = new ObjectMapper().readValue(bytes, 0, bytes.length, String.class); + assertEquals(result4, "value"); + String result5 = new ObjectMapper().readValue(bytes, 0, bytes.length, SimpleType.constructUnsafe(String.class)); + assertEquals(result5, "value"); + String result6 = new ObjectMapper().readValue(bytes, 0, bytes.length, new TypeReference() {}); + assertEquals(result6, "value"); + } + + public void test_readValue_String() throws Exception + { + String string = "\"value\""; + String result1 = new ObjectMapper().readValue(string, String.class); + assertEquals(result1, "value"); + String result2 = new ObjectMapper().readValue(string, SimpleType.constructUnsafe(String.class)); + assertEquals(result2, "value"); + String result3 = new ObjectMapper().readValue(string, new TypeReference() {}); + assertEquals(result3, "value"); + } + + public void test_readValue_DataInput() throws Exception + { + InputStream inputStream = new ByteArrayInputStream("\"value\"".getBytes(StandardCharsets.UTF_8)); + DataInput dataInput1 = new DataInputStream(inputStream); + String result1 = new ObjectMapper().readValue(dataInput1, String.class); + assertEquals(result1, "value"); + + inputStream.reset(); + DataInput dataInput2 = new DataInputStream(inputStream); + String result2 = new ObjectMapper().readValue(dataInput2, SimpleType.constructUnsafe(String.class)); + assertEquals(result2, "value"); + + inputStream.reset(); + DataInput dataInput3 = new DataInputStream(inputStream); + String result3 = new ObjectMapper().readValue(dataInput3, new TypeReference() {}); + assertEquals(result3, "value"); + } + + public void test_readValue_JsonParser() throws Exception + { + String string = "\"value\""; + + JsonParser jsonParser1 = new ObjectMapper().createParser(string); + String result1 = new ObjectMapper().readValue(jsonParser1, String.class); + assertEquals(result1, "value"); + + JsonParser jsonParser2 = new ObjectMapper().createParser(string); + String result2 = new ObjectMapper().readValue(jsonParser2, SimpleType.constructUnsafe(String.class)); + assertEquals(result2, "value"); + + JsonParser jsonParser3 = new ObjectMapper().createParser(string); + String result3 = new ObjectMapper().readValue(jsonParser3, new TypeReference() {}); + assertEquals(result3, "value"); + } + + public void test_readValue_failsIfArgumentIsNull() throws Exception + { + ObjectMapper objectMapper = new ObjectMapper(); + test_method_failsIfArgumentIsNull(() -> objectMapper.readValue((InputStream) null, Map.class)); + test_method_failsIfArgumentIsNull(() -> objectMapper.readValue((InputStream) null, SimpleType.constructUnsafe(Map.class))); + test_method_failsIfArgumentIsNull(() -> objectMapper.readValue((InputStream) null, new TypeReference() {})); + test_method_failsIfArgumentIsNull(() -> objectMapper.readValue((DataInput) null, Map.class)); + test_method_failsIfArgumentIsNull(() -> objectMapper.readValue((DataInput) null, SimpleType.constructUnsafe(Map.class))); + test_method_failsIfArgumentIsNull(() -> objectMapper.readValue((DataInput) null, new TypeReference() {})); + test_method_failsIfArgumentIsNull(() -> objectMapper.readValue((URL) null, Map.class)); + test_method_failsIfArgumentIsNull(() -> objectMapper.readValue((URL) null, SimpleType.constructUnsafe(Map.class))); + test_method_failsIfArgumentIsNull(() -> objectMapper.readValue((URL) null, new TypeReference() {})); + test_method_failsIfArgumentIsNull(() -> objectMapper.readValue((Path) null, Map.class)); + test_method_failsIfArgumentIsNull(() -> objectMapper.readValue((Path) null, SimpleType.constructUnsafe(Map.class))); + test_method_failsIfArgumentIsNull(() -> objectMapper.readValue((Path) null, new TypeReference() {})); + test_method_failsIfArgumentIsNull(() -> objectMapper.readValue((File) null, Map.class)); + test_method_failsIfArgumentIsNull(() -> objectMapper.readValue((File) null, SimpleType.constructUnsafe(Map.class))); + test_method_failsIfArgumentIsNull(() -> objectMapper.readValue((File) null, new TypeReference() {})); + test_method_failsIfArgumentIsNull(() -> objectMapper.readValue((Reader) null, Map.class)); + test_method_failsIfArgumentIsNull(() -> objectMapper.readValue((Reader) null, SimpleType.constructUnsafe(Map.class))); + test_method_failsIfArgumentIsNull(() -> objectMapper.readValue((Reader) null, new TypeReference() {})); + test_method_failsIfArgumentIsNull(() -> objectMapper.readValue((String) null, Map.class)); + test_method_failsIfArgumentIsNull(() -> objectMapper.readValue((String) null, SimpleType.constructUnsafe(Map.class))); + test_method_failsIfArgumentIsNull(() -> objectMapper.readValue((String) null, new TypeReference() {})); + test_method_failsIfArgumentIsNull(() -> objectMapper.readValue((JsonParser) null, Map.class)); + test_method_failsIfArgumentIsNull(() -> objectMapper.readValue((JsonParser) null, SimpleType.constructUnsafe(Map.class))); + test_method_failsIfArgumentIsNull(() -> objectMapper.readValue((JsonParser) null, new TypeReference() {})); + test_method_failsIfArgumentIsNull(() -> objectMapper.readValue((byte[]) null, Map.class)); + test_method_failsIfArgumentIsNull(() -> objectMapper.readValue((byte[]) null, SimpleType.constructUnsafe(Map.class))); + test_method_failsIfArgumentIsNull(() -> objectMapper.readValue((byte[]) null, new TypeReference() {})); + test_method_failsIfArgumentIsNull(() -> objectMapper.readValue((byte[]) null, -1, -1, Map.class)); + test_method_failsIfArgumentIsNull(() -> objectMapper.readValue((byte[]) null, -1, -1, SimpleType.constructUnsafe(Map.class))); + test_method_failsIfArgumentIsNull(() -> objectMapper.readValue((byte[]) null, -1, -1, new TypeReference() {})); + } + + public void test_writeValue_OutputStream() throws Exception + { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + new ObjectMapper().writeValue(outputStream, "value"); + + assertEquals(new String(outputStream.toByteArray(), StandardCharsets.UTF_8), "\"value\""); + + // the stream has not been closed by close + outputStream.write(1); + } + + public void test_writeValue_File() throws Exception + { + Path path = Files.createTempFile("", ""); + new ObjectMapper().writeValue(path.toFile(), "value"); + + assertEquals(new String(Files.readAllBytes(path), StandardCharsets.UTF_8), "\"value\""); + } + + public void test_writeValue_Path() throws Exception + { + Path path = Files.createTempFile("", ""); + new ObjectMapper().writeValue(path, "value"); + + assertEquals(new String(Files.readAllBytes(path), StandardCharsets.UTF_8), "\"value\""); + } + + public void test_writeValue_Writer() throws Exception + { + Writer writer = new StringWriter(); + new ObjectMapper().writeValue(writer, "value"); + + assertEquals(writer.toString(), "\"value\""); + + // the writer has not been closed by close + writer.append('1'); + } + + public void test_writeValue_DataOutput() throws Exception + { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + DataOutput dataOutput = new DataOutputStream(outputStream); + new ObjectMapper().writeValue(dataOutput, "value"); + + assertEquals(new String(outputStream.toByteArray(), StandardCharsets.UTF_8), "\"value\""); + + // the data output has not been closed by close + dataOutput.write(1); + } + + public void test_writeValue_JsonGenerator() throws Exception + { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + JsonGenerator jsonGenerator = new ObjectMapper().createGenerator(outputStream); + new ObjectMapper().writeValue(jsonGenerator, "value"); + + assertEquals(new String(outputStream.toByteArray(), StandardCharsets.UTF_8), "\"value\""); + + // the output stream has not been closed by close + outputStream.write(1); + } + + public void test_writeValue_failsIfArgumentIsNull() throws Exception + { + ObjectMapper objectMapper = new ObjectMapper(); + test_method_failsIfArgumentIsNull(() -> objectMapper.writeValue((OutputStream) null, null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.writeValue((DataOutput) null, null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.writeValue((Path) null, null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.writeValue((File) null, null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.writeValue((Writer) null, null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.writeValue((JsonGenerator) null, null)); + } + + /** + * Verifies that {@link ObjectMapper} can read from and write to {@link Path}s + * whose {@link Path#getFileSystem()} is not the {@linkplain FileSystems#getDefault() default file system}. + */ + public void test_readValue_writeValue_Path_nonDefaultFileSystem() throws IOException { + Path zipFile = Files.createTempFile("", ".zip"); + // write an empty zip archive to the temp file + try (OutputStream out = Files.newOutputStream(zipFile); + ZipOutputStream zipped = new ZipOutputStream(out)) { + } + + // Open the empty zip archive as a file system. + try (FileSystem zipFs = FileSystems.newFileSystem(zipFile, (ClassLoader) null)) { + ObjectMapper mapper = new ObjectMapper(); + + Path jsonFile = zipFs.getPath("/test.json"); + mapper.writeValue(jsonFile, "value"); + + String serialized = new String(Files.readAllBytes(jsonFile), StandardCharsets.UTF_8); + assertEquals(serialized, "\"value\""); + + String result = mapper.readValue(jsonFile, String.class); + assertEquals(result, "value"); + } + } } diff --git a/src/test/java/com/fasterxml/jackson/databind/ObjectReaderTest.java b/src/test/java/com/fasterxml/jackson/databind/ObjectReaderTest.java index df0eaedf37..2334238e19 100644 --- a/src/test/java/com/fasterxml/jackson/databind/ObjectReaderTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/ObjectReaderTest.java @@ -1,6 +1,9 @@ package com.fasterxml.jackson.databind; -import java.io.StringWriter; +import java.io.*; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.nio.file.*; import java.util.*; import com.fasterxml.jackson.annotation.JsonCreator; @@ -471,4 +474,328 @@ public boolean handleUnknownProperty(DeserializationContext ctxt, JsonParser p, assertEquals("test", aObject.knownField); } + + public void test_createParser_InputStream() throws Exception + { + InputStream inputStream = new ByteArrayInputStream("\"value\"".getBytes(StandardCharsets.UTF_8)); + JsonParser jsonParser = new ObjectMapper().reader().createParser(inputStream); + + assertEquals(jsonParser.nextTextValue(), "value"); + } + + public void test_createParser_File() throws Exception + { + Path path = Files.createTempFile("", ""); + Files.write(path, "\"value\"".getBytes(StandardCharsets.UTF_8)); + JsonParser jsonParser = new ObjectMapper().reader().createParser(path.toFile()); + + assertEquals(jsonParser.nextTextValue(), "value"); + } + + public void test_createParser_Path() throws Exception + { + Path path = Files.createTempFile("", ""); + Files.write(path, "\"value\"".getBytes(StandardCharsets.UTF_8)); + JsonParser jsonParser = new ObjectMapper().reader().createParser(path); + + assertEquals(jsonParser.nextTextValue(), "value"); + } + + public void test_createParser_Url() throws Exception + { + Path path = Files.createTempFile("", ""); + Files.write(path, "\"value\"".getBytes(StandardCharsets.UTF_8)); + JsonParser jsonParser = new ObjectMapper().reader().createParser(path.toUri().toURL()); + + assertEquals(jsonParser.nextTextValue(), "value"); + } + + public void test_createParser_Reader() throws Exception + { + Reader reader = new StringReader("\"value\""); + JsonParser jsonParser = new ObjectMapper().reader().createParser(reader); + + assertEquals(jsonParser.nextTextValue(), "value"); + } + + public void test_createParser_ByteArray() throws Exception + { + byte[] bytes = "\"value\"".getBytes(StandardCharsets.UTF_8); + JsonParser jsonParser = new ObjectMapper().reader().createParser(bytes); + + assertEquals(jsonParser.nextTextValue(), "value"); + } + + public void test_createParser_String() throws Exception + { + String string = "\"value\""; + JsonParser jsonParser = new ObjectMapper().reader().createParser(string); + + assertEquals(jsonParser.nextTextValue(), "value"); + } + + public void test_createParser_CharArray() throws Exception + { + char[] chars = "\"value\"".toCharArray(); + JsonParser jsonParser = new ObjectMapper().reader().createParser(chars); + + assertEquals(jsonParser.nextTextValue(), "value"); + } + + public void test_createParser_DataInput() throws Exception + { + InputStream inputStream = new ByteArrayInputStream("\"value\"".getBytes(StandardCharsets.UTF_8)); + DataInput dataInput = new DataInputStream(inputStream); + JsonParser jsonParser = new ObjectMapper().reader().createParser(dataInput); + + assertEquals(jsonParser.nextTextValue(), "value"); + } + + private static void test_method_failsIfArgumentIsNull(Runnable runnable) throws Exception + { + try { + runnable.run(); + fail("IllegalArgumentException expected."); + } catch (IllegalArgumentException expected) { + } + } + + public void test_createParser_failsIfArgumentIsNull() throws Exception + { + ObjectReader objectReader = new ObjectMapper().reader(); + test_method_failsIfArgumentIsNull(() -> objectReader.createParser((InputStream) null)); + test_method_failsIfArgumentIsNull(() -> objectReader.createParser((DataInput) null)); + test_method_failsIfArgumentIsNull(() -> objectReader.createParser((URL) null)); + test_method_failsIfArgumentIsNull(() -> objectReader.createParser((Path) null)); + test_method_failsIfArgumentIsNull(() -> objectReader.createParser((File) null)); + test_method_failsIfArgumentIsNull(() -> objectReader.createParser((Reader) null)); + test_method_failsIfArgumentIsNull(() -> objectReader.createParser((String) null)); + test_method_failsIfArgumentIsNull(() -> objectReader.createParser((byte[]) null)); + test_method_failsIfArgumentIsNull(() -> objectReader.createParser((byte[]) null, -1, -1)); + test_method_failsIfArgumentIsNull(() -> objectReader.createParser((char[]) null)); + test_method_failsIfArgumentIsNull(() -> objectReader.createParser((char[]) null, -1, -1)); + } + + public void test_readTree_InputStream() throws Exception + { + InputStream inputStream = new ByteArrayInputStream("\"value\"".getBytes(StandardCharsets.UTF_8)); + JsonNode jsonNode = new ObjectMapper().reader().readTree(inputStream); + + assertEquals(jsonNode.textValue(), "value"); + } + + public void test_readTree_Reader() throws Exception + { + Reader reader = new StringReader("\"value\""); + JsonNode jsonNode = new ObjectMapper().reader().readTree(reader); + + assertEquals(jsonNode.textValue(), "value"); + } + + public void test_readTree_ByteArray() throws Exception + { + // with offset and length + byte[] bytes = "\"value\"".getBytes(StandardCharsets.UTF_8); + JsonNode jsonNode1 = new ObjectMapper().reader().readTree(bytes); + + assertEquals(jsonNode1.textValue(), "value"); + + // without offset and length + JsonNode jsonNode2 = new ObjectMapper().reader().readTree(bytes, 0, bytes.length); + + assertEquals(jsonNode2.textValue(), "value"); + } + + public void test_readTree_String() throws Exception + { + String string = "\"value\""; + JsonNode jsonNode = new ObjectMapper().reader().readTree(string); + + assertEquals(jsonNode.textValue(), "value"); + } + + public void test_readTree_failsIfArgumentIsNull() throws Exception + { + ObjectReader objectReader = new ObjectMapper().reader(); + test_method_failsIfArgumentIsNull(() -> objectReader.readTree((InputStream) null)); + test_method_failsIfArgumentIsNull(() -> objectReader.readTree((Reader) null)); + test_method_failsIfArgumentIsNull(() -> objectReader.readTree((String) null)); + test_method_failsIfArgumentIsNull(() -> objectReader.readTree((byte[]) null)); + test_method_failsIfArgumentIsNull(() -> objectReader.readTree((byte[]) null, -1, -1)); + } + + public void test_readValue_InputStream() throws Exception + { + InputStream inputStream = new ByteArrayInputStream("\"value\"".getBytes(StandardCharsets.UTF_8)); + String result = new ObjectMapper().readerFor(String.class).readValue(inputStream); + assertEquals(result, "value"); + } + + public void test_readValue_File() throws Exception + { + Path path = Files.createTempFile("", ""); + Files.write(path, "\"value\"".getBytes(StandardCharsets.UTF_8)); + String result = new ObjectMapper().readerFor(String.class).readValue(path.toFile()); + assertEquals(result, "value"); + } + + public void test_readValue_Path() throws Exception + { + Path path = Files.createTempFile("", ""); + Files.write(path, "\"value\"".getBytes(StandardCharsets.UTF_8)); + String result = new ObjectMapper().readerFor(String.class).readValue(path); + assertEquals(result, "value"); + } + + public void test_readValue_Url() throws Exception + { + Path path = Files.createTempFile("", ""); + Files.write(path, "\"value\"".getBytes(StandardCharsets.UTF_8)); + String result = new ObjectMapper().readerFor(String.class).readValue(path.toUri().toURL()); + assertEquals(result, "value"); + } + + public void test_readValue_Reader() throws Exception + { + Reader reader = new StringReader("\"value\""); + String result = new ObjectMapper().readerFor(String.class).readValue(reader); + assertEquals(result, "value"); + } + + public void test_readValue_ByteArray() throws Exception + { + byte[] bytes = "\"value\"".getBytes(StandardCharsets.UTF_8); + String result = new ObjectMapper().readerFor(String.class).readValue(bytes); + assertEquals(result, "value"); + } + + public void test_readValue_String() throws Exception + { + String string = "\"value\""; + String result = new ObjectMapper().readerFor(String.class).readValue(string); + assertEquals(result, "value"); + } + + public void test_readValue_DataInput() throws Exception + { + InputStream inputStream = new ByteArrayInputStream("\"value\"".getBytes(StandardCharsets.UTF_8)); + DataInput dataInput = new DataInputStream(inputStream); + String result = new ObjectMapper().readerFor(String.class).readValue(dataInput); + assertEquals(result, "value"); + } + + public void test_readValue_JsonParser() throws Exception + { + String string = "\"value\""; + JsonParser jsonParser = new ObjectMapper().reader().createParser(string); + String result = new ObjectMapper().readerFor(String.class).readValue(jsonParser); + assertEquals(result, "value"); + } + + public void test_readValue_failsIfArgumentIsNull() throws Exception + { + ObjectReader objectReader = new ObjectMapper().reader(); + test_method_failsIfArgumentIsNull(() -> objectReader.readValue((InputStream) null)); + test_method_failsIfArgumentIsNull(() -> objectReader.readValue((DataInput) null)); + test_method_failsIfArgumentIsNull(() -> objectReader.readValue((URL) null)); + test_method_failsIfArgumentIsNull(() -> objectReader.readValue((Path) null)); + test_method_failsIfArgumentIsNull(() -> objectReader.readValue((File) null)); + test_method_failsIfArgumentIsNull(() -> objectReader.readValue((Reader) null)); + test_method_failsIfArgumentIsNull(() -> objectReader.readValue((String) null)); + test_method_failsIfArgumentIsNull(() -> objectReader.readValue((JsonParser) null)); + test_method_failsIfArgumentIsNull(() -> objectReader.readValue((byte[]) null)); + test_method_failsIfArgumentIsNull(() -> objectReader.readValue((byte[]) null, -1, -1)); + } + + public void test_readValues_InputStream() throws Exception + { + InputStream inputStream = new ByteArrayInputStream("\"value\"".getBytes(StandardCharsets.UTF_8)); + MappingIterator result = new ObjectMapper().readerFor(String.class).readValues(inputStream); + assertEquals(result.next(), "value"); + assertEquals(result.hasNext(), false); + } + + public void test_readValues_File() throws Exception + { + Path path = Files.createTempFile("", ""); + Files.write(path, "\"value\"".getBytes(StandardCharsets.UTF_8)); + MappingIterator result = new ObjectMapper().readerFor(String.class).readValues(path.toFile()); + assertEquals(result.next(), "value"); + assertEquals(result.hasNext(), false); + } + + public void test_readValues_Path() throws Exception + { + Path path = Files.createTempFile("", ""); + Files.write(path, "\"value\"".getBytes(StandardCharsets.UTF_8)); + MappingIterator result = new ObjectMapper().readerFor(String.class).readValues(path); + assertEquals(result.next(), "value"); + assertEquals(result.hasNext(), false); + } + + public void test_readValues_Url() throws Exception + { + Path path = Files.createTempFile("", ""); + Files.write(path, "\"value\"".getBytes(StandardCharsets.UTF_8)); + MappingIterator result = new ObjectMapper().readerFor(String.class).readValues(path.toUri().toURL()); + assertEquals(result.next(), "value"); + assertEquals(result.hasNext(), false); + } + + public void test_readValues_Reader() throws Exception + { + Reader reader = new StringReader("\"value\""); + MappingIterator result = new ObjectMapper().readerFor(String.class).readValues(reader); + assertEquals(result.next(), "value"); + assertEquals(result.hasNext(), false); + } + + public void test_readValues_ByteArray() throws Exception + { + byte[] bytes = "\"value\"".getBytes(StandardCharsets.UTF_8); + MappingIterator result = new ObjectMapper().readerFor(String.class).readValues(bytes); + assertEquals(result.next(), "value"); + assertEquals(result.hasNext(), false); + } + + public void test_readValues_String() throws Exception + { + String string = "\"value\""; + MappingIterator result = new ObjectMapper().readerFor(String.class).readValues(string); + assertEquals(result.next(), "value"); + assertEquals(result.hasNext(), false); + } + + public void test_readValues_DataInput() throws Exception + { + InputStream inputStream = new ByteArrayInputStream("\"value\"".getBytes(StandardCharsets.UTF_8)); + DataInput dataInput = new DataInputStream(inputStream); + MappingIterator result = new ObjectMapper().readerFor(String.class).readValues(dataInput); + assertEquals(result.next(), "value"); + assertEquals(result.hasNext(), false); + } + + public void test_readValues_JsonParser() throws Exception + { + String string = "\"value\""; + JsonParser jsonParser = new ObjectMapper().reader().createParser(string); + MappingIterator result = new ObjectMapper().readerFor(String.class).readValues(jsonParser); + assertEquals(result.next(), "value"); + assertEquals(result.hasNext(), false); + } + + public void test_readValues_failsIfArgumentIsNull() throws Exception + { + ObjectReader objectReader = new ObjectMapper().reader(); + test_method_failsIfArgumentIsNull(() -> objectReader.readValues((InputStream) null)); + test_method_failsIfArgumentIsNull(() -> objectReader.readValues((DataInput) null)); + test_method_failsIfArgumentIsNull(() -> objectReader.readValues((URL) null)); + test_method_failsIfArgumentIsNull(() -> objectReader.readValues((Path) null)); + test_method_failsIfArgumentIsNull(() -> objectReader.readValues((File) null)); + test_method_failsIfArgumentIsNull(() -> objectReader.readValues((Reader) null)); + test_method_failsIfArgumentIsNull(() -> objectReader.readValues((String) null)); + test_method_failsIfArgumentIsNull(() -> objectReader.readValues((JsonParser) null)); + test_method_failsIfArgumentIsNull(() -> objectReader.readValues((byte[]) null)); + test_method_failsIfArgumentIsNull(() -> objectReader.readValues((byte[]) null, -1, -1)); + } } diff --git a/src/test/java/com/fasterxml/jackson/databind/ObjectWriterTest.java b/src/test/java/com/fasterxml/jackson/databind/ObjectWriterTest.java index 351fae72a4..c97b324e02 100644 --- a/src/test/java/com/fasterxml/jackson/databind/ObjectWriterTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/ObjectWriterTest.java @@ -1,9 +1,8 @@ package com.fasterxml.jackson.databind; -import java.io.ByteArrayOutputStream; -import java.io.Closeable; -import java.io.IOException; -import java.io.StringWriter; +import java.io.*; +import java.nio.charset.StandardCharsets; +import java.nio.file.*; import java.util.*; import com.fasterxml.jackson.annotation.JsonTypeInfo; @@ -299,4 +298,334 @@ public void testSchema() throws Exception verifyException(e, "Cannot use FormatSchema"); } } + + private static void test_method_failsIfArgumentIsNull(Runnable runnable) throws Exception + { + try { + runnable.run(); + fail("IllegalArgumentException expected."); + } catch (IllegalArgumentException expected) { + } + } + + public void test_createGenerator_OutputStream() throws Exception + { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + JsonGenerator jsonGenerator = new ObjectMapper().writer().createGenerator(outputStream); + + jsonGenerator.writeString("value"); + jsonGenerator.close(); + + assertEquals(new String(outputStream.toByteArray(), StandardCharsets.UTF_8), "\"value\""); + + // the stream has not been closed by close + outputStream.write(1); + } + + public void test_createGenerator_File() throws Exception + { + Path path = Files.createTempFile("", ""); + JsonGenerator jsonGenerator = new ObjectMapper().writer().createGenerator(path.toFile(), JsonEncoding.UTF8); + + jsonGenerator.writeString("value"); + jsonGenerator.close(); + + assertEquals(new String(Files.readAllBytes(path), StandardCharsets.UTF_8), "\"value\""); + } + + public void test_createGenerator_Path() throws Exception + { + Path path = Files.createTempFile("", ""); + JsonGenerator jsonGenerator = new ObjectMapper().writer().createGenerator(path, JsonEncoding.UTF8); + + jsonGenerator.writeString("value"); + jsonGenerator.close(); + + assertEquals(new String(Files.readAllBytes(path), StandardCharsets.UTF_8), "\"value\""); + } + + public void test_createGenerator_Writer() throws Exception + { + Writer writer = new StringWriter(); + JsonGenerator jsonGenerator = new ObjectMapper().writer().createGenerator(writer); + + jsonGenerator.writeString("value"); + jsonGenerator.close(); + + assertEquals(writer.toString(), "\"value\""); + + // the writer has not been closed by close + writer.append('1'); + } + + public void test_createGenerator_DataOutput() throws Exception + { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + DataOutput dataOutput = new DataOutputStream(outputStream); + JsonGenerator jsonGenerator = new ObjectMapper().writer().createGenerator(dataOutput); + + jsonGenerator.writeString("value"); + jsonGenerator.close(); + + assertEquals(new String(outputStream.toByteArray(), StandardCharsets.UTF_8), "\"value\""); + + // the data output has not been closed by close + dataOutput.write(1); + } + + public void test_createGenerator_failsIfArgumentIsNull() throws Exception + { + ObjectWriter objectWriter = new ObjectMapper().writer(); + test_method_failsIfArgumentIsNull(() -> objectWriter.createGenerator((OutputStream) null)); + test_method_failsIfArgumentIsNull(() -> objectWriter.createGenerator((OutputStream) null, null)); + test_method_failsIfArgumentIsNull(() -> objectWriter.createGenerator((DataOutput) null)); + test_method_failsIfArgumentIsNull(() -> objectWriter.createGenerator((Path) null, null)); + test_method_failsIfArgumentIsNull(() -> objectWriter.createGenerator((File) null, null)); + test_method_failsIfArgumentIsNull(() -> objectWriter.createGenerator((Writer) null)); + } + + public void test_writeValue_OutputStream() throws Exception + { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + new ObjectMapper().writer().writeValue(outputStream, "value"); + + assertEquals(new String(outputStream.toByteArray(), StandardCharsets.UTF_8), "\"value\""); + + // the stream has not been closed by close + outputStream.write(1); + } + + public void test_writeValue_File() throws Exception + { + Path path = Files.createTempFile("", ""); + new ObjectMapper().writer().writeValue(path.toFile(), "value"); + + assertEquals(new String(Files.readAllBytes(path), StandardCharsets.UTF_8), "\"value\""); + } + + public void test_writeValue_Path() throws Exception + { + Path path = Files.createTempFile("", ""); + new ObjectMapper().writer().writeValue(path, "value"); + + assertEquals(new String(Files.readAllBytes(path), StandardCharsets.UTF_8), "\"value\""); + } + + public void test_writeValue_Writer() throws Exception + { + Writer writer = new StringWriter(); + new ObjectMapper().writer().writeValue(writer, "value"); + + assertEquals(writer.toString(), "\"value\""); + + // the writer has not been closed by close + writer.append('1'); + } + + public void test_writeValue_DataOutput() throws Exception + { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + DataOutput dataOutput = new DataOutputStream(outputStream); + new ObjectMapper().writer().writeValue(dataOutput, "value"); + + assertEquals(new String(outputStream.toByteArray(), StandardCharsets.UTF_8), "\"value\""); + + // the data output has not been closed by close + dataOutput.write(1); + } + + public void test_writeValue_JsonGenerator() throws Exception + { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + JsonGenerator jsonGenerator = new ObjectMapper().createGenerator(outputStream); + new ObjectMapper().writer().writeValue(jsonGenerator, "value"); + + assertEquals(new String(outputStream.toByteArray(), StandardCharsets.UTF_8), "\"value\""); + + // the output stream has not been closed by close + outputStream.write(1); + } + + public void test_writeValue_failsIfArgumentIsNull() throws Exception + { + ObjectMapper objectMapper = new ObjectMapper(); + test_method_failsIfArgumentIsNull(() -> objectMapper.writer().writeValue((OutputStream) null, null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.writer().writeValue((DataOutput) null, null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.writer().writeValue((Path) null, null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.writer().writeValue((File) null, null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.writer().writeValue((Writer) null, null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.writer().writeValue((JsonGenerator) null, null)); + } + + public void test_writeValues_OutputStream() throws Exception + { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + SequenceWriter sequenceWriter = new ObjectMapper().writer().writeValues(outputStream); + sequenceWriter.write("value"); + + assertEquals(new String(outputStream.toByteArray(), StandardCharsets.UTF_8), "\"value\""); + + // the stream has not been closed by close + outputStream.write(1); + } + + public void test_writeValues_File() throws Exception + { + Path path = Files.createTempFile("", ""); + SequenceWriter sequenceWriter = new ObjectMapper().writer().writeValues(path.toFile()); + sequenceWriter.write("value"); + + assertEquals(new String(Files.readAllBytes(path), StandardCharsets.UTF_8), "\"value\""); + } + + public void test_writeValues_Path() throws Exception + { + Path path = Files.createTempFile("", ""); + SequenceWriter sequenceWriter = new ObjectMapper().writer().writeValues(path); + sequenceWriter.write("value"); + + assertEquals(new String(Files.readAllBytes(path), StandardCharsets.UTF_8), "\"value\""); + } + + public void test_writeValues_Writer() throws Exception + { + Writer writer = new StringWriter(); + SequenceWriter sequenceWriter = new ObjectMapper().writer().writeValues(writer); + sequenceWriter.write("value"); + + assertEquals(writer.toString(), "\"value\""); + + // the writer has not been closed by close + writer.append('1'); + } + + public void test_writeValues_DataOutput() throws Exception + { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + DataOutput dataOutput = new DataOutputStream(outputStream); + SequenceWriter sequenceWriter = new ObjectMapper().writer().writeValues(dataOutput); + sequenceWriter.write("value"); + + assertEquals(new String(outputStream.toByteArray(), StandardCharsets.UTF_8), "\"value\""); + + // the data output has not been closed by close + dataOutput.write(1); + } + + public void test_writeValues_JsonGenerator() throws Exception + { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + JsonGenerator jsonGenerator = new ObjectMapper().createGenerator(outputStream); + SequenceWriter sequenceWriter = new ObjectMapper().writer().writeValues(jsonGenerator); + sequenceWriter.write("value"); + + assertEquals(new String(outputStream.toByteArray(), StandardCharsets.UTF_8), "\"value\""); + + // the data output has not been closed by close + outputStream.write(1); + } + + public void test_writeValues_failsIfArgumentIsNull() throws Exception + { + ObjectMapper objectMapper = new ObjectMapper(); + test_method_failsIfArgumentIsNull(() -> objectMapper.writer().writeValues((OutputStream) null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.writer().writeValues((DataOutput) null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.writer().writeValues((Path) null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.writer().writeValues((File) null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.writer().writeValues((Writer) null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.writer().writeValues((JsonGenerator) null)); + } + + public void test_writeValuesAsArray_OutputStream() throws Exception + { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + SequenceWriter sequenceWriter = new ObjectMapper().writer().writeValuesAsArray(outputStream); + sequenceWriter.write("value"); + sequenceWriter.flush(); + sequenceWriter.close(); + + assertEquals(new String(outputStream.toByteArray(), StandardCharsets.UTF_8), "[\"value\"]"); + + // the stream has not been closed by close + outputStream.write(1); + } + + public void test_writeValuesAsArray_File() throws Exception + { + Path path = Files.createTempFile("", ""); + SequenceWriter sequenceWriter = new ObjectMapper().writer().writeValuesAsArray(path.toFile()); + sequenceWriter.write("value"); + sequenceWriter.flush(); + sequenceWriter.close(); + + assertEquals(new String(Files.readAllBytes(path), StandardCharsets.UTF_8), "[\"value\"]"); + } + + public void test_writeValuesAsArray_Path() throws Exception + { + Path path = Files.createTempFile("", ""); + SequenceWriter sequenceWriter = new ObjectMapper().writer().writeValuesAsArray(path); + sequenceWriter.write("value"); + sequenceWriter.flush(); + sequenceWriter.close(); + + assertEquals(new String(Files.readAllBytes(path), StandardCharsets.UTF_8), "[\"value\"]"); + } + + public void test_writeValuesAsArray_Writer() throws Exception + { + Writer writer = new StringWriter(); + SequenceWriter sequenceWriter = new ObjectMapper().writer().writeValuesAsArray(writer); + sequenceWriter.write("value"); + sequenceWriter.flush(); + sequenceWriter.close(); + + assertEquals(writer.toString(), "[\"value\"]"); + + // the writer has not been closed by close + writer.append('1'); + } + + public void test_writeValuesAsArray_DataOutput() throws Exception + { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + DataOutput dataOutput = new DataOutputStream(outputStream); + SequenceWriter sequenceWriter = new ObjectMapper().writer().writeValuesAsArray(dataOutput); + sequenceWriter.write("value"); + sequenceWriter.flush(); + sequenceWriter.close(); + + assertEquals(new String(outputStream.toByteArray(), StandardCharsets.UTF_8), "[\"value\"]"); + + // the data output has not been closed by close + dataOutput.write(1); + } + + public void test_writeValuesAsArray_JsonGenerator() throws Exception + { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + JsonGenerator jsonGenerator = new ObjectMapper().createGenerator(outputStream); + SequenceWriter sequenceWriter = new ObjectMapper().writer().writeValuesAsArray(jsonGenerator); + sequenceWriter.write("value"); + sequenceWriter.flush(); + sequenceWriter.close(); + jsonGenerator.flush(); + jsonGenerator.close(); + + assertEquals(new String(outputStream.toByteArray(), StandardCharsets.UTF_8), "[\"value\"]"); + + // the data output has not been closed by close + outputStream.write(1); + } + + public void test_writeValuesAsArray_failsIfArgumentIsNull() throws Exception + { + ObjectMapper objectMapper = new ObjectMapper(); + test_method_failsIfArgumentIsNull(() -> objectMapper.writer().writeValuesAsArray((OutputStream) null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.writer().writeValuesAsArray((DataOutput) null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.writer().writeValuesAsArray((Path) null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.writer().writeValuesAsArray((File) null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.writer().writeValuesAsArray((Writer) null)); + test_method_failsIfArgumentIsNull(() -> objectMapper.writer().writeValuesAsArray((JsonGenerator) null)); + } }