0
# Object Mapping
1
2
The core functionality of Jackson Databind centers around ObjectMapper, ObjectReader, and ObjectWriter classes. These provide the main API for reading and writing JSON to/from Java objects (POJOs), with ObjectMapper serving as the primary entry point and factory for the other components.
3
4
## ObjectMapper
5
6
ObjectMapper is the main API class that provides functionality for reading and writing JSON, either to/from basic POJOs or to/from a general-purpose JSON Tree Model (JsonNode). It also acts as a factory for more advanced ObjectReader and ObjectWriter classes.
7
8
```java { .api }
9
public class ObjectMapper extends ObjectCodec implements Versioned, Serializable {
10
// Construction
11
public ObjectMapper();
12
public ObjectMapper(JsonFactory jf);
13
public ObjectMapper(JsonFactory jf, DefaultSerializerProvider sp, DefaultDeserializationContext dc);
14
15
// Reading from String
16
public <T> T readValue(String content, Class<T> valueType) throws JsonProcessingException;
17
public <T> T readValue(String content, TypeReference<T> valueTypeRef) throws JsonProcessingException;
18
public <T> T readValue(String content, JavaType valueType) throws JsonProcessingException;
19
20
// Reading from InputStream
21
public <T> T readValue(InputStream src, Class<T> valueType) throws IOException, StreamReadException, DatabindException;
22
public <T> T readValue(InputStream src, TypeReference<T> valueTypeRef) throws IOException, StreamReadException, DatabindException;
23
public <T> T readValue(InputStream src, JavaType valueType) throws IOException, StreamReadException, DatabindException;
24
25
// Reading from Reader
26
public <T> T readValue(Reader src, Class<T> valueType) throws IOException, StreamReadException, DatabindException;
27
public <T> T readValue(Reader src, TypeReference<T> valueTypeRef) throws IOException, StreamReadException, DatabindException;
28
public <T> T readValue(Reader src, JavaType valueType) throws IOException, StreamReadException, DatabindException;
29
30
// Reading from URL and File
31
public <T> T readValue(URL src, Class<T> valueType) throws IOException, StreamReadException, DatabindException;
32
public <T> T readValue(File src, Class<T> valueType) throws IOException, StreamReadException, DatabindException;
33
public <T> T readValue(URL src, TypeReference<T> valueTypeRef) throws IOException, StreamReadException, DatabindException;
34
public <T> T readValue(File src, TypeReference<T> valueTypeRef) throws IOException, StreamReadException, DatabindException;
35
public <T> T readValue(URL src, JavaType valueType) throws IOException, StreamReadException, DatabindException;
36
public <T> T readValue(File src, JavaType valueType) throws IOException, StreamReadException, DatabindException;
37
38
// Reading from byte arrays
39
public <T> T readValue(byte[] src, Class<T> valueType) throws IOException, StreamReadException, DatabindException;
40
public <T> T readValue(byte[] src, int offset, int len, Class<T> valueType) throws IOException, StreamReadException, DatabindException;
41
public <T> T readValue(byte[] src, TypeReference<T> valueTypeRef) throws IOException, StreamReadException, DatabindException;
42
public <T> T readValue(byte[] src, int offset, int len, TypeReference<T> valueTypeRef) throws IOException, StreamReadException, DatabindException;
43
public <T> T readValue(byte[] src, JavaType valueType) throws IOException, StreamReadException, DatabindException;
44
public <T> T readValue(byte[] src, int offset, int len, JavaType valueType) throws IOException, StreamReadException, DatabindException;
45
46
// Reading from DataInput
47
public <T> T readValue(DataInput src, Class<T> valueType) throws IOException;
48
public <T> T readValue(DataInput src, JavaType valueType) throws IOException;
49
50
// Reading sequences/arrays
51
public <T> MappingIterator<T> readValues(JsonParser p, Class<T> valueType) throws IOException;
52
public <T> MappingIterator<T> readValues(JsonParser p, TypeReference<T> valueTypeRef) throws IOException;
53
public <T> MappingIterator<T> readValues(JsonParser p, JavaType valueType) throws IOException;
54
55
// Writing to String
56
public String writeValueAsString(Object value) throws JsonProcessingException;
57
58
// Writing to byte arrays
59
public byte[] writeValueAsBytes(Object value) throws JsonProcessingException;
60
61
// Writing to OutputStream
62
public void writeValue(OutputStream out, Object value) throws IOException, StreamWriteException, DatabindException;
63
64
// Writing to Writer
65
public void writeValue(Writer w, Object value) throws IOException, StreamWriteException, DatabindException;
66
67
// Writing to File
68
public void writeValue(File resultFile, Object value) throws IOException, StreamWriteException, DatabindException;
69
70
// Writing to DataOutput
71
public void writeValue(DataOutput out, Object value) throws IOException;
72
73
// Writing to JsonGenerator
74
public void writeValue(JsonGenerator g, Object value) throws IOException, StreamWriteException, DatabindException;
75
76
// Tree model methods
77
public JsonNode readTree(String content) throws JsonProcessingException;
78
public JsonNode readTree(InputStream in) throws IOException, StreamReadException, DatabindException;
79
public JsonNode readTree(Reader r) throws IOException, StreamReadException, DatabindException;
80
public JsonNode readTree(JsonParser p) throws IOException, StreamReadException, DatabindException;
81
public JsonNode readTree(byte[] content) throws IOException, StreamReadException, DatabindException;
82
public JsonNode readTree(File file) throws IOException, StreamReadException, DatabindException;
83
public JsonNode readTree(URL source) throws IOException, StreamReadException, DatabindException;
84
85
public void writeTree(JsonGenerator g, TreeNode rootNode) throws IOException, StreamWriteException, DatabindException;
86
public void writeTree(JsonGenerator g, JsonNode rootNode) throws IOException, StreamWriteException, DatabindException;
87
88
// Object conversion
89
public <T> T convertValue(Object fromValue, Class<T> toValueType) throws IllegalArgumentException;
90
public <T> T convertValue(Object fromValue, TypeReference<T> toValueTypeRef) throws IllegalArgumentException;
91
public <T> T convertValue(Object fromValue, JavaType toValueType) throws IllegalArgumentException;
92
93
// Updating existing objects
94
public ObjectReader readerForUpdating(Object valueToUpdate);
95
public <T> T updateValue(T valueToUpdate, Object overrides) throws JsonMappingException;
96
97
// Factory methods for ObjectReader
98
public ObjectReader reader();
99
public ObjectReader reader(DeserializationFeature feature);
100
public ObjectReader reader(DeserializationFeature first, DeserializationFeature... other);
101
public ObjectReader readerFor(Class<?> type);
102
public ObjectReader readerFor(JavaType type);
103
public ObjectReader readerFor(TypeReference<?> type);
104
public ObjectReader readerWithView(Class<?> view);
105
106
// Factory methods for ObjectWriter
107
public ObjectWriter writer();
108
public ObjectWriter writer(SerializationFeature feature);
109
public ObjectWriter writer(SerializationFeature first, SerializationFeature... other);
110
public ObjectWriter writer(DateFormat df);
111
public ObjectWriter writerWithView(Class<?> serializationView);
112
public ObjectWriter writerFor(Class<?> rootType);
113
public ObjectWriter writerFor(TypeReference<?> rootType);
114
public ObjectWriter writerFor(JavaType rootType);
115
public ObjectWriter writer(PrettyPrinter pp);
116
public ObjectWriter writerWithDefaultPrettyPrinter();
117
public ObjectWriter writer(FilterProvider filterProvider);
118
119
// Configuration
120
public ObjectMapper configure(SerializationFeature f, boolean state);
121
public ObjectMapper configure(DeserializationFeature f, boolean state);
122
public ObjectMapper configure(MapperFeature f, boolean state);
123
public ObjectMapper configure(JsonParser.Feature f, boolean state);
124
public ObjectMapper configure(JsonGenerator.Feature f, boolean state);
125
public ObjectMapper configure(JsonFactory.Feature f, boolean state);
126
127
public ObjectMapper enable(SerializationFeature... features);
128
public ObjectMapper disable(SerializationFeature... features);
129
public ObjectMapper enable(DeserializationFeature... features);
130
public ObjectMapper disable(DeserializationFeature... features);
131
public ObjectMapper enable(MapperFeature... features);
132
public ObjectMapper disable(MapperFeature... features);
133
134
// Configuration checking
135
public boolean isEnabled(SerializationFeature f);
136
public boolean isEnabled(DeserializationFeature f);
137
public boolean isEnabled(MapperFeature f);
138
public boolean isEnabled(JsonParser.Feature f);
139
public boolean isEnabled(JsonGenerator.Feature f);
140
public boolean isEnabled(JsonFactory.Feature f);
141
142
// Type factory access
143
public TypeFactory getTypeFactory();
144
public ObjectMapper setTypeFactory(TypeFactory tf);
145
146
// Advanced configuration
147
public ObjectMapper setSerializerFactory(SerializerFactory f);
148
public SerializerFactory getSerializerFactory();
149
public ObjectMapper setSerializerProvider(DefaultSerializerProvider p);
150
public SerializerProvider getSerializerProvider();
151
public ObjectMapper setMixInAnnotations(Map<Class<?>, Class<?>> sourceMixins);
152
public ObjectMapper addMixIn(Class<?> target, Class<?> mixinSource);
153
public Class<?> findMixInClassFor(Class<?> cls);
154
public int mixInCount();
155
156
// Modules
157
public ObjectMapper registerModule(Module module);
158
public ObjectMapper registerModules(Module... modules);
159
public ObjectMapper registerModules(Iterable<? extends Module> modules);
160
public Set<Object> getRegisteredModuleIds();
161
162
// Subtypes
163
public void registerSubtypes(Class<?>... classes);
164
public void registerSubtypes(NamedType... types);
165
public void registerSubtypes(Collection<Class<?>> subtypes);
166
167
// Copy and factory
168
public ObjectMapper copy();
169
public JsonFactory getFactory();
170
171
// Node factory
172
public JsonNodeFactory getNodeFactory();
173
public ObjectMapper setNodeFactory(JsonNodeFactory f);
174
175
// Context attributes
176
public ObjectMapper setDefaultAttributes(ContextAttributes attrs);
177
public ContextAttributes getDefaultAttributes();
178
179
// Advanced settings
180
public ObjectMapper setDefaultPrettyPrinter(PrettyPrinter pp);
181
public void setSerializationInclusion(JsonInclude.Include incl);
182
public ObjectMapper setPropertyNamingStrategy(PropertyNamingStrategy s);
183
public PropertyNamingStrategy getPropertyNamingStrategy();
184
public ObjectMapper setDefaultSetterInfo(JsonSetter.Value v);
185
public ObjectMapper setDefaultVisibility(PropertyAccessor forMethod, JsonAutoDetect.Visibility visibility);
186
public ObjectMapper setDefaultMergeable(Boolean b);
187
public ObjectMapper setDefaultLeniency(Boolean b);
188
189
// Polymorphic type validation
190
public ObjectMapper activateDefaultTyping(PolymorphicTypeValidator ptv);
191
public ObjectMapper activateDefaultTyping(PolymorphicTypeValidator ptv, ObjectMapper.DefaultTyping dti);
192
public ObjectMapper activateDefaultTyping(PolymorphicTypeValidator ptv, ObjectMapper.DefaultTyping dti, JsonTypeInfo.As implType);
193
public ObjectMapper deactivateDefaultTyping();
194
public PolymorphicTypeValidator getPolymorphicTypeValidator();
195
196
// Injectable values
197
public ObjectMapper setInjectableValues(InjectableValues injectableValues);
198
public InjectableValues getInjectableValues();
199
200
// Locale and TimeZone
201
public ObjectMapper setLocale(Locale l);
202
public ObjectMapper setTimeZone(TimeZone tz);
203
204
// Date handling
205
public ObjectMapper setDateFormat(DateFormat dateFormat);
206
public DateFormat getDateFormat();
207
208
// Handler instantiation
209
public ObjectMapper setHandlerInstantiator(HandlerInstantiator hi);
210
public HandlerInstantiator getHandlerInstantiator();
211
212
// Base64 variant
213
public ObjectMapper setBase64Variant(Base64Variant v);
214
public Base64Variant getBase64Variant();
215
216
// Configuration access
217
public SerializationConfig getSerializationConfig();
218
public DeserializationConfig getDeserializationConfig();
219
}
220
221
public enum DefaultTyping {
222
JAVA_LANG_OBJECT,
223
OBJECT_AND_NON_CONCRETE,
224
NON_CONCRETE_AND_ARRAYS,
225
NON_FINAL,
226
EVERYTHING;
227
}
228
```
229
230
## ObjectReader
231
232
ObjectReader is a builder-style factory for configuring JsonParser instances for reading JSON content. It provides a fluent API for creating configured instances that can be reused efficiently.
233
234
```java { .api }
235
public class ObjectReader implements Versioned, Serializable {
236
// Reading methods matching ObjectMapper
237
public <T> T readValue(InputStream src) throws IOException, StreamReadException, DatabindException;
238
public <T> T readValue(Reader src) throws IOException, StreamReadException, DatabindException;
239
public <T> T readValue(String src) throws IOException, StreamReadException, DatabindException;
240
public <T> T readValue(byte[] src) throws IOException, StreamReadException, DatabindException;
241
public <T> T readValue(byte[] src, int offset, int length) throws IOException, StreamReadException, DatabindException;
242
public <T> T readValue(File src) throws IOException, StreamReadException, DatabindException;
243
public <T> T readValue(URL src) throws IOException, StreamReadException, DatabindException;
244
public <T> T readValue(JsonNode src) throws IOException, StreamReadException, DatabindException;
245
public <T> T readValue(DataInput src) throws IOException;
246
247
// Reading with JsonParser
248
public <T> T readValue(JsonParser p) throws IOException, StreamReadException, DatabindException;
249
250
// Reading sequences
251
public <T> MappingIterator<T> readValues(InputStream src) throws IOException, StreamReadException, DatabindException;
252
public <T> MappingIterator<T> readValues(Reader src) throws IOException, StreamReadException, DatabindException;
253
public <T> MappingIterator<T> readValues(String json) throws IOException, StreamReadException, DatabindException;
254
public <T> MappingIterator<T> readValues(byte[] src, int offset, int length) throws IOException, StreamReadException, DatabindException;
255
public <T> MappingIterator<T> readValues(byte[] src) throws IOException, StreamReadException, DatabindException;
256
public <T> MappingIterator<T> readValues(File src) throws IOException, StreamReadException, DatabindException;
257
public <T> MappingIterator<T> readValues(URL src) throws IOException, StreamReadException, DatabindException;
258
public <T> MappingIterator<T> readValues(JsonParser p) throws IOException, StreamReadException, DatabindException;
259
260
// Tree reading
261
public JsonNode readTree(InputStream in) throws IOException, StreamReadException, DatabindException;
262
public JsonNode readTree(Reader r) throws IOException, StreamReadException, DatabindException;
263
public JsonNode readTree(String content) throws IOException, StreamReadException, DatabindException;
264
public JsonNode readTree(byte[] content) throws IOException, StreamReadException, DatabindException;
265
public JsonNode readTree(byte[] content, int offset, int len) throws IOException, StreamReadException, DatabindException;
266
public JsonNode readTree(DataInput src) throws IOException;
267
268
// Configuration - Features
269
public ObjectReader with(DeserializationFeature feature);
270
public ObjectReader with(DeserializationFeature first, DeserializationFeature... other);
271
public ObjectReader withFeatures(DeserializationFeature... features);
272
public ObjectReader without(DeserializationFeature feature);
273
public ObjectReader without(DeserializationFeature first, DeserializationFeature... other);
274
public ObjectReader withoutFeatures(DeserializationFeature... features);
275
276
// Configuration - Type
277
public ObjectReader forType(Class<?> valueType);
278
public ObjectReader forType(JavaType valueType);
279
public ObjectReader forType(TypeReference<?> valueTypeRef);
280
281
// Configuration - Root name
282
public ObjectReader withRootName(String rootName);
283
public ObjectReader withRootName(PropertyName rootName);
284
public ObjectReader withoutRootName();
285
286
// Configuration - View
287
public ObjectReader withView(Class<?> activeView);
288
289
// Configuration - Type factory
290
public ObjectReader with(TypeFactory tf);
291
292
// Configuration - Injectable values
293
public ObjectReader with(InjectableValues injectableValues);
294
295
// Configuration - Attributes
296
public ObjectReader withAttributes(Map<?, ?> attrs);
297
public ObjectReader withAttribute(Object key, Object value);
298
public ObjectReader withoutAttribute(Object key);
299
300
// Configuration - Parser features
301
public ObjectReader with(JsonParser.Feature feature);
302
public ObjectReader withFeatures(JsonParser.Feature... features);
303
public ObjectReader without(JsonParser.Feature feature);
304
public ObjectReader withoutFeatures(JsonParser.Feature... features);
305
306
// Configuration - Format features
307
public ObjectReader with(FormatFeature feature);
308
public ObjectReader withFeatures(FormatFeature... features);
309
public ObjectReader without(FormatFeature feature);
310
public ObjectReader withoutFeatures(FormatFeature... features);
311
312
// Configuration - Schema
313
public ObjectReader with(FormatSchema schema);
314
315
// Configuration - Base64
316
public ObjectReader with(Base64Variant defaultBase64);
317
318
// Configuration - Character escaping
319
public ObjectReader with(CharacterEscapes escapes);
320
321
// Configuration - Context attributes
322
public ObjectReader with(ContextAttributes attrs);
323
324
// Configuration access
325
public boolean isEnabled(DeserializationFeature f);
326
public boolean isEnabled(MapperFeature f);
327
public boolean isEnabled(JsonParser.Feature f);
328
public DeserializationConfig getConfig();
329
public JsonFactory getFactory();
330
public TypeFactory getTypeFactory();
331
public ContextAttributes getAttributes();
332
333
// Value type access
334
public JavaType getValueType();
335
336
// Versioning
337
public Version version();
338
}
339
```
340
341
## ObjectWriter
342
343
ObjectWriter is a builder-style factory for configuring JsonGenerator instances for writing JSON content. It provides a fluent API for creating configured instances for efficient reuse.
344
345
```java { .api }
346
public class ObjectWriter implements Versioned, Serializable {
347
// Writing methods matching ObjectMapper
348
public void writeValue(OutputStream out, Object value) throws IOException, StreamWriteException, DatabindException;
349
public void writeValue(Writer w, Object value) throws IOException, StreamWriteException, DatabindException;
350
public void writeValue(File resultFile, Object value) throws IOException, StreamWriteException, DatabindException;
351
public void writeValue(DataOutput out, Object value) throws IOException;
352
353
// Writing to strings/bytes
354
public String writeValueAsString(Object value) throws JsonProcessingException;
355
public byte[] writeValueAsBytes(Object value) throws JsonProcessingException;
356
357
// Writing with JsonGenerator
358
public void writeValue(JsonGenerator g, Object value) throws IOException, StreamWriteException, DatabindException;
359
360
// Sequence writing
361
public SequenceWriter writeValues(OutputStream out) throws IOException;
362
public SequenceWriter writeValues(Writer out) throws IOException;
363
public SequenceWriter writeValues(File out) throws IOException;
364
public SequenceWriter writeValues(DataOutput out) throws IOException;
365
public SequenceWriter writeValuesAsArray(OutputStream out) throws IOException;
366
public SequenceWriter writeValuesAsArray(Writer out) throws IOException;
367
public SequenceWriter writeValuesAsArray(File out) throws IOException;
368
public SequenceWriter writeValuesAsArray(DataOutput out) throws IOException;
369
370
// Configuration - Features
371
public ObjectWriter with(SerializationFeature feature);
372
public ObjectWriter with(SerializationFeature first, SerializationFeature... other);
373
public ObjectWriter withFeatures(SerializationFeature... features);
374
public ObjectWriter without(SerializationFeature feature);
375
public ObjectWriter without(SerializationFeature first, SerializationFeature... other);
376
public ObjectWriter withoutFeatures(SerializationFeature... features);
377
378
// Configuration - Type
379
public ObjectWriter forType(Class<?> rootType);
380
public ObjectWriter forType(JavaType rootType);
381
public ObjectWriter forType(TypeReference<?> rootType);
382
383
// Configuration - Root name
384
public ObjectWriter withRootName(String rootName);
385
public ObjectWriter withRootName(PropertyName rootName);
386
public ObjectWriter withoutRootName();
387
388
// Configuration - View
389
public ObjectWriter withView(Class<?> view);
390
391
// Configuration - Pretty printing
392
public ObjectWriter with(PrettyPrinter pp);
393
public ObjectWriter withDefaultPrettyPrinter();
394
public ObjectWriter with(MinimalPrettyPrinter pp);
395
396
// Configuration - Filter provider
397
public ObjectWriter with(FilterProvider filterProvider);
398
399
// Configuration - Date format
400
public ObjectWriter with(DateFormat df);
401
402
// Configuration - Type factory
403
public ObjectWriter with(TypeFactory tf);
404
405
// Configuration - Attributes
406
public ObjectWriter withAttributes(Map<?, ?> attrs);
407
public ObjectWriter withAttribute(Object key, Object value);
408
public ObjectWriter withoutAttribute(Object key);
409
410
// Configuration - Generator features
411
public ObjectWriter with(JsonGenerator.Feature feature);
412
public ObjectWriter withFeatures(JsonGenerator.Feature... features);
413
public ObjectWriter without(JsonGenerator.Feature feature);
414
public ObjectWriter withoutFeatures(JsonGenerator.Feature... features);
415
416
// Configuration - Format features
417
public ObjectWriter with(FormatFeature feature);
418
public ObjectWriter withFeatures(FormatFeature... features);
419
public ObjectWriter without(FormatFeature feature);
420
public ObjectWriter withoutFeatures(FormatFeature... features);
421
422
// Configuration - Schema
423
public ObjectWriter with(FormatSchema schema);
424
425
// Configuration - Base64
426
public ObjectWriter with(Base64Variant b64variant);
427
428
// Configuration - Character escaping
429
public ObjectWriter with(CharacterEscapes escapes);
430
431
// Configuration - Root value separator
432
public ObjectWriter withRootValueSeparator(String sep);
433
public ObjectWriter withRootValueSeparator(SerializableString sep);
434
435
// Configuration access
436
public boolean isEnabled(SerializationFeature f);
437
public boolean isEnabled(MapperFeature f);
438
public boolean isEnabled(JsonGenerator.Feature f);
439
public SerializationConfig getConfig();
440
public JsonFactory getFactory();
441
public TypeFactory getTypeFactory();
442
public boolean hasPrefixElements();
443
444
// Versioning
445
public Version version();
446
}
447
```
448
449
## Usage Examples
450
451
### Basic Object Mapping
452
453
```java
454
import com.fasterxml.jackson.databind.ObjectMapper;
455
456
// Create mapper
457
ObjectMapper mapper = new ObjectMapper();
458
459
// Simple POJO
460
public class Person {
461
public String name;
462
public int age;
463
public Person() {} // Default constructor required
464
public Person(String name, int age) {
465
this.name = name;
466
this.age = age;
467
}
468
}
469
470
// Writing object to JSON
471
Person person = new Person("John", 30);
472
String json = mapper.writeValueAsString(person);
473
// Result: {"name":"John","age":30}
474
475
// Reading JSON to object
476
String json = "{\"name\":\"Jane\", \"age\":25}";
477
Person person = mapper.readValue(json, Person.class);
478
479
// Reading from different sources
480
Person person1 = mapper.readValue(new File("person.json"), Person.class);
481
Person person2 = mapper.readValue(new URL("http://example.com/person.json"), Person.class);
482
Person person3 = mapper.readValue(inputStream, Person.class);
483
```
484
485
### Generic Types with TypeReference
486
487
```java
488
import com.fasterxml.jackson.core.type.TypeReference;
489
import java.util.List;
490
import java.util.Map;
491
492
ObjectMapper mapper = new ObjectMapper();
493
494
// Reading collections
495
String jsonArray = "[{\"name\":\"John\",\"age\":30}, {\"name\":\"Jane\",\"age\":25}]";
496
List<Person> people = mapper.readValue(jsonArray, new TypeReference<List<Person>>() {});
497
498
// Reading maps
499
String jsonMap = "{\"person1\":{\"name\":\"John\",\"age\":30}, \"person2\":{\"name\":\"Jane\",\"age\":25}}";
500
Map<String, Person> personMap = mapper.readValue(jsonMap, new TypeReference<Map<String, Person>>() {});
501
502
// Complex nested generics
503
String complexJson = "{\"data\":[{\"items\":[{\"name\":\"item1\"}]}]}";
504
Map<String, List<Map<String, Object>>> complex = mapper.readValue(complexJson,
505
new TypeReference<Map<String, List<Map<String, Object>>>>() {});
506
```
507
508
### ObjectReader Configuration
509
510
```java
511
ObjectMapper mapper = new ObjectMapper();
512
513
// Create configured reader
514
ObjectReader reader = mapper.readerFor(Person.class)
515
.with(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
516
.without(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
517
.withView(PublicView.class);
518
519
// Reuse configured reader
520
Person person1 = reader.readValue(json1);
521
Person person2 = reader.readValue(json2);
522
Person person3 = reader.readValue(inputStream);
523
524
// Reading sequences
525
MappingIterator<Person> iterator = reader.readValues(jsonArrayStream);
526
while (iterator.hasNextValue()) {
527
Person person = iterator.nextValue();
528
// Process person
529
}
530
```
531
532
### ObjectWriter Configuration
533
534
```java
535
ObjectMapper mapper = new ObjectMapper();
536
537
// Create configured writer
538
ObjectWriter writer = mapper.writerFor(Person.class)
539
.with(SerializationFeature.INDENT_OUTPUT)
540
.without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
541
.withView(PublicView.class)
542
.withDefaultPrettyPrinter();
543
544
// Reuse configured writer
545
String json1 = writer.writeValueAsString(person1);
546
String json2 = writer.writeValueAsString(person2);
547
writer.writeValue(outputStream, person3);
548
549
// Writing sequences
550
SequenceWriter seqWriter = writer.writeValues(outputStream);
551
seqWriter.write(person1);
552
seqWriter.write(person2);
553
seqWriter.close();
554
```
555
556
### Object Conversion
557
558
```java
559
ObjectMapper mapper = new ObjectMapper();
560
561
// Convert between different object types
562
Map<String, Object> map = new HashMap<>();
563
map.put("name", "John");
564
map.put("age", 30);
565
566
Person person = mapper.convertValue(map, Person.class);
567
568
// Convert back
569
Map<String, Object> converted = mapper.convertValue(person,
570
new TypeReference<Map<String, Object>>() {});
571
572
// Update existing objects
573
Person existing = new Person("Jane", 25);
574
Person updates = new Person("Jane Smith", 26);
575
Person updated = mapper.updateValue(existing, updates);
576
```
577
578
### Handling Errors
579
580
```java
581
ObjectMapper mapper = new ObjectMapper();
582
583
try {
584
Person person = mapper.readValue(invalidJson, Person.class);
585
} catch (JsonProcessingException e) {
586
// Handle JSON parsing errors
587
System.err.println("JSON processing error: " + e.getMessage());
588
} catch (IOException e) {
589
// Handle IO errors
590
System.err.println("IO error: " + e.getMessage());
591
}
592
593
// Configure error handling behavior
594
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
595
mapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
596
```
597
598
## Types
599
600
```java { .api }
601
// Mapping iterator for sequences
602
public class MappingIterator<T> implements Iterator<T>, Closeable {
603
public boolean hasNext();
604
public boolean hasNextValue() throws IOException;
605
public T next();
606
public T nextValue() throws IOException;
607
public void remove();
608
public void close() throws IOException;
609
public List<T> readAll() throws IOException;
610
public List<T> readAll(List<T> resultList) throws IOException;
611
public JsonLocation getCurrentLocation();
612
public JsonParser getParser();
613
}
614
615
// Sequence writer for output sequences
616
public class SequenceWriter implements Versioned, Closeable, Flushable {
617
public SequenceWriter write(Object value) throws IOException;
618
public SequenceWriter writeAll(Object[] array) throws IOException;
619
public SequenceWriter writeAll(Iterable<?> iterable) throws IOException;
620
public void close() throws IOException;
621
public void flush() throws IOException;
622
public Version version();
623
}
624
625
// Exception for databind operations
626
public abstract class DatabindException extends JsonProcessingException {
627
public abstract void prependPath(Object referrer, String fieldName);
628
public abstract void prependPath(Object referrer, int index);
629
public abstract void prependPath(JsonMappingException.Reference r);
630
}
631
632
// Stream exceptions
633
public class StreamReadException extends JsonProcessingException {
634
public StreamReadException(JsonParser p, String msg);
635
public StreamReadException(JsonParser p, String msg, Throwable root);
636
public JsonParser getProcessor();
637
public RequestPayload getRequestPayload();
638
public String getRequestPayloadAsString();
639
}
640
641
public class StreamWriteException extends JsonProcessingException {
642
public StreamWriteException(JsonGenerator g, String msg);
643
public StreamWriteException(JsonGenerator g, String msg, Throwable root);
644
public JsonGenerator getProcessor();
645
}
646
```