我有以下场景:我正在使用WebAPI并根据模型将JSON结果返回给使用者.我现在还需要将模型序列化为base64,以便能够将它们保存在缓存中和/或将它们用于审计目的.问题是当我将[Serializable]属性添加到模型以便将模型转换为Base64时,JSON输出会发生如下变化:
该模型:
[Serializable]
public class ResortModel
{
public int ResortKey { get; set; }
public string ResortName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
如果没有该[Serializable]属性,则JSON输出为:
{
"ResortKey": 1,
"ResortName": "Resort A"
}
Run Code Online (Sandbox Code Playgroud)
使用[Serializable]JSON输出的属性是:
{
"<ResortKey>k__BackingField": 1,
"<ResortName>k__BackingField": "Resort A"
}
Run Code Online (Sandbox Code Playgroud)
如何在[Serializable]不更改JSON输出的情况下使用该属性?
我正在编写一个可序列化的类,它带有几个参数,包括Function:
public class Cls implements Serializable {
private final Collection<String> _coll;
private final Function<String, ?> _func;
public Cls(Collection<String> coll, Function<String, ?> func) {
_coll = coll;
_func = func;
}
}
Run Code Online (Sandbox Code Playgroud)
func存储在成员变量中,因此需要可序列化.如果它们被分配的类型是可序列化的,则 Java lambdas 是可序列化的.Function如果使用lambda创建,那么确保我在构造函数中传递的最佳方法是可序列化的?
创建一个SerializableFunction类型并使用它:
public interface SerializableFunction<F, R> implements Function<F, R>, Serializable {}
....
public Cls(Collection<String> coll, SerializableFunction<String, ?> func) {...}
Run Code Online (Sandbox Code Playgroud)
问题:
coll和func参数之间存在不匹配,在func签名中声明为可序列化,但coll不是,但两者都需要可序列化才能工作.Function可序列化.在构造函数上使用类型参数:
public <F extends Function<String, …Run Code Online (Sandbox Code Playgroud)使用XMLRoot/XMLElement和使用Serializable()属性有什么区别?我怎么知道何时使用?
我有一个标有[Serializable]的类.当我从Web API返回它时,字段名称都很时髦.
通常返回的JSON是
[{"OrderId":797 ...
Run Code Online (Sandbox Code Playgroud)
使用[Serializable]时返回JSON
[{"<OrderId>k__BackingField":797 ...
Run Code Online (Sandbox Code Playgroud)
我不想将其标记为可序列化以使用BinaryFormatter进行缓存.除了编写自定义序列化程序或制作一个不可序列化的双胞胎并编写猴子代码以在两者之间"施放"之外,还有其他方法吗?
我刚刚学习如何开发Android Wear,我已经为智能手表创建了一个全屏活动,在我的应用程序的移动部分,我获得了一些JSON数据,并从中创建了一个自定义对象列表.
在我的移动应用程序中,我在ListView中显示这些对象的信息.
在我的应用程序的Wear部分,我想显示此列表的限制版本,例如列表中的前3个将显示在Wearable上的全屏应用程序上.
我的问题是,似乎没有办法将Parcelable Objects发送到Android Wear,因此在DataItem中没有putParcelable选项.
看起来唯一的选择是以字节为单位发送对象,如下所示:
public void sendWearableData(GoogleApiClient aGoogleApiClient, ArrayList<MyObject> myObjectList, String path)
{
googleApiClient = aGoogleApiClient;
byte[] testBytes = new byte[0];
if (googleApiClient.isConnected()) {
PutDataMapRequest dataMapRequest = PutDataMapRequest.create(path);
try {
testBytes = BytesUtils.toByteArray(myObjectList);
} catch (IOException e) {
e.printStackTrace();
}
dataMapRequest.getDataMap().putByteArray(Constants.TEST_KEY, testBytes);
PutDataRequest request = dataMapRequest.asPutDataRequest();
Wearable.DataApi.putDataItem(googleApiClient, request);
}
}
Run Code Online (Sandbox Code Playgroud)
所以我必须将我的对象转换为字节,将其发送到Android Wear并将其转换回来?这意味着我已经实现了Parcelable的对象,所以我现在可以通过Intents发送它们也需要实现Serializable,这是正确的还是有更好的方法呢?
android serializable parcelable android-wear-data-api wear-os
为了更好地控制序列化,我将一个类从[DataContract]转换为[Serializable],实现了GetObjectData和特殊的反序列化构造函数.当我这样做时,现在发出的XML具有应用于所有元素的类型信息.我不想要这些多余的信息,我想知道如何通知序列化程序不输出它.
以下是使用[DataContract]的示例代码:
[DataContract(Namespace = "")]
class Test
{
public Test() { }
[DataMember]
public Nullable<int> NullableNumber = 7;
[DataMember]
public int Number = 5;
public static void Go()
{
var test = new Test();
var dcs = new DataContractSerializer(typeof(Test));
using (var s = new StreamWriter("test.xml"))
{
dcs.WriteObject(s.BaseStream, test);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这将输出以下XML(请注意Nullable Number和Number上没有类型信息 - 这是所需的输出):
<Test xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<NullableNumber>7</NullableNumber>
<Number>5</Number>
</Test>
Run Code Online (Sandbox Code Playgroud)
如果我修改上面的代码如下(添加[Serializable],:ISerializable和两个序列化方法):
[Serializable]
class Test : ISerializable
{
public Test() { }
public Nullable<int> NullableNumber = 7;
public int Number = …Run Code Online (Sandbox Code Playgroud) 我今天遇到了这个问题,并且能够确定在进行代码清理时,R#不会将属性从支持字段转换为使用SerializableAttribute修饰的类中的自动属性,例如
using System;
namespace DataContracts
{
[Serializable]
public class Class1
{
private bool _wontChange;
public bool WontChange
{
get { return _wontChange; }
set { _wontChange = value; }
}
}
}
Run Code Online (Sandbox Code Playgroud)
在自动代码清理期间,不会更改上述代码.当然,我可以手动执行此操作,我仍然可以从R#获取快速操作菜单选项,以便在单个属性级别执行此操作.但它让我想知道在[Serializable]类中使用自动属性是否存在我不知道的潜在问题.
在JetBrains论坛帖子中,我们提到了一个讨论这个问题的问题,但似乎没有明确解决.
在C#中,考虑我们有一个泛型类和一个具体类
[Serializable]
public class GenericUser
{ ...
[Serializable]
public class ConcreteUser : GenericUser
{ ...
Run Code Online (Sandbox Code Playgroud)
是否有必要将ConcreteUser标记为[Serializable]或继承将处理它?
执行.aspx页面时,我收到以下错误.你能告诉我怎样才能解决它.
Unable to serialize the session state. In 'StateServer' and 'SQLServer' mode, ASP.NET will serialize the session state objects, and as a result non-serializable objects or MarshalByRef objects are not permitted. The same restriction applies if similar serialization is done by the custom session state store in 'Custom' mode.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. …Run Code Online (Sandbox Code Playgroud) 我想将 json 字符串响应从 API 转换为对象:
val obj = Json.decodeFromString<MyModel>(jsonResponseString)
Run Code Online (Sandbox Code Playgroud)
我的数据类:
@Serializable
data class MyModel(
@SerializedName("field") val field: String
)
Run Code Online (Sandbox Code Playgroud)
它看起来非常简单,并且可以在调试模式下运行!
但是,当编译 AppBundle、以发布模式构建并从 Play Store 内部测试下载应用程序时,我收到以下错误:
Serializer for class '...' is not found. Mark the class as @serializable or provide the
serializer explicitly.
kotlinx.serialization.internal.Platform_commonKt.serializerNotRegistered
Run Code Online (Sandbox Code Playgroud)