{"id":109,"date":"2021-11-15T09:11:41","date_gmt":"2021-11-14T23:11:41","guid":{"rendered":"http:\/\/gerardwknight.duckdns.org\/?p=109"},"modified":"2022-06-24T09:57:01","modified_gmt":"2022-06-23T23:57:01","slug":"serialize-size-struct-in-protobuf-net","status":"publish","type":"post","link":"https:\/\/gerardwknight.duckdns.org\/index.php\/2021\/11\/15\/serialize-size-struct-in-protobuf-net\/","title":{"rendered":"Serialize Size Struct In ProtoBuf-net"},"content":{"rendered":"\n<p>The following code allows you to serialise the Size struct when using ProtoBuf-net.<\/p>\n\n\n\n<p>First define a surrogate.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;ProtoContract]\n    struct ProtoSize\n    {\n        &#91;ProtoMember(1)]\n        int Width;\n        &#91;ProtoMember(2)]\n        int Height;\n      \n        public static implicit operator Size(ProtoSize s)\n        { return new Size(new Point(s.Width, s.Height)); }\n        \n        public static implicit operator ProtoSize(Size s)\n        { return new ProtoSize { Width = s.Width, Height = s.Height };  }\n    }<\/code><\/pre>\n\n\n\n<p>Then add it to the RuntimeTypeModel.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>static RuntimeTypeModel Model;\n\nModel = RuntimeTypeModel.Create();\nModel.AllowParseableTypes = true;\nModel.Add(typeof(Size), false).SetSurrogate(typeof(ProtoSize));<\/code><\/pre>\n\n\n\n<p>Then use it.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> Model.Serialize(someFile, someObjectWithSizeProperty);<\/code><\/pre>\n\n\n\n<p>Remember to decorate the size property with a [ProtoMember] attribute.<\/p>\n\n\n\n<p>Here is a list of other useful surrogates for Dot Net, which I gleaned from the <a href=\"https:\/\/stackoverflow.com\/questions\/18405160\/how-to-serialize-a-closed-immutable-type-with-protobuf-net\">Internet<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n    &#91;ProtoContract]\n    struct ProtoColor\n    {\n        &#91;ProtoMember(1, DataFormat = DataFormat.FixedSize)]\n        public uint argb;\n        public static implicit operator Color(ProtoColor c)\n        { return Color.FromArgb((int)c.argb); }\n        public static implicit operator ProtoColor(Color c)\n        { return new ProtoColor { argb = (uint)c.ToArgb() }; }\n    }\n    &#91;ProtoContract()]\n    class ProtoFont\n    {\n        &#91;ProtoMember(1)]\n        string FontFamily;\n        &#91;ProtoMember(2)]\n        float SizeInPoints;\n        &#91;ProtoMember(3)]\n        FontStyle Style;\n\n        public static implicit operator Font(ProtoFont f)\n        {\n            return new Font(f.FontFamily, f.SizeInPoints, f.Style);\n        }\n        public static implicit operator ProtoFont(Font f)\n        {\n            return f == null ? null : new ProtoFont\n            {\n                FontFamily = f.FontFamily.Name,\n                SizeInPoints = f.SizeInPoints,\n                Style = f.Style\n            };\n        }\n    }\n    &#91;ProtoContract()]\n    class ProtoStringFormat\n    {\n        &#91;ProtoMember(1, DataFormat = DataFormat.Group)]\n        StringAlignment Alignment;\n        &#91;ProtoMember(2)]\n        StringAlignment LineAlignment;\n        &#91;ProtoMember(3)]\n        StringFormatFlags Flags;\n        public static implicit operator StringFormat(ProtoStringFormat f)\n        {\n            return new StringFormat(f.Flags)\n            {\n                Alignment = f.Alignment,\n                LineAlignment = f.LineAlignment\n            };\n        }\n        public static implicit operator ProtoStringFormat(StringFormat f)\n        {\n            return f == null ? null : new ProtoStringFormat()\n            {\n                Flags = f.FormatFlags,\n                Alignment = f.Alignment,\n                LineAlignment = f.LineAlignment\n            };\n        }\n    }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The following code allows you to serialise the Size struct when using ProtoBuf-net. First define a surrogate. Then add it to the RuntimeTypeModel. Then use it. Remember to decorate the size property with a [ProtoMember] attribute. Here is a list of other useful surrogates for Dot Net, which I gleaned from the Internet.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,1],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v15.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Serialize Size Struct In ProtoBuf-net - GerardWKnight<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gerardwknight.duckdns.org\/index.php\/2021\/11\/15\/serialize-size-struct-in-protobuf-net\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Serialize Size Struct In ProtoBuf-net - GerardWKnight\" \/>\n<meta property=\"og:description\" content=\"The following code allows you to serialise the Size struct when using ProtoBuf-net. First define a surrogate. Then add it to the RuntimeTypeModel. Then use it. Remember to decorate the size property with a [ProtoMember] attribute. Here is a list of other useful surrogates for Dot Net, which I gleaned from the Internet.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gerardwknight.duckdns.org\/index.php\/2021\/11\/15\/serialize-size-struct-in-protobuf-net\/\" \/>\n<meta property=\"og:site_name\" content=\"GerardWKnight\" \/>\n<meta property=\"article:published_time\" content=\"2021-11-14T23:11:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-06-23T23:57:01+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\">\n\t<meta name=\"twitter:data1\" content=\"2 minutes\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/gerardwknight.duckdns.org\/#website\",\"url\":\"https:\/\/gerardwknight.duckdns.org\/\",\"name\":\"GerardWKnight\",\"description\":\"Web site of Melbourne based developer Gerard Knight\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/gerardwknight.duckdns.org\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/gerardwknight.duckdns.org\/index.php\/2021\/11\/15\/serialize-size-struct-in-protobuf-net\/#webpage\",\"url\":\"https:\/\/gerardwknight.duckdns.org\/index.php\/2021\/11\/15\/serialize-size-struct-in-protobuf-net\/\",\"name\":\"Serialize Size Struct In ProtoBuf-net - GerardWKnight\",\"isPartOf\":{\"@id\":\"https:\/\/gerardwknight.duckdns.org\/#website\"},\"datePublished\":\"2021-11-14T23:11:41+00:00\",\"dateModified\":\"2022-06-23T23:57:01+00:00\",\"author\":{\"@id\":\"https:\/\/gerardwknight.duckdns.org\/#\/schema\/person\/9253da78f8cec50174c8c71e03a0fa45\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/gerardwknight.duckdns.org\/index.php\/2021\/11\/15\/serialize-size-struct-in-protobuf-net\/\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/gerardwknight.duckdns.org\/#\/schema\/person\/9253da78f8cec50174c8c71e03a0fa45\",\"name\":\"gerardwknight\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/gerardwknight.duckdns.org\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6e6f44202403f41895c15856cb917d94?s=96&d=mm&r=g\",\"caption\":\"gerardwknight\"},\"sameAs\":[\"http:\/\/192.168.0.16\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/gerardwknight.duckdns.org\/index.php\/wp-json\/wp\/v2\/posts\/109"}],"collection":[{"href":"https:\/\/gerardwknight.duckdns.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gerardwknight.duckdns.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gerardwknight.duckdns.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gerardwknight.duckdns.org\/index.php\/wp-json\/wp\/v2\/comments?post=109"}],"version-history":[{"count":3,"href":"https:\/\/gerardwknight.duckdns.org\/index.php\/wp-json\/wp\/v2\/posts\/109\/revisions"}],"predecessor-version":[{"id":113,"href":"https:\/\/gerardwknight.duckdns.org\/index.php\/wp-json\/wp\/v2\/posts\/109\/revisions\/113"}],"wp:attachment":[{"href":"https:\/\/gerardwknight.duckdns.org\/index.php\/wp-json\/wp\/v2\/media?parent=109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gerardwknight.duckdns.org\/index.php\/wp-json\/wp\/v2\/categories?post=109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gerardwknight.duckdns.org\/index.php\/wp-json\/wp\/v2\/tags?post=109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}