Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

反序列化枚举,错误的枚举值不抛异常,版本:1.2.36 #1393

Open
x403368945 opened this issue Aug 9, 2017 · 6 comments
Open
Labels
Milestone

Comments

@x403368945
Copy link

一个错误的枚举值,反序列化时不抛异常

@x403368945
Copy link
Author

x403368945 commented Aug 10, 2017

下面是测试案例代码:

public enum Code{
	SUCCESS, FAILURE
}

public static class Result{
	private int id;
	private Code code;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public Code getCode() {
		return code;
	}
	public void setCode(Code code) {
		this.code = code;
	}
	@Override
	public String toString() {
		return JSON.toJSONString(this);
	}
}

public static void main(String[] args) {
	String jsonText = "";
	jsonText = "{\"code\":\"SUCCESS\",\"id\":1}";
	System.out.println(JSON.parseObject(jsonText, Result.class));
	jsonText = "{\"code\":\"FAILURE\",\"id\":2}";
	System.out.println(JSON.parseObject(jsonText, Result.class));
	jsonText = "{\"code\":\"ERROR\",\"id\":3}";
	System.out.println(JSON.parseObject(jsonText, Result.class));
}

测试结果:
{"code":"SUCCESS","id":1}
{"code":"FAILURE","id":2}
{"id":3}

@x403368945 x403368945 changed the title 反序列化枚举 反序列化枚举,错误的枚举值不抛异常 Aug 11, 2017
@x403368945 x403368945 changed the title 反序列化枚举,错误的枚举值不抛异常 反序列化枚举,错误的枚举值不抛异常,版本:1.2.36 Aug 11, 2017
@xiaotown123
Copy link

1.2.40也发现了同样的问题,而且当结构化Json后就会抛JSONException异常,不结构化是不抛异常。

@xiaotown123
Copy link

xiaotown123 commented Feb 2, 2018

非法枚举值不抛异常问题,发现只有在JsonArray中对象的key和value之间有空格的话就会抛JSONException异常,没空格的话不抛异常,赋了null值。@wenshao

@wenshao wenshao added the bug label Feb 2, 2018
@wenshao wenshao added this to the 1.2.46 milestone Feb 2, 2018
@wenshao
Copy link
Member

wenshao commented Feb 2, 2018

最初抛错的,后来改为不抛错,但改的不够彻底(比如你说的有空格的场景)。不抛错有什么问题?

@xiaotown123
Copy link

@wenshao 感谢回复,这个问题会导致我们程序在处理有空格json和无空格json的行为不一致,在1.2.40版本中有没有什么方法可以使fastJson处理非法值时都抛异常?

@holiday12138
Copy link

希望能有个非法值抛异常的方式

@felix0813 felix0813 mentioned this issue May 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants