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]2.0.14定制java.sql.Date, java.sql.Time, java.util.Date序列化格式失效 #781

Closed
leonchen83 opened this issue Sep 19, 2022 · 4 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@leonchen83
Copy link

leonchen83 commented Sep 19, 2022

问题描述

简要描述您碰到的问题。

环境信息

请填写以下信息:

  • OS信息: win10
  • JDK信息: jdk11
  • 版本信息:2.0.14

重现步骤

如何操作可以重现该问题:

public class Main {
	public static void main(String[] args) {
		JSON.register(java.sql.Date.class, new ObjectWriter<java.sql.Date>() {
			@Override
			public void write(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) {
				Date v = Objects.cast(object);
				jsonWriter.writeString(String.valueOf(v.getTime()));
			}
		});
		
		Test t = new Test();
		t.setDate(new Date(System.currentTimeMillis()));
		
		String s = JSON.toJSONString(t);
		System.out.println(s);
	}
	
	static class Test {
		java.sql.Date date;

		public Date getDate() {
			return date;
		}

		public void setDate(Date date) {
			this.date = date;
		}
	}
}

输出

{"date":"2022-09-19 11:23:01.586"}

期待输出

{"date":"1663557803850"}

java.sql.Time,java.util.Date也是如此

@leonchen83 leonchen83 added the bug Something isn't working label Sep 19, 2022
@leonchen83 leonchen83 changed the title [BUG]2.0.14定制java.sql.Date, java.sql.Time序列化格式失效 [BUG]2.0.14定制java.sql.Date, java.sql.Time, java.util.Date序列化格式失效 Sep 19, 2022
@wenshao
Copy link
Member

wenshao commented Sep 19, 2022

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.15-SNAPSHOT/
问题已修复,请帮忙用2.0.15-SNAPSHOT验证,2.0.15版本预计在10月7日前发布

你也可以用一下的代码,更简洁:

   @Test
    public void test1() {
        JSON.mixIn(Date.class, DateMixin.class);

        long millis = 1663595616049L;
        Bean1 t = new Bean1();
        t.setDate(new Date(millis));

        String s = JSON.toJSONString(t);
        assertEquals("{\"date\":1663595616049}", s);
    }

    @JSONType(format = "millis")
    public static class DateMixin {
    }

    static class Bean1 {
        Date date;

        public Date getDate() {
            return date;
        }

        public void setDate(Date date) {
            this.date = date;
        }
    }

@leonchen83
Copy link
Author

@wenshao java.util.Date仍然没有显示时间戳

import java.lang.reflect.Type;
import java.util.Date;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.writer.ObjectWriter;

import cn.nextop.gadget.core.util.Objects;

/**
 * @author Baoyi Chen
 */
public class Main {
	public static void main(String[] args) {
		JSON.register(java.util.Date.class, new ObjectWriter<java.util.Date>() {
			@Override
			public void write(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) {
				Date v = Objects.cast(object);
				jsonWriter.writeString(String.valueOf(v.getTime()));
			}
		});
		
		Test t = new Test();
		t.setDate(new Date(System.currentTimeMillis()));
		
		String s = JSON.toJSONString(t);
		System.out.println(s);
	}
	
	static class Test {
		java.util.Date date;

		public Date getDate() {
			return date;
		}

		public void setDate(Date date) {
			this.date = date;
		}
	}
}

@leonchen83
Copy link
Author

@wenshao 麻烦看下上面的消息

@wenshao
Copy link
Member

wenshao commented Oct 5, 2022

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

@wenshao wenshao closed this as completed Oct 5, 2022
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