Skip to content
New issue

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

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

Already on GitHub? # to your account

[BUG] PropertyNamingStrategy does not apply when deserializing JSON string into an Object #2478

Open
SergiusSidorov opened this issue Apr 23, 2024 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@SergiusSidorov
Copy link

问题描述

简要描述您碰到的问题。

When serializing and deserializing I use the PropertyNamingStrategy (for example, LowerCaseWithUnderScores). It works fine when serializing Object to Json string. But when deserializing from a Json string to an Object, it does not work.

环境信息

请填写以下信息:

  • OS信息:MacOS 14.4.1, Apple M1 Pro, 32Gb
  • JDK信息: openjdk 21.0.2 2024-01-16 LTS
  • 版本信息:FastJson 2.0.49

重现步骤

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

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.PropertyNamingStrategy;
import com.alibaba.fastjson2.filter.NameFilter;
import org.junit.jupiter.api.Test;

public class FastJsonSerializationTest {
    @Test
    public void jsonFastSerializationTest() {
        var namingStrategy = NameFilter.of(PropertyNamingStrategy.LowerCaseWithUnderScores);

        var user = new User(123L, "First", "Last");

        var userAsJsonString = JSON.toJSONString(user, namingStrategy);

        System.out.println(userAsJsonString); // {"first_name":"First","id":123,"last_name":"Last"} -> Ok

        var deserializedUser = JSON.parseObject(userAsJsonString, User.class, namingStrategy);

        System.out.println(deserializedUser); // UserTest{id=123, firstName='null', lastName='null'} -> Bug!!!
    }

    public static class User {
        private Long id;
        private String firstName;
        private String lastName;

        public User(Long id, String firstName, String lastName) {
            this.id = id;
            this.firstName = firstName;
            this.lastName = lastName;
        }

        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public String getFirstName() {
            return firstName;
        }

        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }

        public String getLastName() {
            return lastName;
        }

        public void setLastName(String lastName) {
            this.lastName = lastName;
        }

        @Override
        public String toString() {
            return "UserTest{" +
                    "id=" + id +
                    ", firstName='" + firstName + '\'' +
                    ", lastName='" + lastName + '\'' +
                    '}';
        }
    }
}

期待的正确结果

对您期望发生的结果进行清晰简洁的描述。

The naming strategy should work both ways (serialization/deserialization)

@SergiusSidorov SergiusSidorov added the bug Something isn't working label Apr 23, 2024
@yanxutao89 yanxutao89 self-assigned this Apr 23, 2024
@wenshao
Copy link
Member

wenshao commented Apr 27, 2024

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.50-SNAPSHOT/
问题已修复,请帮忙用2.0.50-SNAPSHOT版本验证

@wenshao wenshao added the fixed label Apr 27, 2024
@wenshao wenshao added this to the 2.0.50 milestone Apr 27, 2024
@wenshao
Copy link
Member

wenshao commented May 12, 2024

https://github.com/alibaba/fastjson2/releases/tag/2.0.50
2.0.50已发布,请用新版本

@wenshao wenshao closed this as completed May 12, 2024
@wenshao wenshao modified the milestones: 2.0.50, 2.0.52 May 13, 2024
@wenshao wenshao reopened this May 13, 2024
@wenshao wenshao removed the fixed label May 13, 2024
@wenshao wenshao modified the milestones: 2.0.52, 2.0.53 Jul 7, 2024
@wenshao wenshao modified the milestones: 2.0.53, 2.0.54 Sep 16, 2024
@wenshao wenshao modified the milestones: 2.0.54, 2.0.55 Jan 1, 2025
@wenshao wenshao added fixed and removed fixed labels Feb 13, 2025
@wenshao wenshao removed this from the 2.0.55 milestone Feb 13, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants