7
7
import java .io .*;
8
8
import java .lang .ref .SoftReference ;
9
9
import java .net .URL ;
10
+ import java .nio .file .Files ;
11
+ import java .nio .file .Path ;
10
12
import java .util .List ;
11
13
import java .util .Locale ;
12
14
@@ -703,6 +705,32 @@ public PropertyNameMatcher constructCINameMatcher(List<Named> matches, boolean a
703
705
public abstract JsonParser createParser (ObjectReadContext readCtxt ,
704
706
File f ) throws JacksonException ;
705
707
708
+ /**
709
+ * Method for constructing parser instance to decode
710
+ * contents of specified path.
711
+ *<p>
712
+ * Encoding is auto-detected from contents according to JSON
713
+ * specification recommended mechanism. Json specification
714
+ * supports only UTF-8, UTF-16 and UTF-32 as valid encodings,
715
+ * so auto-detection implemented only for this charsets.
716
+ * For other charsets use {@link #createParser(java.io.Reader)}.
717
+ *<p>
718
+ * Underlying input stream (needed for reading contents)
719
+ * will be <b>owned</b> (and managed, i.e. closed as need be) by
720
+ * the parser, since caller has no access to it.
721
+ *
722
+ * @param readCtxt Object read context to use
723
+ * @param p Path that contains content to parse
724
+ *
725
+ * @return Parser constructed
726
+ *
727
+ * @throws JacksonException If parser construction or initialization fails
728
+ *
729
+ * @since 3.0
730
+ */
731
+ public abstract JsonParser createParser (ObjectReadContext readCtxt ,
732
+ Path p ) throws JacksonException ;
733
+
706
734
/**
707
735
* Method for constructing JSON parser instance to decode
708
736
* contents of resource reference by given URL.
@@ -1131,6 +1159,30 @@ public abstract JsonGenerator createGenerator(ObjectWriteContext writeCtxt, File
1131
1159
JsonEncoding enc )
1132
1160
throws JacksonException ;
1133
1161
1162
+ /**
1163
+ * Method for constructing generator that writes contents
1164
+ * to specified path, overwriting contents it might have (or creating
1165
+ * it if such path does not yet exist).
1166
+ *<p>
1167
+ * Underlying stream <b>is owned</b> by the generator constructed,
1168
+ * i.e. generator will handle closing of path when
1169
+ * {@link JsonGenerator#close} is called.
1170
+ *
1171
+ * @param writeCtxt Object-binding context where applicable; used for providing contextual
1172
+ * configuration
1173
+ * @param p Path to write contents to
1174
+ * @param enc Character set encoding to use (usually {@link JsonEncoding#UTF8})
1175
+ *
1176
+ * @return Generator constructed
1177
+ *
1178
+ * @throws JacksonException If generator construction or initialization fails
1179
+ *
1180
+ * @since 3.0
1181
+ */
1182
+ public abstract JsonGenerator createGenerator (ObjectWriteContext writeCtxt , Path p ,
1183
+ JsonEncoding enc )
1184
+ throws JacksonException ;
1185
+
1134
1186
/**
1135
1187
* Method for constructing generator that writes content into specified {@link DataOutput},
1136
1188
* using UTF-8 encoding (with formats where encoding is user-configurable).
@@ -1329,6 +1381,15 @@ protected InputStream _fileInputStream(File f) throws JacksonException
1329
1381
}
1330
1382
}
1331
1383
1384
+ protected InputStream _pathInputStream (Path p ) throws JacksonException
1385
+ {
1386
+ try {
1387
+ return Files .newInputStream (p );
1388
+ } catch (IOException e ) {
1389
+ throw _wrapIOFailure (e );
1390
+ }
1391
+ }
1392
+
1332
1393
protected OutputStream _fileOutputStream (File f ) throws JacksonException
1333
1394
{
1334
1395
try {
@@ -1338,6 +1399,15 @@ protected OutputStream _fileOutputStream(File f) throws JacksonException
1338
1399
}
1339
1400
}
1340
1401
1402
+ protected OutputStream _pathOutputStream (Path p ) throws JacksonException
1403
+ {
1404
+ try {
1405
+ return Files .newOutputStream (p );
1406
+ } catch (IOException e ) {
1407
+ throw _wrapIOFailure (e );
1408
+ }
1409
+ }
1410
+
1341
1411
protected JacksonException _wrapIOFailure (IOException e ) {
1342
1412
return WrappedIOException .construct (e , this );
1343
1413
}
0 commit comments