使用 GSON 将 JSON 样式属性名称转换为 Java CamelCase 名称

发布于 2024-08-23 18:58:15 字数 289 浏览 12 评论 0原文

我正在使用 GSON 将获取的 JSON 数据转换为 Java 对象。它在我的所有测试中都表现良好。 问题是我们的真实对象有一些名为 is_online 的属性。 GSON 仅在名称完全相等时才映射它们,最好让 GSON 将名称转换为 Java 驼峰式命名法 isOnline。

在创建 JSON 数据时似乎这是可能的,驼峰式大小写会转换为 JSON 中下划线分隔的单词。但我找不到一种方法来反过来指定这一点。

I'm using GSON to convert JSON data I get to a Java object. It works pretty well in all my tests.
The problem is that our real objects have some properties named like is_online. GSON only maps them if they are named totally equal, it would be nice to have GSON convert the names to Java camel case isOnline.

It seems this is possible while creating the JSON data, camel case is converted to underscore separated words in JSON. But I can't find a way to specify this the other way round.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

守护在此方 2024-08-30 18:58:15

我发现以下设置在读取带有下划线属性的 json 并在模型中使用驼峰式命名时非常有效。

Gson gson = new GsonBuilder()
    .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
    .create()

I have found the following setting works perfect when reading json with underscored attributes and using camelcasing in my models.

Gson gson = new GsonBuilder()
    .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
    .create()
一页 2024-08-30 18:58:15

您可以使用 SerializedName 注释:

@SerializedName("field_name_in_json")
private final String fieldNameInJava;

注意:当您已经设置了 FieldNamingPolicy 时,SerializedName 将覆盖该特定字段的设置(相当对于特殊情况很方便)。

You can use the SerializedName annotation:

@SerializedName("field_name_in_json")
private final String fieldNameInJava;

Note: When you have set a FieldNamingPolicy already, SerializedName will overwrite its settings for that specific field (quite handy for special cases).

饮湿 2024-08-30 18:58:15

请记住,您的示例是一种边缘情况。如果你有一个属性“foo”,它的 getter 应该命名为“getFoo”,如果你有一个名为“foo_bar”的属性,它的 getter 应该命名为“getFooBar”,但是,在你的示例中,你映射的是一个布尔值,并且布尔值有java 中的特殊情况命名约定。名为 online 的原始布尔属性应该有一个名为“isOnline”的 getter,而不是“getOnline”,甚至更糟糕的是“getIsOnline”。布尔包装对象(即 Boolean)不应该遵循这种特殊情况,并且名为“online”的属性应该有一个名为“getOnline”的 getter。

因此,名称中带有“is”的布尔属性是一种边缘情况,您需要在转换过程中删除这个特定的前缀。在相反的方向上,您的代码可能想要检查 json 对象的原始属性名称以及“is_XXX”版本。

Bear in mind your example is an edge case. If you have a property 'foo' its getter should be named 'getFoo', and if you have a property named 'foo_bar' its getter should be named 'getFooBar', however, in your example you're mapping a boolean and booleans have special case naming conventions in java. A primitive boolean property named online should have a getter named 'isOnline', NOT 'getOnline' or even worse, 'getIsOnline'. A boolean wrapper object (i.e. Boolean) should not follow this special case and a property named 'online' should have a getter named 'getOnline'.

Hence, having boolean properties with 'is' in the name is an edge case, where you'll want to strip out this particular prefix during your conversion. In the reverse direction, your code may want to inspect the json object for both a raw property name as well as a 'is_XXX' version.

装迷糊 2024-08-30 18:58:15

我想你想要的是这里< /a>.使用注释,您可以告诉 GSON mySuperCoolField 实际上在 JSON 中称为 this_field_is_fun 并且它会正确解压它。至少我认为它也适用于反序列化。

如果这不起作用,您可以使用自定义 JsonSerializer/JsonDeserializers,它们效果很好,但是您必须针对类中的更改(例如添加字段时)更新它们。你失去了自动魔法。

最简单的事情(这会很难看,但如果第一个建议不起作用,那么非常干净和简单)就是简单地以让 GSON 满意的方式命名字段,并使用您喜欢的名称添加额外的访问器方法,例如

public boolean isXXX() {return this.is_XXX;}

I think what you want is here. Using annotations you can tell GSON that the mySuperCoolField is actually called this_field_is_fun in the JSON and it will unpack it correctly. At least I think it works for deserialization too.

If that doesn't work, you can use custom JsonSerializer/JsonDeserializers, which work great, but you have to update them for changes in your class (like when you add a field). You lose the auto-magic.

The easiest thing to do (which would be ugly, but very clean and simple if the first suggestion doesn't work) would be to simply name the field in a way to make GSON happy, and add extra accessor methods with the names you like, e.g.

public boolean isXXX() {return this.is_XXX;}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文