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 @@ -275,6 +275,12 @@ public <T> ObjectReader<T> createObjectReader(
match = false;
break;
}

if (fieldReader.fieldClass != fieldReader.fieldType
&& fieldReader.fieldClass == Number.class) {
match = false;
break;
}
}
}

Expand Down Expand Up @@ -393,6 +399,12 @@ protected <T> ObjectReaderNoneDefaultConstructor createNoneDefaultConstructorObj
match = false;
break;
}

if (fieldReader.fieldClass != fieldReader.fieldType
&& fieldReader.fieldClass == Number.class) {
match = false;
break;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.alibaba.fastjson2.issues;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Map;

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

public class Issue3156 {
@Test
Expand Down Expand Up @@ -66,39 +69,51 @@ public record ListResult<T>(int code, String msg, List<T> datas) {
public record MapListResult<K, V>(int code, String msg, Map<String, List<V>> data) {
}

// public static class AjaxResult<T> {
// private int code;
// private String msg;
// private T data;
//
// public AjaxResult() {
// }
//
// public int getCode() {
// return code;
// }
//
// public AjaxResult<T> setCode(int code) {
// this.code = code;
// return this;
// }
//
// public String getMsg() {
// return msg;
// }
//
// public AjaxResult<T> setMsg(String msg) {
// this.msg = msg;
// return this;
// }
//
// public T getData() {
// return data;
// }
//
// public AjaxResult<T> setData(T data) {
// this.data = data;
// return this;
// }
// }
@Test
public void test_2() { // 泛型 无默认构造器
String json = "{\"temperature\":{\"enabled\":true,\"value\":0.1},\"presencePenalty\":{\"enabled\":false,\"value\":0},\"frequencyPenalty\":{\"enabled\":false,\"value\":0},\"maxToken\":{\"enabled\":false,\"value\":1024}}";
Options options = JSONObject.parseObject(json, new TypeReference<Options>() {});
Object val = options.getTemperature().value();
Object val2 = options.getPresencePenalty().value();
Object val3 = options.getMaxToken().value();
assertInstanceOf(Double.class, val);
assertInstanceOf(Double.class, val2);
assertInstanceOf(Integer.class, val3);
}

@Data
public static class Options {
private Parameter<Double> temperature;
private Parameter<Double> presencePenalty;
private Parameter<Double> frequencyPenalty;
private Parameter<Integer> maxToken;
public record Parameter<T extends Number>(Boolean enabled, T value) {}
}

@Test
public void test_3() { // 泛型 有默认构造器
String json = "{\"temperature\":{\"enabled\":true,\"value\":0.1},\"presencePenalty\":{\"enabled\":false,\"value\":0},\"frequencyPenalty\":{\"enabled\":false,\"value\":0},\"maxToken\":{\"enabled\":false,\"value\":1024}}";
Options2 options = JSONObject.parseObject(json, new TypeReference<Options2>() {});
Object val = options.getTemperature().getValue();
Object val2 = options.getPresencePenalty().getValue();
Object val3 = options.getMaxToken().getValue();
assertInstanceOf(Double.class, val);
assertInstanceOf(Double.class, val2);
assertInstanceOf(Integer.class, val3);
}

@Data
public static class Options2 {
private Parameter<Double> temperature;
private Parameter<Double> presencePenalty;
private Parameter<Double> frequencyPenalty;
private Parameter<Integer> maxToken;

@NoArgsConstructor
@Data
public static class Parameter<T extends Number>{
private Boolean enabled;
private T value;
}
}
}
Loading