/** * Overridable method, used to create a non-blueprint instances from the blueprint. * This is needed to retain state during serialization. */ protected StdSerializerProvider createInstance(SerializationConfig config, SerializerFactory jsf) { return new StdSerializerProvider(config, this, jsf); }
@Override public final void serializeValue(SerializationConfig config, JsonGenerator jgen, Object value, JavaType rootType, SerializerFactory jsf) throws IOException, JsonGenerationException { if (jsf == null) { throw new IllegalArgumentException("Can not pass null serializerFactory"); } StdSerializerProvider inst = createInstance(config, jsf); if (inst.getClass() != getClass()) { throw new IllegalStateException("Broken serializer provider: createInstance returned instance of type "+inst.getClass()+"; blueprint of type "+getClass()); } inst._serializeValue(jgen, value, rootType); }
@Override public JsonSchema generateJsonSchema(Class<?> type, SerializationConfig config, SerializerFactory jsf) throws JsonMappingException { if (type == null) { throw new IllegalArgumentException("A class must be provided"); } /* First: we need a separate instance, which will hold a copy of the * non-shared ("local") read-only lookup Map for fast * class-to-serializer lookup */ StdSerializerProvider inst = createInstance(config, jsf); // sanity check to avoid weird errors; to ensure sub-classes do override createInstance if (inst.getClass() != getClass()) { throw new IllegalStateException("Broken serializer provider: createInstance returned instance of type "+inst.getClass()+"; blueprint of type "+getClass()); } /* no need for embedded type information for JSON schema generation (all * type information it needs is accessible via "untyped" serializer) */ JsonSerializer<Object> ser = inst.findValueSerializer(type, null); JsonNode schemaNode = (ser instanceof SchemaAware) ? ((SchemaAware) ser).getSchema(inst, null) : JsonSchema.getDefaultSchemaNode(); if (!(schemaNode instanceof ObjectNode)) { throw new IllegalArgumentException("Class " + type.getName() + " would not be serialized as a JSON object and therefore has no schema"); } return new JsonSchema((ObjectNode) schemaNode); }
@Override public boolean hasSerializerFor(SerializationConfig config, Class<?> cls, SerializerFactory jsf) { return createInstance(config, jsf)._findExplicitUntypedSerializer(cls, null) != null; }
if (ser == null) { ser = _createAndCacheUntypedSerializer(valueType, property); ser = getUnknownTypeSerializer(valueType.getRawClass()); return _handleContextualResolvable(ser, property);
ser = getNullValueSerializer(); wrap = false; } else { _reportIncompatibleRootType(value, rootType); ser = findTypedValueSerializer(rootType, true, null);
ser = getNullValueSerializer(); wrap = false; // no name to use for wrapping; can't do! } else { Class<?> cls = value.getClass(); ser = findTypedValueSerializer(cls, true, null);
@Override public void defaultSerializeDateKey(Date date, JsonGenerator jgen) throws IOException, JsonProcessingException { if (isEnabled(SerializationConfig.Feature.WRITE_DATE_KEYS_AS_TIMESTAMPS)) { jgen.writeFieldName(String.valueOf(date.getTime())); } else { if (_dateFormat == null) { DateFormat blueprint = _config.getDateFormat(); // must create a clone since Formats are not thread-safe: _dateFormat = (DateFormat)blueprint.clone(); } jgen.writeFieldName(_dateFormat.format(date)); } }
/** * Method called on the actual non-blueprint provider instance object, to kick off * the serialization. */ protected void _serializeValue(JsonGenerator jgen, Object value) throws IOException, JsonGenerationException { JsonSerializer<Object> ser = (value == null) ? getNullValueSerializer() : findValueSerializer(value.getClass()); ser.serialize(value, jgen, this); }
@Override public JsonSerializer<Object> findTypedValueSerializer(JavaType valueType, boolean cache, BeanProperty property) throws JsonMappingException { // Two-phase lookups; local non-shared cache, then shared: JsonSerializer<Object> ser = _knownSerializers.typedValueSerializer(valueType); if (ser != null) { return ser; } // If not, maybe shared map already has it? ser = _serializerCache.typedValueSerializer(valueType); if (ser != null) { return ser; } // Well, let's just compose from pieces: ser = findValueSerializer(valueType, property); TypeSerializer typeSer = _serializerFactory.createTypeSerializer(_config, valueType, property); if (typeSer != null) { ser = new WrappedSerializer(typeSer, ser); } if (cache) { _serializerCache.addTypedSerializer(valueType, ser); } return ser; }
/** * @since 1.5 ] */ protected JsonSerializer<Object> _createAndCacheUntypedSerializer(JavaType type, BeanProperty property) throws JsonMappingException { JsonSerializer<Object> ser; try { ser = _createUntypedSerializer(type, property); } catch (IllegalArgumentException iae) { /* We better only expose checked exceptions, since those * are what caller is expected to handle */ throw new JsonMappingException(iae.getMessage(), null, iae); } if (ser != null) { _serializerCache.addAndResolveNonTypedSerializer(type, ser, this); } return ser; }
ObjectMapper objMapper = new ObjectMapper(); StdSerializerProvider sp = new StdSerializerProvider(); sp.setNullValueSerializer(new NullSerializer()); objMapper.setSerializerProvider(sp);
@Override protected void _serializeValue(JsonGenerator jgen, Object value) throws IOException, JsonProcessingException { QName rootName = (value == null) ? ROOT_NAME_FOR_NULL : _rootNameLookup.findRootName(value.getClass(), _config); ToXmlGenerator xgen = (ToXmlGenerator) jgen; xgen.setNextName(rootName); xgen.initGenerator(); super._serializeValue(jgen, value); }
public boolean hasSerializerFor(SerializationConfig config, Class<?> cls, SerializerFactory jsf) { return createInstance(config, jsf)._findExplicitSerializer(cls) != null; }
/** * Method that will try to find a serializer, either from cache * or by constructing one; but will not return an "unknown" serializer * if this can not be done but rather returns null. * * @return Serializer if one can be found, null if not. */ protected JsonSerializer<Object> _findExplicitUntypedSerializer(Class<?> runtimeType, BeanProperty property) { // Fast lookup from local lookup thingy works? JsonSerializer<Object> ser = _knownSerializers.untypedValueSerializer(runtimeType); if (ser != null) { return ser; } // If not, maybe shared map already has it? ser = _serializerCache.untypedValueSerializer(runtimeType); if (ser != null) { return ser; } try { return _createAndCacheUntypedSerializer(runtimeType, property); } catch (Exception e) { return null; } }
ObjectMapper mapper = new ObjectMapper(); StdSerializerProvider provider = new StdSerializerProvider(); provider.setKeySerializer(new KeySerializer(A.class)); mapper.setSerializerProvider(provider); StringWriter out = new StringWriter(); mapper.writeValue(out, c); String json = out.toString(); System.out.println("JSON= "+json); C c2 = mapper.readValue(json, C.class); System.out.print("C2= "); StringWriter outC2 = new StringWriter(); mapper.writeValue(outC2, c2); System.out.println(outC2.toString());
@Override public JsonSerializer<Object> findValueSerializer(Class<?> type) throws JsonMappingException { // Fast lookup from local lookup thingy works? JsonSerializer<Object> ser = _knownSerializers.get(type); if (ser != null) { return ser; } // If not, maybe shared map already has it? ser = _serializerCache.findSerializer(type); if (ser != null) { return ser; } // If neither, must create ser = _createAndCacheSerializer(type); // Not found? Must use the unknown type serializer /* Couldn't create? Need to return the fallback serializer, which * most likely will report an error: but one question is whether * we should cache it? */ if (ser == null) { ser = getUnknownTypeSerializer(type); // Should this be added to lookups? if (CACHE_UNKNOWN_MAPPINGS) { _serializerCache.addSerializer(type, ser); } } return ser; }
if (ser == null) { ser = _createAndCacheUntypedSerializer(valueType, property); ser = getUnknownTypeSerializer(valueType); return _handleContextualResolvable(ser, property);
ser = getNullValueSerializer(); wrap = false; } else { _reportIncompatibleRootType(value, rootType); ser = findTypedValueSerializer(rootType, true, null);
@Override public boolean hasSerializerFor(SerializationConfig config, Class<?> cls, SerializerFactory jsf) { return createInstance(config, jsf)._findExplicitUntypedSerializer(cls, null) != null; }