-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c6607d1
commit 4299ccc
Showing
5 changed files
with
108 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/main/java/com/fasterxml/jackson/databind/ser/std/ToEmptyObjectSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.fasterxml.jackson.databind.ser.std; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.Type; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.core.JsonToken; | ||
import com.fasterxml.jackson.core.type.WritableTypeId; | ||
|
||
import com.fasterxml.jackson.databind.JavaType; | ||
import com.fasterxml.jackson.databind.JsonMappingException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl; | ||
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper; | ||
import com.fasterxml.jackson.databind.jsontype.TypeSerializer; | ||
|
||
/** | ||
* Simple "bogus" serializer that will just serialize an empty | ||
* Object for any given value. | ||
* Quite similar to {@code UnknownSerializer} with the exception that | ||
* serialization never fails. | ||
* | ||
* @since 2.13 | ||
*/ | ||
@JacksonStdImpl | ||
@SuppressWarnings("serial") | ||
public class ToEmptyObjectSerializer | ||
extends StdSerializer<Object> | ||
{ | ||
protected ToEmptyObjectSerializer(Class<?> raw) { | ||
super(raw, false); | ||
} | ||
|
||
public ToEmptyObjectSerializer(JavaType type) { | ||
super(type); | ||
} | ||
|
||
@Override | ||
public void serialize(Object value, JsonGenerator gen, SerializerProvider ctxt) throws IOException | ||
{ | ||
gen.writeStartObject(value, 0); | ||
gen.writeEndObject(); | ||
} | ||
|
||
@Override | ||
public void serializeWithType(Object value, JsonGenerator gen, | ||
SerializerProvider ctxt, TypeSerializer typeSer) throws IOException | ||
{ | ||
WritableTypeId typeIdDef = typeSer.writeTypePrefix(gen, | ||
typeSer.typeId(value, JsonToken.START_OBJECT)); | ||
typeSer.writeTypeSuffix(gen, typeIdDef); | ||
} | ||
|
||
@Override | ||
public boolean isEmpty(SerializerProvider provider, Object value) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public JsonNode getSchema(SerializerProvider provider, Type typeHint) | ||
throws JsonMappingException { | ||
return null; | ||
} | ||
|
||
@Override | ||
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, | ||
JavaType typeHint) | ||
throws JsonMappingException | ||
{ | ||
/*JsonObjectFormatVisitor v =*/ visitor.expectObjectFormat(typeHint); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters