Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]com.alibaba.fastjson.JSONException: can not cast to org.joda.time.DateTime, from class java.util.Date #1770

Closed
crazythinking opened this issue Aug 19, 2023 · 2 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@crazythinking
Copy link

crazythinking commented Aug 19, 2023

问题描述

当将如下json转换为包含org.joda.time.DateTime的对象时,发生如题异常;

{"data":"tp20140101","effectiveDate":1388505600000}

该对象定义如下:

public class TimelineParameter implements HasEffectiveDate, ReverseIndexInter {

	public DateTime effectiveDate;
	
	public String data;
	
	public List<InnerParameter> paramList;
	
	public TimelineParameter(String data, DateTime effectiveDate)
	{
		this.effectiveDate = effectiveDate;
		this.data = data;
	}
	
	@Override
	public Date getEffectiveDate()
	{
		return effectiveDate.toDate();
	}

	@Override
	public void setEffectiveDate(Date effectiveDate)
	{
		this.effectiveDate = new DateTime(effectiveDate);
	}

	@Override
	public List<Field> fillIndexFields() {
		List<Field> fields = Lists.newArrayList();
		try {
			fields.add(TimelineParameter.class.getField("data"));
			fields.add(TimelineParameter.class.getField("effectiveDate"));
		} catch (NoSuchFieldException e) {
			// do nothing
		}
		return fields;
	}
}

环境信息

请填写以下信息:

  • OS信息: windows
  • JDK信息:Openjdk 17
  • 版本信息:2.0.39

重现步骤

image

如上图,该方法断点处发生异常

换成1.2.83 可正常处理;

期待的正确结果

同1版本兼容,可正确转换

相关日志输出

Caused by: com.alibaba.fastjson.JSONException: can not cast to org.joda.time.DateTime, from class java.util.Date at com.alibaba.fastjson.JSON.parseObject(JSON.java:520) at net.engining.pg.parameter.JsonGuavaCachedParameterFacility.convertParameterObject(JsonGuavaCachedParameterFacility.java:93) at net.engining.pg.parameter.JsonGuavaCachedParameterFacility$1.load(JsonGuavaCachedParameterFacility.java:77) at net.engining.pg.parameter.JsonGuavaCachedParameterFacility$1.load(JsonGuavaCachedParameterFacility.java:1) at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3571) at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2313) at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2190) at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2080) ... 80 more Caused by: com.alibaba.fastjson2.JSONException: can not cast to org.joda.time.DateTime, from class java.util.Date at com.alibaba.fastjson2.util.TypeUtils.cast(TypeUtils.java:1522) at com.alibaba.fastjson2.util.TypeUtils.cast(TypeUtils.java:1404) at com.alibaba.fastjson2.reader.ConstructorFunction.apply(ConstructorFunction.java:172) at com.alibaba.fastjson2.reader.ConstructorFunction.apply(ConstructorFunction.java:18) at com.alibaba.fastjson2.reader.ObjectReaderNoneDefaultConstructor.readObject(ObjectReaderNoneDefaultConstructor.java:313) at com.alibaba.fastjson.JSON.parseObject(JSON.java:510) ... 87 more

@crazythinking crazythinking added the bug Something isn't working label Aug 19, 2023
@wenshao wenshao added this to the 2.0.40 milestone Aug 19, 2023
wenshao added a commit that referenced this issue Aug 19, 2023
@wenshao
Copy link
Member

wenshao commented Aug 19, 2023

DISABLE_STRING_UNSAFE_GET

@wenshao wenshao added the fixed label Aug 19, 2023
@wenshao
Copy link
Member

wenshao commented Sep 3, 2023

https://github.com/alibaba/fastjson2/releases/tag/2.0.40
问题已修复,请用新版本

@wenshao wenshao closed this as completed Sep 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed
Projects
None yet
Development

No branches or pull requests

2 participants