Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6095307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:47:51+00:00 2026-05-23T12:47:51+00:00

As part of my protobuf protocol I require the ability to send data of

  • 0

As part of my protobuf protocol I require the ability to send data of a dynamic type, a little bit like VARIANT. Roughly I require the data to be an integer, string, boolean or “other” where “other” (e.g. DateTime) is serialized as a string. I need to be able to use these as a single field and in lists in a number of different locations in the protocol.

How can this best be implemented while keeping message size minimal and performance optimal?

I’m using protobuf-net with C#.

EDIT:
I’ve posted a proposed answer below which uses what I think is the minimum of memory required.

EDIT2:
Created a github.com project at http://github.com/pvginkel/ProtoVariant with a complete implementation.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:47:51+00:00Added an answer on May 23, 2026 at 12:47 pm

    Jon’s multiple optionals covers the simplest setup, especially if you need cross-platform support. On the .NET side (to ensure you don’t serialize unnecessary values), simply return null from any property that isn’t a match, for example:

    public object Value { get;set;}
    [ProtoMember(1)]
    public int? ValueInt32 {
        get { return (Value is int) ? (int)Value : (int?)null; }
        set { Value = value; }
    }
    [ProtoMember(2)]
    public string ValueString {
        get { return (Value is string) ? (string)Value : null; }
        set { Value = value; }
    }
    // etc
    

    You can also do the same using the bool ShouldSerialize*() pattern if you don’t like the nulls.

    Wrap that up in a class and you should be fine to use that at either the field level or list level. You mention optimal performance; the only additional thing I can suggest there is to perhaps consider treating as a “group” rather than “submessage”, as this is easier to encode (and just as easy to decode, as long as you expect the data). To do that, use the Grouped data-format, via [ProtoMember], i.e.

    [ProtoMember(12, DataFormat = DataFormat.Group)]
    public MyVariant Foo {get;set;}
    

    However, the difference here can be minimal – but it avoids some back-tracking in the output stream to fix the lengths. Either way, in terms of overheads a “submessage” will take at least 2 bytes; “at least one” for the field-header (perhaps taking more if the 12 is actually 1234567) – and “at least one” for the length, which gets bigger for longer messages. A group takes 2 x the field-header, so if you use low field-numbers this will be 2 bytes regardless of the length of the encapsulated data (it could be 5MB of binary).

    A separate trick, useful for more complex scenarios but not as interoperable, is generic inheritance, i.e. an abstract base class that has ConcreteType<int>, ConcreteType<string> etc listed as subtypes – this, however, takes an extra 2 bytes (typically), so is not as frugal.

    Taking another step further away from the core spec, if you genuinely can’t tell what types you need to support, and don’t need interoperability – there is some support for including (optimized) type information in the data; see the DynamicType option on ProtoMember – this takes more space than the other two options.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Part of My Item Model: Ext.define('DnD.model.Item', { extend: 'Ext.data.Model', idProperty:'item_number', associations: [{ type: 'belongsTo',
Part of my spreadsheet's script runs the following piece of code: temp.getRange(1, 1).setValue(=QUERY(data!A1:H125, \Select
There's a neat option in protobuf-net ver 2 called [ProtoMember(2,AsReference=true)]. For the most part,
Part of my .htaccess file looks like this- AuthUserFile /path/to/.htpasswd AuthName Authorization Required AuthType
Part of the data layer for my application is a converter cache similar to
Part of a web application I am developing requires the ability to capture still
Part of the XML looks like this: <ipcEntry kind=1 symbol=A01B0013080000 ipcLevel=A entryType=K lang=EN nocore=yes>
In protobuf-net can i have a byte field? Is a byte array field part
Part of my project directory structure looks like: \projects\project\main.R \projects\project\src where \src contains a
part of my code for downloading financial data: library(quantmod) tickers <- c(XOM, DIS) stock1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.