|
1 | 1 | package com.fasterxml.jackson.core.json;
|
2 | 2 |
|
3 | 3 | import java.io.*;
|
| 4 | +import java.nio.charset.StandardCharsets; |
| 5 | +import java.nio.file.*; |
4 | 6 |
|
5 | 7 | import com.fasterxml.jackson.core.*;
|
6 | 8 | import com.fasterxml.jackson.core.io.SerializedString;
|
@@ -200,4 +202,122 @@ public void testRootValues() throws Exception
|
200 | 202 | g.close();
|
201 | 203 | assertEquals("1/2/3", w.toString());
|
202 | 204 | }
|
| 205 | + |
| 206 | + public void test_createGenerator_OutputStream() throws Exception |
| 207 | + { |
| 208 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 209 | + JsonGenerator jsonGenerator = new JsonFactory() |
| 210 | + .createGenerator(ObjectWriteContext.empty(), outputStream); |
| 211 | + |
| 212 | + jsonGenerator.writeString("value"); |
| 213 | + jsonGenerator.close(); |
| 214 | + |
| 215 | + assertEquals(new String(outputStream.toByteArray(), StandardCharsets.UTF_8), "\"value\""); |
| 216 | + |
| 217 | + // the stream has not been closed by close |
| 218 | + outputStream.write(1); |
| 219 | + } |
| 220 | + |
| 221 | + public void test_createGenerator_File() throws Exception |
| 222 | + { |
| 223 | + Path path = Files.createTempFile("", ""); |
| 224 | + JsonGenerator jsonGenerator = new JsonFactory() |
| 225 | + .createGenerator(ObjectWriteContext.empty(), path.toFile(), JsonEncoding.UTF8); |
| 226 | + |
| 227 | + jsonGenerator.writeString("value"); |
| 228 | + jsonGenerator.close(); |
| 229 | + |
| 230 | + assertEquals(new String(Files.readAllBytes(path), StandardCharsets.UTF_8), "\"value\""); |
| 231 | + } |
| 232 | + |
| 233 | + public void test_createGenerator_Path() throws Exception |
| 234 | + { |
| 235 | + Path path = Files.createTempFile("", ""); |
| 236 | + JsonGenerator jsonGenerator = new JsonFactory() |
| 237 | + .createGenerator(ObjectWriteContext.empty(), path, JsonEncoding.UTF8); |
| 238 | + |
| 239 | + jsonGenerator.writeString("value"); |
| 240 | + jsonGenerator.close(); |
| 241 | + |
| 242 | + assertEquals(new String(Files.readAllBytes(path), StandardCharsets.UTF_8), "\"value\""); |
| 243 | + } |
| 244 | + |
| 245 | + public void test_createGenerator_Writer() throws Exception |
| 246 | + { |
| 247 | + Writer writer = new StringWriter(); |
| 248 | + JsonGenerator jsonGenerator = new JsonFactory() |
| 249 | + .createGenerator(ObjectWriteContext.empty(), writer); |
| 250 | + |
| 251 | + jsonGenerator.writeString("value"); |
| 252 | + jsonGenerator.close(); |
| 253 | + |
| 254 | + assertEquals(writer.toString(), "\"value\""); |
| 255 | + |
| 256 | + // the writer has not been closed by close |
| 257 | + writer.append('1'); |
| 258 | + } |
| 259 | + |
| 260 | + public void test_createGenerator_DataOutput() throws Exception |
| 261 | + { |
| 262 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 263 | + DataOutput dataOutput = new DataOutputStream(outputStream); |
| 264 | + JsonGenerator jsonGenerator = new JsonFactory() |
| 265 | + .createGenerator(ObjectWriteContext.empty(), dataOutput); |
| 266 | + |
| 267 | + jsonGenerator.writeString("value"); |
| 268 | + jsonGenerator.close(); |
| 269 | + |
| 270 | + assertEquals(new String(outputStream.toByteArray(), StandardCharsets.UTF_8), "\"value\""); |
| 271 | + |
| 272 | + // the data output has not been closed by close |
| 273 | + dataOutput.write(1); |
| 274 | + } |
| 275 | + |
| 276 | + public void test_createParser_OutputStream() throws Exception |
| 277 | + { |
| 278 | + ByteArrayInputStream inputStream = new ByteArrayInputStream("\"value\"".getBytes(StandardCharsets.UTF_8)); |
| 279 | + JsonParser jsonParser = new JsonFactory() |
| 280 | + .createParser(ObjectReadContext.empty(), inputStream); |
| 281 | + |
| 282 | + assertEquals(jsonParser.nextTextValue(), "value"); |
| 283 | + } |
| 284 | + |
| 285 | + public void test_createParser_File() throws Exception |
| 286 | + { |
| 287 | + Path path = Files.createTempFile("", ""); |
| 288 | + Files.write(path, "\"value\"".getBytes(StandardCharsets.UTF_8)); |
| 289 | + JsonParser jsonParser = new JsonFactory() |
| 290 | + .createParser(ObjectReadContext.empty(), path.toFile()); |
| 291 | + |
| 292 | + assertEquals(jsonParser.nextTextValue(), "value"); |
| 293 | + } |
| 294 | + |
| 295 | + public void test_createParser_Path() throws Exception |
| 296 | + { |
| 297 | + Path path = Files.createTempFile("", ""); |
| 298 | + Files.write(path, "\"value\"".getBytes(StandardCharsets.UTF_8)); |
| 299 | + JsonParser jsonParser = new JsonFactory() |
| 300 | + .createParser(ObjectReadContext.empty(), path); |
| 301 | + |
| 302 | + assertEquals(jsonParser.nextTextValue(), "value"); |
| 303 | + } |
| 304 | + |
| 305 | + public void test_createParser_Reader() throws Exception |
| 306 | + { |
| 307 | + Reader reader = new StringReader("\"value\""); |
| 308 | + JsonParser jsonParser = new JsonFactory() |
| 309 | + .createParser(ObjectReadContext.empty(), reader); |
| 310 | + |
| 311 | + assertEquals(jsonParser.nextTextValue(), "value"); |
| 312 | + } |
| 313 | + |
| 314 | + public void test_createParser_DataInput() throws Exception |
| 315 | + { |
| 316 | + InputStream inputStream = new ByteArrayInputStream("\"value\"".getBytes(StandardCharsets.UTF_8)); |
| 317 | + DataInput dataInput = new DataInputStream(inputStream); |
| 318 | + JsonParser jsonParser = new JsonFactory() |
| 319 | + .createParser(ObjectReadContext.empty(), dataInput); |
| 320 | + |
| 321 | + assertEquals(jsonParser.nextTextValue(), "value"); |
| 322 | + } |
203 | 323 | }
|
0 commit comments