Skip to content

Commit

Permalink
improved kotlin support, for issue #2069
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Dec 23, 2023
1 parent e7f6244 commit a9e0ec6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
16 changes: 14 additions & 2 deletions core/src/main/java/com/alibaba/fastjson2/util/BeanUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,10 @@ public static void getters(Class objectClass, Consumer<Method> methodConsumer) {
}

public static void getters(Class objectClass, Class mixinSource, Consumer<Method> methodConsumer) {
getters(objectClass, mixinSource, false, methodConsumer);
}

public static void getters(Class objectClass, Class mixinSource, boolean kotlin, Consumer<Method> methodConsumer) {
if (objectClass == null) {
return;
}
Expand Down Expand Up @@ -955,7 +959,7 @@ && findAnnotation(method, JSONField.class) == null) {
if (firstChar >= 'a' && firstChar <= 'z' && methodNameLength == 4) {
nameMatch = false;
}
} else if (returnClass == boolean.class || returnClass == Boolean.class) {
} else if (returnClass == boolean.class || returnClass == Boolean.class || kotlin) {
nameMatch = methodNameLength > 2 && methodName.startsWith("is");
if (nameMatch) {
char firstChar = methodName.charAt(2);
Expand Down Expand Up @@ -1143,7 +1147,15 @@ public static String setterName(String methodName, int prefixLength) {
}

public static String getterName(Method method, String namingStrategy) {
String fieldName = getterName(method.getName(), namingStrategy);
String methodName = method.getName();
if (methodName.startsWith("is")) {
Class<?> returnType = method.getReturnType();
if (returnType != Boolean.class && returnType != boolean.class) {
return methodName;
}
}

String fieldName = getterName(methodName, namingStrategy);

if (fieldName.length() > 2
&& fieldName.charAt(0) >= 'A' && fieldName.charAt(0) <= 'Z'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ boolean record = BeanUtils.isRecord(objectClass);

Class mixIn = provider.getMixIn(objectClass);

BeanUtils.getters(objectClass, mixIn, method -> {
BeanUtils.getters(objectClass, mixIn, beanInfo.kotlin, method -> {
fieldInfo.init();
fieldInfo.features = writerFieldFeatures;
fieldInfo.format = beanInfo.format;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ boolean record = BeanUtils.isRecord(objectClass);
}

Class mixIn = provider.getMixIn(objectClass);
BeanUtils.getters(objectClass, mixIn, method -> {
BeanUtils.getters(objectClass, mixIn, beanInfo.kotlin, method -> {
fieldInfo.init();
fieldInfo.features |= writerFieldFeatures;
fieldInfo.format = beanInfo.format;
Expand Down
36 changes: 36 additions & 0 deletions core/src/test/java/com/alibaba/fastjson2/issues_1900/Issue2069.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.alibaba.fastjson2.issues_1900

import com.alibaba.fastjson2.JSON
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

class Issue2069 {
data class User(
val id: Long?,
val username: String?,
val isTrue: String?,
val isMain: Int?,
val isBoolean: Boolean?
) {
var isNormal: String? = null

override fun toString(): String {
return "User(id=$id, username=$username, isTrue=$isTrue, isMain=$isMain, isBoolean=$isBoolean, isNormal=$isNormal)"
}
}

@Test
fun test() {
val user = User(1, "lili", "", 1, true)
user.isNormal = ""
var jsonStr = JSON.toJSONString(user)
jsonStr = "{\"id\":1,\"username\":\"lili\",\"isTrue\":\"\",\"isMain\":1,\"isBoolean\": true,\"isNormal\":\"\"}"
val parseObject = JSON.parseObject(jsonStr, User::class.java)
assertEquals(parseObject.id, parseObject.id);
assertEquals(parseObject.username, parseObject.username);
assertEquals(parseObject.isMain, parseObject.isMain);
assertEquals(parseObject.isBoolean, parseObject.isBoolean);
assertEquals(parseObject.isTrue, parseObject.isTrue);
assertEquals(parseObject.isNormal, parseObject.isNormal);
}
}

0 comments on commit a9e0ec6

Please sign in to comment.