gpt4 book ai didi

adding new parameters to json saved in shared preferences(将新参数添加到保存在共享首选项中的json)

转载 作者:bug小助手 更新时间:2023-10-28 13:44:45 25 4
gpt4 key购买 nike



In my Android application, after converting a custom data class to a string as json, I save it as shared preferences. In the new version, I need to add a new parameter to the data class here and check it and display it on the screen where relevant.
Will users of the old version experience a crash when I release the new version of the application with the added parameter?
Or what are the possible scenarios. Does anyone have an idea?

在我的Android应用程序中,在将定制数据类转换为字符串json后,我将其保存为共享首选项。在新版本中,我需要在这里向数据类添加一个新参数,并检查它并在相关的屏幕上显示它。当我使用添加的参数发布新版本的应用程序时,旧版本的用户是否会遇到崩溃?或者可能的情况是什么。有谁有主意吗?


I am using Gson to convert data class to json and back to data class.

我正在使用GSON将数据类转换为json,然后再转换回数据类。


更多回答
优秀答案推荐

Most likely this will not be an issue because Gson will not fail if the value for a field is missing (see related Gson feature request). However, your code has to account for the new field being null (or for primitive types having the default value) if the data comes from a previous version of your app and no value for this field is present.

这很可能不是问题,因为如果字段的值丢失,GSON不会失败(请参阅相关的GSON功能请求)。但是,如果数据来自应用程序的以前版本,并且没有该字段的值,则您的代码必须考虑新字段为空(或具有缺省值的基元类型)。


You can also assign a default value to the field (e.g. public String newField = "default";) which is used if the field is missing in the JSON data, but in that case you have to make sure your class is static (explicitly, or implicitly) and has a no-args constructor. Otherwise you could run into issues with Gson's usage of JDK Unsafe (see also GsonBuilder.disableJdkUnsafe()).

您还可以为该字段分配一个缺省值(例如,公共字符串Newfield=“Default”;),如果该字段在JSON数据中缺失,则使用该缺省值,但在这种情况下,您必须确保您的类是静态的(显式或隐式),并且有一个无参数的构造函数。否则,您可能会遇到Gson使用JDK不安全的问题(另请参阅GsonBuilder.disableJdkUnsafe())。


Since this is an Android app, you also have to make sure you had configured ProGuard or R8 properly in the past, or used @SerializedName. Otherwise your JSON data might use obfuscated field names such as a, b... and those names might change in the new version of your app, which would prevent it from reading the JSON data. See the Gson Troubleshooting Guide for more information.

由于这是一款Android应用程序,你还必须确保你过去正确配置了ProGuard或R8,或者使用了@SerializedName。否则,您的JSON数据可能会使用模糊的字段名称,如a,b…这些名称可能会在新版本的应用程序中更改,这将阻止它读取JSON数据。有关详细信息,请参阅《GSON故障排除指南》。



You can make new parameters is nullable, so the old user will not crashed until parsing.

你可以让新的参数为空,这样旧的用户在解析之前不会崩溃。


or if you use Gson you can create the TypeAdapter for data class and do logic when parse json string to make sure it succeed.

或者,如果您使用gson,您可以为数据类创建TypeAdapter,并在解析json字符串时执行逻辑,以确保它成功。


Let check: https://www.tutorialspoint.com/gson/gson_custom_adapters.htm

让我们来看看:https://www.tutorialspoint.com/gson/gson_custom_adapters.htm


更多回答

I tested it and noticed that the new field came null. I made sure it wouldn't crash on older version users. Thank you.

我对其进行了测试,注意到新字段为空。我确保它不会在老版本的用户上崩溃。谢谢。

dont paste broken links here!

请不要在此粘贴断开的链接!

has fixed, btw this link dont end with html :D

已修复,顺便说一下,此链接不以html:d结尾

I have tested. And I noticed that it didn't crash while parsing. Thank you for your reply :)

我已经测试过了。我注意到它在解析时没有崩溃。感谢您的回复:)

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com