Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,10 @@ public class GenericJsonSerializingTranscoder extends SpyObject implements Trans
private final int maxSize;
private final CompressionUtils cu;
private final TranscoderUtils tu;
private final boolean isCollection;
private final boolean forceJsonSerializeForCollection;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

질문입니다.
forceJDKSerializeForCollection 용어를 사용해야 하지 않는 지?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ObectMapper 기반의 Json 변환을 하고 있으므로 JDKSerialize 방식을 사용하지 않습니다.
따라서 지금과 같이 변수명을 작성했습니다.


/**
* Construct a new GenericJsonSerializingTranscoder
*
* @param objectMapper the object mapper to use. This transcoder enables
* polymorphic typing to preserve concrete types.
* Jackson polymorphic deserialization can be vulnerable
* for any untrusted JSON input if default typing is
* too permissive without proper validation.
* It is recommended to configure a restrictive
* {@code BasicPolymorphicTypeValidator}
* @param typeHintPropertyName the property name to use for type hints.
* If {@code null} is given, do not set DefaultTyping of ObjectMapper.
* If empty String is given, set DefaultTyping of ObjectMapper with
* default type property name ("@class").
* Otherwise, set DefaultTyping of ObjectMapper with given String
* to write type info into JSON.
* @param max the maximum size of a serialized object
*/
@Deprecated
public GenericJsonSerializingTranscoder(ObjectMapper objectMapper, String typeHintPropertyName,
int max) {
this(objectMapper, max);
Expand All @@ -66,6 +51,7 @@ public GenericJsonSerializingTranscoder(ObjectMapper objectMapper, String typeHi
}
}

@Deprecated
public GenericJsonSerializingTranscoder(ObjectMapper objectMapper, int max) {
if (objectMapper == null) {
throw new IllegalArgumentException("ObjectMapper must not be null");
Expand All @@ -74,13 +60,69 @@ public GenericJsonSerializingTranscoder(ObjectMapper objectMapper, int max) {
this.maxSize = max;
this.cu = new CompressionUtils();
this.tu = new TranscoderUtils(true);
this.isCollection = false;
this.forceJsonSerializeForCollection = false;
}

/**
* Constructor with full customization.
* Use static factory methods forKV() or forCollection() for default settings,
* or Builder for custom configurations.
*/
private GenericJsonSerializingTranscoder(ObjectMapper objectMapper, int max,
boolean isCollection,
boolean forceJsonSerializeForCollection) {
if (objectMapper == null) {
throw new IllegalArgumentException("ObjectMapper must not be null");
}
this.objectMapper = objectMapper;
this.maxSize = max;
this.cu = new CompressionUtils();
this.tu = new TranscoderUtils(true);
this.isCollection = isCollection;
this.forceJsonSerializeForCollection = forceJsonSerializeForCollection;
}

/**
* Factory method for general key-value usage.
*
* @param objectMapper the object mapper to use. This transcoder enables
* polymorphic typing to preserve concrete types.
* Jackson polymorphic deserialization can be vulnerable
* for any untrusted JSON input if default typing is
* too permissive without proper validation.
* It is recommended to configure a restrictive
* {@code BasicPolymorphicTypeValidator}
*/
public static Builder forKV(ObjectMapper objectMapper) {
return new Builder(objectMapper).forKV();
}

/**
* Factory method for collection item usage.
*
* @param objectMapper the object mapper to use. This transcoder enables
* polymorphic typing to preserve concrete types.
* Jackson polymorphic deserialization can be vulnerable
* for any untrusted JSON input if default typing is
* too permissive without proper validation.
* It is recommended to configure a restrictive
* {@code BasicPolymorphicTypeValidator}
*/
public static Builder forCollection(ObjectMapper objectMapper) {
return new Builder(objectMapper).forCollection();
}

@Override
public int getMaxSize() {
return maxSize;
}

@Override
public boolean isForceSerializeForCollection() {
return forceJsonSerializeForCollection;
}

/**
* Set the compression threshold to the given number of bytes. This
* transcoder will attempt to compress any data being stored that's larger
Expand Down Expand Up @@ -110,7 +152,7 @@ public Object decode(CachedData d) {
return null; // No data to decode
}

if ((d.getFlags() & COMPRESSED) != 0) {
if (!isCollection && (d.getFlags() & COMPRESSED) != 0) {
Copy link
Collaborator

@jhpark816 jhpark816 Feb 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!isCollection 조건은 없어야 할 것 같습니다.
이미 COMPRESSED 상태이면, decompress 해야 하기 때문입니다.

data = cu.decompress(data);
}

Expand Down Expand Up @@ -162,6 +204,12 @@ public CachedData encode(Object o) {
byte[] b;
int flags = 0;

if (isCollection && forceJsonSerializeForCollection) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isCollection == false이면
forceJsonSerializeForCollection == true이더라도 serialize() 하지 않는군요.

forceJsonSerializeForCollection 조건에 의해 serialize() 여부가 결정되는 것이
나을 것 같습니다.

b = serialize(o);
flags |= SERIALIZED;
return new CachedData(flags, b, getMaxSize());
}

if (o instanceof String) {
b = tu.encodeString((String) o);
} else if (o instanceof Long) {
Expand Down Expand Up @@ -193,7 +241,7 @@ public CachedData encode(Object o) {
flags |= SERIALIZED;
}
assert b != null;
if (cu.isCompressionCandidate(b)) {
if (!isCollection && cu.isCompressionCandidate(b)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isCollection은 collection element를 encode한다는 의미인가요?
collection 여부 보다는 compress 자체 기준에 따르는 것이 어떤가요?

byte[] compressed = cu.compress(b);
if (compressed.length < b.length) {
getLogger().debug("Compressed %s from %d to %d",
Expand Down Expand Up @@ -229,5 +277,77 @@ private byte[] serialize(Object o) {
throw new IllegalArgumentException("Non-serializable object, cause=" + e.getMessage(), e);
}
}

/**
* Builder for constructing GenericJsonSerializingTranscoder instances with custom settings.
*/
public static final class Builder {
private final ObjectMapper objectMapper;
private String typeHintPropertyName = "";
private int max;
private boolean isCollection;
private boolean forceJsonSerializeForCollection;

private Builder(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}

Builder forKV() {
this.max = CachedData.MAX_SIZE;
this.isCollection = false;
this.forceJsonSerializeForCollection = false;
return this;
}

Builder forCollection() {
this.max = SerializingTranscoder.MAX_COLLECTION_ELEMENT_SIZE;
this.isCollection = true;
this.forceJsonSerializeForCollection = false;
return this;
}

public Builder maxSize(int max) {
this.max = max;
return this;
}

/**
* @param typeHintPropertyName the property name to use for type hints.
* Use "@class" by default without setting this method.
* If {@code null} is given, do not set DefaultTyping
* of given ObjectMapper.
* If empty String is given, set DefaultTyping of ObjectMapper with
* default type property name ("@class").
* Otherwise, set DefaultTyping of ObjectMapper with given String
* to write type info into JSON.
*/
public Builder typeHintPropertyName(String typeHintPropertyName) {
this.typeHintPropertyName = typeHintPropertyName;
return this;
}

public Builder forceJsonSerializeForCollection() {
if (!isCollection) {
throw new IllegalStateException("forceJsonSerializationForCollection can only be " +
"used with collection transcoders");
}
this.forceJsonSerializeForCollection = true;
return this;
}

public GenericJsonSerializingTranscoder build() {
if (typeHintPropertyName != null) {
@SuppressWarnings("deprecation")
StdTypeResolverBuilder typer = new ObjectMapper.DefaultTypeResolverBuilder(
ObjectMapper.DefaultTyping.EVERYTHING, objectMapper.getPolymorphicTypeValidator());
typer = typer.init(JsonTypeInfo.Id.CLASS, null);
typer = typer.inclusion(JsonTypeInfo.As.PROPERTY);
typer = typer.typeProperty(typeHintPropertyName);
objectMapper.setDefaultTyping(typer);
}
return new GenericJsonSerializingTranscoder(objectMapper, max,
isCollection, forceJsonSerializeForCollection);
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@
*/
class GenericJsonSerializingTranscoderTest {

private final ObjectMapper mapper = new ObjectMapper();
private GenericJsonSerializingTranscoder tc;

@BeforeEach
void setUp() {
tc = new GenericJsonSerializingTranscoder(mapper, "", CachedData.MAX_SIZE);
tc = GenericJsonSerializingTranscoder
.forKV(new ObjectMapper())
.build();
}

@Test
Expand Down Expand Up @@ -239,14 +240,17 @@ void testJsonObject() {

@Test
void testObjectMapperWithoutDefaultTyping() {
ObjectMapper objectMapper = new ObjectMapper();
GenericJsonSerializingTranscoder tcWithoutDefaultTyping
= new GenericJsonSerializingTranscoder(objectMapper, null, CachedData.MAX_SIZE);
GenericJsonSerializingTranscoder tcWithoutDefaultTyping =
GenericJsonSerializingTranscoder
.forKV(new ObjectMapper())
.typeHintPropertyName(null)
.build();
TestPojo pojo = new TestPojo("test", 123);

CachedData cd = tcWithoutDefaultTyping.encode(pojo);
assertEquals(TranscoderUtils.SERIALIZED, cd.getFlags());
assertFalse(new String(cd.getData()).contains("@class"));
String s = new String(cd.getData());
assertFalse(s.contains("@class"));

assertThrows(ClassCastException.class, () -> {
TestPojo decoded = (TestPojo) tcWithoutDefaultTyping.decode(cd);
Expand Down Expand Up @@ -392,7 +396,10 @@ void testEnum() {
@Test
void testCustomTypePropertyName() {
GenericJsonSerializingTranscoder custom =
new GenericJsonSerializingTranscoder(new ObjectMapper(), "type", CachedData.MAX_SIZE);
GenericJsonSerializingTranscoder
.forKV(new ObjectMapper())
.typeHintPropertyName("type")
.build();
TestPojo p = new TestPojo("abc", 1);
CachedData cd = custom.encode(p);
String json = new String(cd.getData(), StandardCharsets.UTF_8);
Expand Down

This file was deleted.

Loading