nin*_*edt 9 c# protobuf-net openstreetmap
尝试通过以下来自此线程以及其他来源的信息来正确运行openstreetmap pbf文件的反序列化:
我目前正在使用r480的protobug dll.我使用protogen从osm proto创建了csharp类文件,但是当我在代码中点击这一点时,我试图从流中读入:
BlockHeader header;
using (var tmp = new LimitedStream(file, length))
{
header = Serializer.Deserialize<BlockHeader>(tmp); // exception occurs here
}
Run Code Online (Sandbox Code Playgroud)
它抛出以下异常:
InnerException: System.InvalidOperationException
Message=Type is not expected, and no contract can be inferred: BlockHeader
Source=protobuf-net
StackTrace:
at ProtoBuf.Meta.TypeModel.ThrowUnexpectedType(Type type) in C:\Dev\protobuf-net\protobuf-net\Meta\TypeModel.cs:line 1115
at ProtoBuf.Meta.TypeModel.TryDeserializeAuxiliaryType(ProtoReader reader, DataFormat format, Int32 tag, Type type, Object& value, Boolean skipOtherFields, Boolean asListItem, Boolean autoCreate, Boolean insideList) in C:\Dev\protobuf-net\protobuf-net\Meta\TypeModel.cs:line 848
at ProtoBuf.Meta.TypeModel.DeserializeCore(ProtoReader reader, Type type, Object value, Boolean noAutoCreate) in C:\Dev\protobuf-net\protobuf-net\Meta\TypeModel.cs:line 582
at ProtoBuf.Meta.TypeModel.Deserialize(Stream source, Object value, Type type, SerializationContext context) in C:\Dev\protobuf-net\protobuf-net\Meta\TypeModel.cs:line 506
at ProtoBuf.Meta.TypeModel.Deserialize(Stream source, Object value, Type type) in C:\Dev\protobuf-net\protobuf-net\Meta\TypeModel.cs:line 488
at ProtoBuf.Serializer.Deserialize[T](Stream source) in C:\Dev\protobuf-net\protobuf-net\Serializer.cs:line 69
at OsmParserDemo.MainWindow.OsmParse() in C:\Users\crussell\Documents\Visual Studio 2010\Projects\OsmParseMadness\OsmParserDemo\MainWindow.xaml.cs:line 57
at OsmParserDemo.MainWindow..ctor() in C:\Users\crussell\Documents\Visual Studio 2010\Projects\OsmParseMadness\OsmParserDemo\MainWindow.xaml.cs:line 28
Run Code Online (Sandbox Code Playgroud)
因此,在比较代码结构与protobuf-net示例的结构时,我得到的印象是,我可能会遗漏一些关于识别每个成员的内容.这是正确的,还是我在这个空间的出路?非常感谢任何帮助或提示!
编辑:这是生成的FileFormat.cs类的BlockHeader片段:
Mar*_*ell 12
添加代码文件后,一切都变得清晰了.您正在使用的库,基于堆栈跟踪,是protobuf-net; 但是.cs文件与protobuf-net 无关.嗯,很少.
你看,有(至少)2个c#/ .net protobuf实现,我认为你是在混合它们:
XmlSerializer,DataContractSerializer等均可),而且还支持如果你愿意,可以使用.proto令人困惑的是,它们都具有称为"protogen"的代码生成工具.AFAIK,这个命名巧合只是自然的收敛/巧合,而不是计划(好或恶).
从protobuf-csharp-port的protogen生成的c#文件将与protobuf-csharp-port库一起使用
从protobuf-net的protogen生成的c#文件将与protobuf-net库一起使用
这是protobuf-net生成的版本,包括BlockHeader:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
// Generated from: Foo.proto
namespace ConsoleApplication9
{
[global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"Blob")]
public partial class Blob : global::ProtoBuf.IExtensible
{
public Blob() {}
private byte[] _raw = null;
[global::ProtoBuf.ProtoMember(1, IsRequired = false, Name=@"raw", DataFormat = global::ProtoBuf.DataFormat.Default)]
[global::System.ComponentModel.DefaultValue(null)]
public byte[] raw
{
get { return _raw; }
set { _raw = value; }
}
private int _raw_size = default(int);
[global::ProtoBuf.ProtoMember(2, IsRequired = false, Name=@"raw_size", DataFormat = global::ProtoBuf.DataFormat.TwosComplement)]
[global::System.ComponentModel.DefaultValue(default(int))]
public int raw_size
{
get { return _raw_size; }
set { _raw_size = value; }
}
private byte[] _zlib_data = null;
[global::ProtoBuf.ProtoMember(3, IsRequired = false, Name=@"zlib_data", DataFormat = global::ProtoBuf.DataFormat.Default)]
[global::System.ComponentModel.DefaultValue(null)]
public byte[] zlib_data
{
get { return _zlib_data; }
set { _zlib_data = value; }
}
private byte[] _lzma_data = null;
[global::ProtoBuf.ProtoMember(4, IsRequired = false, Name=@"lzma_data", DataFormat = global::ProtoBuf.DataFormat.Default)]
[global::System.ComponentModel.DefaultValue(null)]
public byte[] lzma_data
{
get { return _lzma_data; }
set { _lzma_data = value; }
}
private byte[] _bzip2_data = null;
[global::ProtoBuf.ProtoMember(5, IsRequired = false, Name=@"bzip2_data", DataFormat = global::ProtoBuf.DataFormat.Default)]
[global::System.ComponentModel.DefaultValue(null)]
public byte[] bzip2_data
{
get { return _bzip2_data; }
set { _bzip2_data = value; }
}
private global::ProtoBuf.IExtension extensionObject;
global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
{ return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }
}
[global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"BlockHeader")]
public partial class BlockHeader : global::ProtoBuf.IExtensible
{
public BlockHeader() {}
private string _type;
[global::ProtoBuf.ProtoMember(1, IsRequired = true, Name=@"type", DataFormat = global::ProtoBuf.DataFormat.Default)]
public string type
{
get { return _type; }
set { _type = value; }
}
private byte[] _indexdata = null;
[global::ProtoBuf.ProtoMember(2, IsRequired = false, Name=@"indexdata", DataFormat = global::ProtoBuf.DataFormat.Default)]
[global::System.ComponentModel.DefaultValue(null)]
public byte[] indexdata
{
get { return _indexdata; }
set { _indexdata = value; }
}
private int _datasize;
[global::ProtoBuf.ProtoMember(3, IsRequired = true, Name=@"datasize", DataFormat = global::ProtoBuf.DataFormat.TwosComplement)]
public int datasize
{
get { return _datasize; }
set { _datasize = value; }
}
private global::ProtoBuf.IExtension extensionObject;
global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
{ return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }
}
}
Run Code Online (Sandbox Code Playgroud)
结论:
使用protobuf-net原子,或使用protobuf-csharp-port库.没有混搭.
| 归档时间: |
|
| 查看次数: |
12101 次 |
| 最近记录: |