我试图反序列化一个流,但我总是得到这个错误"在解析完成之前遇到的流结束"?
这是代码:
//Some code here
BinaryFormatter b = new BinaryFormatter();
return (myObject)b.Deserialize(s);//s---> is a Stream object that has been fill up with data some line over here
Run Code Online (Sandbox Code Playgroud)
有谁有想法?
@Controller)@Scope("session").Serializable以便在服务器重新启动时可以物理存储它们Serializable,这意味着它所引用的所有服务(其他spring bean)也将被序列化.它们通常是代理,引用了交易管理器,实体管理器工厂等.ApplicationContext通过实现来保持对引用的引用并不是不太可能ApplicationContextAware,因此这可以有效地意味着整个上下文被序列化.并且鉴于它拥有许多连接 - 即不能通过思想序列化的东西,它将在腐败状态下恢复.到目前为止,我大多忽略了这些问题.最近我想到声明我所有的spring依赖项transient并readResolve()通过静态实用程序类将它们重新WebApplicationContextUtils导入,并将请求/ ServletContext保存在ThreadLocal.这很乏味,但它保证了,当反序列化对象时,它的依赖关系将与当前应用程序上下文"保持同步" .
是否有任何公认的做法,或任何序列化春季背景部分的指南.
请注意,在JSF中,托管bean(~controller)是有状态的(与基于动作的Web框架不同).所以也许我的问题更多的是JSF,而不是spring-mvc.
当你使用类Exception扩展一个类(用于创建新的异常)时,你会得到一个警告serialVersionUID.我知道serialVersionUID在序列化和反序列化时它起着重要作用,但是当我的异常需要被序列化时?任何人都可以给我一个实际案例,我希望我的自定义异常类具有序列化和反序列化?
java serialization exception serialversionuid deserialization
我正在将.NET Framework C#类库移植到可移植类库.一个反复出现的问题是如何处理使用该[Serializable]属性修饰的类,因为此属性不是可移植类库子集的一部分.相反,可移植类库子集中的序列化功能似乎由DataContractAttribute覆盖.
[Serializable]用[DataContract]属性替换就足够了(暗示所有需要序列化的字段和属性也需要进行修饰[DataMember])?[Serializable]应用?鉴于[DataContract]并[DataMember]使用,我正在考虑改变代码沿着以下行.这种方法有明显的缺陷吗?有没有办法制定同样的东西,而不是那么冗长?
#if PORTABLE
[DataContract]
#else
[Serializable]
#endif
public class SerializableClass : SerializableBaseClass
{
...
#if !PORTABLE
protected SerializableClass(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
#endif
...
#if PORTABLE
[DataMember]
#endif
private Type1 _serializableField;
#if PORTABLE
[DataMember]
#endif
private Type2 SerializableProperty { get; set; }
...
}
Run Code Online (Sandbox Code Playgroud) c# serialization datacontract serializable portable-class-library
我正在尝试从Django Rest Framework返回一个HttpResponse,包括来自2个链接模型的数据.模型是:
class Wine(models.Model):
color = models.CharField(max_length=100, blank=True)
country = models.CharField(max_length=100, blank=True)
region = models.CharField(max_length=100, blank=True)
appellation = models.CharField(max_length=100, blank=True)
class Bottle(models.Model):
wine = models.ForeignKey(Wine, null=False)
user = models.ForeignKey(User, null=False, related_name='bottles')
Run Code Online (Sandbox Code Playgroud)
我想要一个Bottle模型的序列化器,其中包含来自相关Wine的信息.
我试过了:
class BottleSerializer(serializers.HyperlinkedModelSerializer):
wine = serializers.RelatedField(source='wine')
class Meta:
model = Bottle
fields = ('url', 'wine.color', 'wine.country', 'user', 'date_rated', 'rating', 'comment', 'get_more')
Run Code Online (Sandbox Code Playgroud)
这不起作用.
任何想法我怎么能这样做?
谢谢 :)
python django serialization foreign-keys django-rest-framework
我正在尝试使用Json.NET反序列化一些JSON对象.但是我发现,当我反序列化一个没有我正在寻找的属性的对象时,不会抛出任何错误,但是当我访问它们时,会返回属性的默认值.重要的是我能够检测到何时反序列化了错误类型的对象.示例代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace Json_Fail_Test
{
class Program
{
[JsonObject(MemberSerialization.OptOut)]
private class MyJsonObjView
{
[JsonProperty("MyJsonInt")]
public int MyJsonInt { get; set; }
}
const string correctData = @"
{
'MyJsonInt': 42
}";
const string wrongData = @"
{
'SomeOtherProperty': 'fbe8c20b'
}";
static void Main(string[] args)
{
var goodObj = JsonConvert.DeserializeObject<MyJsonObjView>(correctData);
System.Console.Out.WriteLine(goodObj.MyJsonInt.ToString());
var badObj = JsonConvert.DeserializeObject<MyJsonObjView>(wrongData);
System.Console.Out.WriteLine(badObj.MyJsonInt.ToString());
}
}
}
Run Code Online (Sandbox Code Playgroud)
该程序的输出是:42 0
我宁愿抛出一个例外来默默地失败.没有办法检测序列化是否找不到参数?
我知道我可以使用Json对象解析数据,然后使用键值查找来检查参数,但我所使用的代码库使用上面的模式,如果可能,我希望保持一致.
当我在Laravel 5.2项目中清除缓存时,我看到以下错误消息:
[LogicException]无法为序列化准备路由[面板].使用闭包.
我认为这与路线有关
Route::get('/article/{slug}', 'Front@slug');
Run Code Online (Sandbox Code Playgroud)
与我的控制器中的特定方法相关联:
public function slug($slug) {
$article = Article::where('slug',$slug)->first();
$id = $article ->id_article ;
if ( ($article=== null) || (is_null($id)) ) return view('errors/Db');
else return view('detail')->with(array('article'=> $article, 'title'=>'My title - '.$article->title));
}`
Run Code Online (Sandbox Code Playgroud)
简而言之,从主视图中我传递$ slug,这是文章的短链接,使用$ slug,这在数据库中是唯一的,我识别记录然后将其内容传递给详细视图.
我写这个方法时没有任何问题,事实上它就像一个魅力,但在我清理缓存后,我得到了这个错误,主视图中的链接没有显示任何短代码.
我哪里做错了?
序列化概念在编程语言中的含义是什么?
当我们Serializable在类上面使用属性时,意思是什么?
{
vendors: [
{
vendor: {
id: 367,
name: "Kuhn-Pollich",
company_id: 1,
}
},
{
vendor: {
id: 374,
name: "Sawayn-Hermann",
company_id: 1,
}
}]
}
Run Code Online (Sandbox Code Playgroud)
我有一个Vendor对象,可以从单个"供应商"json中正确地反序列化,但我想将其反序列化为a Vendor[],我只是无法弄清楚如何让Jackson合作.有小费吗?
我正在尝试使用Gson将涉及多态的对象序列化/反序列化为JSON.
这是我的序列化代码:
ObixBaseObj lobbyObj = new ObixBaseObj();
lobbyObj.setIs("obix:Lobby");
ObixOp batchOp = new ObixOp();
batchOp.setName("batch");
batchOp.setIn("obix:BatchIn");
batchOp.setOut("obix:BatchOut");
lobbyObj.addChild(batchOp);
Gson gson = new Gson();
System.out.println(gson.toJson(lobbyObj));
Run Code Online (Sandbox Code Playgroud)
这是结果:
{"obix":"obj","is":"obix:Lobby","children":[{"obix":"op","name":"batch"}]}
Run Code Online (Sandbox Code Playgroud)
序列化大多的作品,除了它缺少继承成员的内容(尤其是obix:BatchIn和obixBatchout字符串丢失).这是我的基类:
public class ObixBaseObj {
protected String obix;
private String display;
private String displayName;
private ArrayList<ObixBaseObj> children;
public ObixBaseObj()
{
obix = "obj";
}
public void setName(String name) {
this.name = name;
}
...
}
Run Code Online (Sandbox Code Playgroud)
这是我继承的类(ObixOp)的样子:
public class ObixOp extends ObixBaseObj {
private String in;
private String out;
public ObixOp() { …Run Code Online (Sandbox Code Playgroud)