-
Notifications
You must be signed in to change notification settings - Fork 495
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
[QUESTION] JSON转util.Map #384
Comments
String str = "{}";
{
JSONReader jsonReader = JSONReader.of(str);
jsonReader.getContext().setObjectSupplier(HashMap::new);
assertEquals(HashMap.class, jsonReader.readObject().getClass());
}
{
JSONReader jsonReader = JSONReader.of(str);
jsonReader.getContext().setObjectSupplier(HashMap::new);
assertEquals(HashMap.class, jsonReader.read(Object.class).getClass());
}
{
JSONReader jsonReader = JSONReader.of(str);
jsonReader.getContext().setObjectSupplier(HashMap::new);
assertEquals(HashMap.class, jsonReader.read(Map.class).getClass());
} 2.0.5的如上例子中,可以配置ObjectSupplier,你可以自己决定用哪个Map |
https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.6-SNAPSHOT/ assertEquals(HashMap.class, JSON.parse("{}", JSONReader.Feature.UseNativeObject).getClass());
assertEquals(HashMap.class, JSON.parseObject("{}", Object.class, JSONReader.Feature.UseNativeObject).getClass()); @Test
public void test3() {
assertEquals(
HashMap.class,
JSON.parse(
"{}",
JSONFactory.createReadContext(HashMap::new)
).getClass()
);
assertEquals(
HashMap.class,
JSON.parseObject(
"{}",
Object.class,
JSONFactory.createReadContext(HashMap::new)
).getClass()
);
assertEquals(
HashMap.class,
JSON.parseObject(
"{}".getBytes(StandardCharsets.UTF_8),
Object.class,
JSONFactory.createReadContext(HashMap::new)
).getClass()
);
{
Object parse = JSON.parse(
"[{}]",
JSONFactory.createReadContext(TreeMap::new, LinkedList::new)
);
assertEquals(LinkedList.class, parse.getClass());
assertEquals(TreeMap.class, ((List) parse).get(0).getClass());
}
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
2.x中如何将JSON转util.Map
在1.x中JSON转util.Map如下:
//JSON格式: {"a":"111","b":"222","c":"333"}
JSONObject jsonConf = ...;
Map<String, String> conf = jsonConf.toJavaObject(new TypeReference<Map<String, String>>() {});
请问在2.x中应该怎么转换,没看到相应的兼容方法
The text was updated successfully, but these errors were encountered: