1 package com.fasterxml.jackson.databind; 2 3 import java.io.IOException; 4 5 import com.fasterxml.jackson.core.*; 6 import com.fasterxml.jackson.core.format.InputAccessor; 7 import com.fasterxml.jackson.core.format.MatchStrength; 8 9 /** 10 * Sub-class of {@link JsonFactory} that will create a proper 11 * {@link ObjectCodec} to allow seam-less conversions between 12 * JSON content and Java objects (POJOs). 13 * The only addition to regular {@link JsonFactory} currently 14 * is that {@link ObjectMapper} is constructed and passed as 15 * the codec to use. 16 */ 17 public class MappingJsonFactory 18 extends JsonFactory 19 { 20 private static final long serialVersionUID = -1; // since 2.7 21 MappingJsonFactory()22 public MappingJsonFactory() 23 { 24 this(null); 25 } 26 MappingJsonFactory(ObjectMapper mapper)27 public MappingJsonFactory(ObjectMapper mapper) 28 { 29 super(mapper); 30 if (mapper == null) { 31 setCodec(new ObjectMapper(this)); 32 } 33 } 34 MappingJsonFactory(JsonFactory src, ObjectMapper mapper)35 public MappingJsonFactory(JsonFactory src, ObjectMapper mapper) 36 { 37 super(src, mapper); 38 if (mapper == null) { 39 setCodec(new ObjectMapper(this)); 40 } 41 } 42 43 /** 44 * We'll override the method to return more specific type; co-variance 45 * helps here 46 */ 47 @Override getCodec()48 public final ObjectMapper getCodec() { return (ObjectMapper) _objectCodec; } 49 50 // @since 2.1 51 @Override copy()52 public JsonFactory copy() 53 { 54 _checkInvalidCopy(MappingJsonFactory.class); 55 // note: as with base class, must NOT copy mapper reference 56 return new MappingJsonFactory(this, null); 57 } 58 59 /* 60 /********************************************************** 61 /* Format detection functionality (since 1.8) 62 /********************************************************** 63 */ 64 65 /** 66 * Sub-classes need to override this method 67 */ 68 @Override getFormatName()69 public String getFormatName() 70 { 71 /* since non-JSON factories typically should not extend this class, 72 * let's just always return JSON as name. 73 */ 74 return FORMAT_NAME_JSON; 75 } 76 77 /** 78 * Sub-classes need to override this method 79 */ 80 @Override hasFormat(InputAccessor acc)81 public MatchStrength hasFormat(InputAccessor acc) throws IOException 82 { 83 if (getClass() == MappingJsonFactory.class) { 84 return hasJSONFormat(acc); 85 } 86 return null; 87 } 88 } 89