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

[QUESTION] JSON转util.Map #384

Closed
hujian0923 opened this issue May 30, 2022 · 3 comments
Closed

[QUESTION] JSON转util.Map #384

hujian0923 opened this issue May 30, 2022 · 3 comments
Labels
question Further information is requested
Milestone

Comments

@hujian0923
Copy link

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中应该怎么转换,没看到相应的兼容方法

@hujian0923 hujian0923 added the question Further information is requested label May 30, 2022
@wenshao
Copy link
Member

wenshao commented May 30, 2022

        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

@wenshao
Copy link
Member

wenshao commented May 30, 2022

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.6-SNAPSHOT/
或者你可以试用2.0.6-SNAPSHOT,在2.0.6中有更方便的写法,比如:

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());
    }
}

@wenshao wenshao added this to the 2.0.6 milestone May 30, 2022
@wenshao
Copy link
Member

wenshao commented Jun 4, 2022

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

@wenshao wenshao closed this as completed Jun 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants