Skip to content

Commit

Permalink
support serialize jdbc struct, for issue #2205
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jan 27, 2024
1 parent d638047 commit 4cafb9a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
6 changes: 6 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@
<artifactId>hessian</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.dameng</groupId>
<artifactId>DmJdbcDriver18</artifactId>
<version>8.1.3.62</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
Expand Down
8 changes: 7 additions & 1 deletion core/src/main/java/com/alibaba/fastjson2/util/BeanUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public static void declaredFields(Class objectClass, Consumer<Field> fieldConsum
return;
}

if (ignore(objectClass) || objectClass.getName().contains("$$Lambda")) {
if (ignore(objectClass) || objectClass.getName().contains("$$Lambda") || JdbcSupport.isStruct(objectClass)) {
return;
}

Expand Down Expand Up @@ -844,6 +844,7 @@ public static void getters(Class objectClass, Class mixinSource, boolean kotlin,
}

boolean record = isRecord(objectClass);
boolean jdbcStruct = JdbcSupport.isStruct(objectClass);

String[] recordFieldNames = null;
if (record) {
Expand Down Expand Up @@ -880,6 +881,11 @@ boolean record = isRecord(objectClass);
}

String methodName = method.getName();
if (jdbcStruct) {
if (!"getSQLTypeName".equals(methodName) && !"getAttributes".equals(methodName)) {
continue;
}
}

boolean methodSkip = false;
switch (methodName) {
Expand Down
15 changes: 15 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/util/JdbcSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import static com.alibaba.fastjson2.support.LambdaMiscCodec.*;

public class JdbcSupport {
static Class CLASS_STRUCT;
static volatile boolean CLASS_STRUCT_ERROR;

static Class CLASS_CLOB;
static volatile boolean CLASS_CLOB_ERROR;

Expand Down Expand Up @@ -686,4 +689,16 @@ public Object readObject(JSONReader jsonReader, Type fieldType, Object fieldName
);
}
}

public static boolean isStruct(Class objectClass) {
if (CLASS_STRUCT == null && !CLASS_STRUCT_ERROR) {
try {
CLASS_STRUCT = Class.forName("java.sql.Struct");
} catch (Throwable e) {
CLASS_STRUCT_ERROR = true;
}
}

return CLASS_STRUCT != null && CLASS_STRUCT.isAssignableFrom(objectClass);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.alibaba.fastjson2.issues_2200;

import com.alibaba.fastjson2.JSONFactory;
import com.alibaba.fastjson2.writer.ObjectWriterAdapter;
import com.alibaba.fastjson2.writer.ObjectWriterProvider;
import dm.jdbc.driver.DmdbStruct;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class Issue2205 {
@Test
public void test() {
ObjectWriterProvider provider = JSONFactory.getDefaultObjectWriterProvider();
ObjectWriterAdapter objectWriter = (ObjectWriterAdapter) provider.getObjectWriter(DmdbStruct.class);
assertEquals(2, objectWriter.getFieldWriters().size());
assertNotNull(objectWriter.getFieldWriter("attributes"));
assertNotNull(objectWriter.getFieldWriter("SQLTypeName"));
}
}

0 comments on commit 4cafb9a

Please sign in to comment.