How to save a <Point> list using protobuf.net without using ascii text?

I am trying to save point data in a list for a protobuf.net generated binary. Although Im not having problems with this on my own, I am also trying to save the data in a format that will not be easily viewed in a text editor. By default, when you save the List of Point structures in a text file, x and y for each point are visible as ascii text.

[global::ProtoBuf.ProtoMember(1, IsRequired = false, Name = @"BufPoints", DataFormat = global::ProtoBuf.DataFormat.Default)] private List<Point> BufPoints { get { return this.Points; } set { this.Points = value; } } 

I tried to create my own class to store the binary data x and y, but part of my routine includes a deep data clone, and the values ​​seem to be lost when preparing this clone.

 [global::ProtoBuf.ProtoMember(1, IsRequired = false, Name = @"EncodedPoints", DataFormat = global::ProtoBuf.DataFormat.Default)] private List<Utils.PointConverter> EncodedPoints { get { List<Utils.PointConverter> temp = new List<Utils.PointConverter>(); if (Points != null) { foreach (Point p in this.Points) { temp.Add(new Utils.PointConverter(p)); } } return temp; } set { if (value != null) { this.Points = new List<Point>(); foreach (Utils.PointConverter pc in value) { this.Points.Add(pc.GetPoint()); } } } } 

The PointConverter class is shown below:

  [global::System.Serializable, global::ProtoBuf.ProtoContract(Name = @"PointConverter")] class PointConverter { [global::ProtoBuf.ProtoMember(1, IsRequired = true, Name = @"X", DataFormat = global::ProtoBuf.DataFormat.Default)] public double X; [global::ProtoBuf.ProtoMember(2, IsRequired = true, Name = @"Y", DataFormat = global::ProtoBuf.DataFormat.Default)] public double Y; public PointConverter(System.Windows.Point point) { this.X = point.X; this.Y = point.Y; } public PointConverter() { } public System.Windows.Point GetPoint() { return new System.Windows.Point(X, Y); } } 

Im unsure why values ​​are lost during deep cloning. Is there a way to save the data in another way in non ascii format or a way to deal with my deep clone problem?

+4
source share
1 answer

Firstly, protobuf is not at any ASCII point (although the string values ​​are stored as UTF-8, which can often look like ASCII for Latin characters without accents) - I can not comment on what you see there without context, but serialization also is not encryption. Using the binary format can make reading or editing content difficult, but not using it as part of a security check.

As for the code: I think you are complicating things too much. In fact, for System.Drawing.Point and System.Windows.Point code is painfully close to automatically detecting the display as part of the "automatic tuple" processing. But not really! But we can just train it by adding a single line configuration setting in your application, telling it to serialize Point , saving .X as field 1 and .Y as field 2. This is true:

 // either or both; whatever you need model.Add(typeof(System.Windows.Point), false).Add("X", "Y"); model.Add(typeof(System.Drawing.Point), false).Add("X", "Y"); 

or if you are using the default model instance (i.e. Serializer.* methods):

 // either or both; whatever you need RuntimeTypeModel.Default.Add(typeof(System.Windows.Point), false).Add("X", "Y"); RuntimeTypeModel.Default.Add(typeof(System.Drawing.Point), false).Add("X", "Y"); 

what is it! that’s all you need. Members of Point or List<Point> or Point[] should now serialize and deserialize correctly.

+2
source

Source: https://habr.com/ru/post/1444635/


All Articles