如何从由 @ToString 注解生成的字符串转换为对象?

huangapple go评论64阅读模式
英文:

How to convert to object from a string generated by @ToString lombok annotation?

问题

我有一个有趣的问题要解决。

让我们考虑以下的类(*Person* 和 *Telephone*):

**Person** 
```java
@AllArgsConstructor
@Getter
@Setter
@ToString
public class Person {
    private String name;
    private int age;
    private Telephone telephone;
}

Telephone

@AllArgsConstructor
@Getter
@Setter
@ToString
public class Telephone {
    private int codeArea;
    private String number;
}

假设我有一个由 lombok 注解 @ToString 生成的 Person 对象的字符串:

Person(name=John, age=30, telephone=Telephone(codeArea=16, number=1111-2222))

我如何将上述字符串转换回对象?是否有任何库可以帮助我进行此过程,还是我必须手动创建一个解析器来完成?

我知道还有其他方法,比如生成 JSON 或将其序列化为 Java 对象,然后再转换回来。然而,对我来说现在这不是一个解决方案,因为我必须使用上面提到的字符串进行操作。


<details>
<summary>英文:</summary>

I have an interesting problem to solve.

Let&#39;s consider the following classes (*Person* and *Telephone*):

**Person** 

@AllArgsConstructor
@Getter
@Setter
@ToString
public class Person {
private String name;
private int age;
private Telephone telephone;
}


**Telephone**

@AllArgsConstructor
@Getter
@Setter
@ToString
public class Telephone {
private int codeArea;
private String number;
}


And suppose that I have a *Person* object string generated by the lombok annotation *@ToString*:

Person(name=John, age=30, telephone=Telephone(codeArea=16, number=1111-2222))


How can I convert the string above to the object again? Is there any library that can help me with this process, or do I have to create manually a parser for it?

I know that there are other ways like generating json or serializing as a java object and then converting it back. However, this is not a solution for me now, as I must have to work with the string mentioned above.



</details>


# 答案1
**得分**: 6

```java
// 在类中进行如下操作,而不是上述的操作。

/**
 * 将对象转换为 JSON 字符串。
 * @return
 */
public String toString() {
    // 初始化 GSON:Gson GSON = new GsonBuilder().create();
    return GSON.toJson(this);
}

**从对象到字符串**:object.toString()。

**从字符串到对象**:GSON.fromJson(jsonString, ModelClass);
英文:

Instead, do this in the class.

/**
 * Object as JSON string.
 * @return
 */
public String toString() {
    // Initiate GSON = new GsonBuilder().create();
    return GSON.toJson(this);
}

From Object to String : object.toString().

From String to Object : GSON.fromJson(jsonString, ModelClass);

答案2

得分: 2

最终,我想出了将这个字符串转换成JSON格式,然后再将其转换为对象的方法。

因此,由lombok生成的字符串是:

Person(name=John, age=30, telephone=Telephone(codeArea=16, number=1111-2222))

然后将其转换为JSON,通过将以下字符进行替换:(=) 分别替换为 {:},同时移除类名:

{name:John, age:30, telephone:{codeArea=16, number=1111-2222}}

现在可以将JSON转换回对象。

我在GitHub上创建了一个项目,用于执行这个转换。

英文:

Finally, I came up with the idea of converting this string into JSON format and then convert it to the object.

So, the string generated by lombok is:

Person(name=John, age=30, telephone=Telephone(codeArea=16, number=1111-2222))

And then it is converted to JSON, by replacing the following characters (, = and ) with {, : and } respectively, and removing the class names:

{name:John, age:30, telephone:{codeArea=16, number=1111-2222}}

Now it is possible to convert the JSON into an object.

I created a project in Github that does this conversion.

huangapple
  • 本文由 发表于 2020年8月30日 03:09:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63650823.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定