logo
Tags down

shadow

C# Protobuf Deserialization


By : Jonathan Gonzales
Date : September 16 2020, 05:00 PM
With these it helps The problem here is the assumption that the deserializer calls the setter - either at all, or exactly when you expect; it is not required to. The serializer assumes reasonably typical implementations, in which case the following is perfectly reasonable:
code :
// when field 2
var val = obj.OffsetDictionary;
bool setValue = false;
if (val == null)
{
    val = new SortedDictionary<short, SortedDictionary<short, uint>>();
    setValue = true;
}
do {
    val.Add(/* parse this entry */);
} while (/* still field 2 */)
if (setValue) obj.OffsetDictionary = val;
[ProtoMember(2, OverwriteList = true)]
[ProtoContract]
public class Test
{
    [ProtoMember(1)]
    public DateTime BaseDate { get; set; }

    [ProtoMember(2)]
    public SortedDictionary<short, SortedDictionary<short, uint>> OffsetDictionary { get; }
        = new SortedDictionary<short, SortedDictionary<short, uint>>();

    private short ToInt16(DateTime value) => (short)(value - BaseDate).TotalDays;

    public void Add(DateTime key, SortedDictionary<short, uint> value)
        => OffsetDictionary.Add(ToInt16(key), value);
    public bool TryGetValue(DateTime key, out SortedDictionary<short, uint> value)
        => OffsetDictionary.TryGetValue(ToInt16(key), out value);
}
[ProtoContract]
public class Test
{
    [ProtoMember(1)]
    public DateTime BaseDate { get; set; }

    private DictionaryWrapper _offsetDictionary;

    [ProtoMember(2)]
    public IDictionary<short, SortedDictionary<short, uint>> OffsetDictionary
        => _offsetDictionary ?? (_offsetDictionary = new DictionaryWrapper(this));

    public SortedDictionary<DateTime, SortedDictionary<short, uint>> Dictionary { get; }
        = new SortedDictionary<DateTime, SortedDictionary<short, uint>>();

    class DictionaryWrapper : IDictionary<short, SortedDictionary<short, uint>>
    {
        public DictionaryWrapper(Test parent)
        {
            _parent = parent;
        }
        private readonly Test _parent;
        private DateTime ToDateTime(short value) => _parent.BaseDate.AddDays(value);
        private short ToInt16(DateTime value) => (short)(value - _parent.BaseDate).TotalDays;

        SortedDictionary<short, uint> IDictionary<short, SortedDictionary<short, uint>>.this[short key]
        {
            get => _parent.Dictionary[ToDateTime(key)];
            set => _parent.Dictionary[ToDateTime(key)] = value;
        }

        int ICollection<KeyValuePair<short, SortedDictionary<short, uint>>>.Count => _parent.Dictionary.Count;

        bool ICollection<KeyValuePair<short, SortedDictionary<short, uint>>>.IsReadOnly => false;

        void IDictionary<short, SortedDictionary<short, uint>>.Add(short key, SortedDictionary<short, uint> value)
            => _parent.Dictionary.Add(ToDateTime(key), value);

        void ICollection<KeyValuePair<short, SortedDictionary<short, uint>>>.Add(KeyValuePair<short, SortedDictionary<short, uint>> item)
            => _parent.Dictionary.Add(ToDateTime(item.Key), item.Value);

        void ICollection<KeyValuePair<short, SortedDictionary<short, uint>>>.Clear()
            => _parent.Dictionary.Clear();

        private ICollection<KeyValuePair<DateTime, SortedDictionary<short, uint>>> AsCollection => _parent.Dictionary;
        bool ICollection<KeyValuePair<short, SortedDictionary<short, uint>>>.Contains(KeyValuePair<short, SortedDictionary<short, uint>> item)
            => AsCollection.Contains(new KeyValuePair<DateTime, SortedDictionary<short, uint>>(ToDateTime(item.Key), item.Value));

        bool IDictionary<short, SortedDictionary<short, uint>>.ContainsKey(short key)
            => _parent.Dictionary.ContainsKey(ToDateTime(key));

        private IEnumerator<KeyValuePair<short, SortedDictionary<short, uint>>> GetEnumerator()
        {
            foreach (var item in _parent.Dictionary)
                yield return new KeyValuePair<short, SortedDictionary<short, uint>>(ToInt16(item.Key), item.Value);
        }
        IEnumerator<KeyValuePair<short, SortedDictionary<short, uint>>> IEnumerable<KeyValuePair<short, SortedDictionary<short, uint>>>.GetEnumerator()
            => GetEnumerator();

        IEnumerator IEnumerable.GetEnumerator()
            => GetEnumerator();

        bool IDictionary<short, SortedDictionary<short, uint>>.Remove(short key)
            => _parent.Dictionary.Remove(ToDateTime(key));

        bool ICollection<KeyValuePair<short, SortedDictionary<short, uint>>>.Remove(KeyValuePair<short, SortedDictionary<short, uint>> item)
            => AsCollection.Remove(new KeyValuePair<DateTime, SortedDictionary<short, uint>>(ToDateTime(item.Key), item.Value));

        bool IDictionary<short, SortedDictionary<short, uint>>.TryGetValue(short key, out SortedDictionary<short, uint> value)
            => _parent.Dictionary.TryGetValue(ToDateTime(key), out value);

        // these are kinda awkward to implement
        ICollection<short> IDictionary<short, SortedDictionary<short, uint>>.Keys
            => throw new NotSupportedException();

        ICollection<SortedDictionary<short, uint>> IDictionary<short, SortedDictionary<short, uint>>.Values
            => throw new NotSupportedException();

        void ICollection<KeyValuePair<short, SortedDictionary<short, uint>>>.CopyTo(KeyValuePair<short, SortedDictionary<short, uint>>[] array, int arrayIndex)
            => throw new NotSupportedException();
    }
}


Share : facebook icon twitter icon

How to do protobuf deserialization in PHP


By : user3540021
Date : March 29 2020, 07:55 AM
will help you I looked into code of library and found out that there is no support for uint64. Now the field which I am getting as uint64 from server is expiry time. Even if I consider 100 years as expiry, It would never get beyond int range. So I have linked uint64 to parsing of int data type and it is working fine for me.
Though it is not a good solution and completely dependent on personal usage.

How can I tell if Deserialization failed in protobuf.net?


By : Saint R
Date : March 29 2020, 07:55 AM
should help you out Update:
There were scenarios in v2 where it wouldn't spot this, but would instead terminate as though it had reached the end of the stream - in particular if the "field number", after applying shifts, was non-positive. However, this is not valid in a protobuf stream, and this will be fixed next build.
code :
var ms = new MemoryStream();
ms.Write(randomBytes, 0, randomBytes.Length);
var obj = Serializer.Deserialize<Foo>(ms);

Protobuf-net serialization/deserialization


By : Varun Dixit
Date : March 29 2020, 07:55 AM
I hope this helps . there's a few different questions here, so I'll answer what I can see: if I've missed anything just let me know.
Firstly, as noted, a MemoryStream is the most common way of getting to a byte[]. This is consistent with most serializers - for example, XmlSerializer, BinaryFormatter and DataContractSerializer also don't have an "as a byte[] overload", but will accept MemoryStream.
code :
[ProtoContract]
[ProtoInclude(1, typeof(Message<Foo>))]
.... More as needed
[ProtoInclude(8, typeof(Message<Bar>))]
public abstract class Message
{   }
[ProtoContract]
public class Message<T> : Message
{
    ...
}
[ProtoContract]
public class MyRoot {
    [ProtoMember(1, DynamicType=true)]
    public object Value { get; set; }
}

partial deserialization with protobuf


By : enzyme
Date : March 29 2020, 07:55 AM
it should still fix some issue First of all: You should not send protobuf serialized messages directly over the wire since TCP is stream based. You can never tell when you have received a complete message.
The WithLengthPrefix is used for just that. To prefix all messages with a binary length so that you can tell when a complete message has arrived.

Protobuf webservice deserialization in vba


By : Cedric Poal
Date : March 29 2020, 07:55 AM
around this issue I am not aware of a non-.NET VB/VBA implementation. You would either have to write one, or choose from the list of existing implementations. Or choose a different serialization technology, but frankly VBA isn't exactly awash with those.
Do you have .NET on the client? If you can use COM interop to call into .NET code then you'll have much more flexibility, including multiple protobuf implementations to choose from.
Related Posts Related Posts :
  • SQL Command Parameter cannot covert string to int
  • Azure Notification Hub example to register a device and receive a notification
  • Disabed element shown as enabled in Selenium C#
  • Html.listboxfor() how to capture event on change/add item - Jquery MVC (C#)
  • c# Accessing my array inside a class constructor
  • How to first 'Split a string to an Array' then 'Add something to that Array'? || C# Console App
  • IEnumerable of Tuple of string of string
  • Why don't I get a warning about possible dereference of a null in C# 8 with a class member of a struct?
  • Visual Studio 2019: how to disable specific CodeAnalysis messages in .editorconfig?
  • Pass ID to controller on prev or next
  • What are the difference using app.run and app.useendpoints in asp.net-core?
  • Why string Method(Object object) may not match Func<Object, string> delegate type?
  • Check inside loop if *txt file has been created
  • C# retry logic While loop
  • Convert object {object[]} to string[]
  • Difference between initialize a string to "" or to "".ToString() - Which one should I use?
  • How to make the console wait but then continue without pressing a Key?
  • extract text in rich text format from range
  • how to handle 1000 concurrent user requests per second in asp.net web api
  • In C# How to get Windows Credentials from credential manager
  • Override default value of null conditional operator
  • How to fix override ToString method
  • System.IO.FileNotFoundException when creating EF migrations on .net core
  • Login limit attempts in C#
  • Loop into ListView elements
  • Changing Blazor folder gives me "Cannot find the fallback endpoint"
  • creating class from JSON with different types of a node
  • How to define models for Database tables in C# without using Linq To DB?
  • Understanding Identityserver4 with Identity (cookies/tokens, server architecture)
  • How to Deserialize a JSON into a list using SIMPLE JSON?
  • another option instead using AsEnumerable() in linq EF, when using custom method in linq
  • Parameterized query that returns TEXT column(s) always returns zero for INT columns
  • Why format is not working while appending zeros?
  • SqlConnection string
  • Entity Framework Core Update Database - Code First Approach Without Migration
  • Unity Update Method
  • Custom Middleware is causing Blazor Server-side page to stop working
  • Declaring hex number: The name 'B9780' does not exist in the current context
  • Invalid cast from 'System.Int32' to Enum using reflections
  • Why does my webrequest line need updating? (error (426) Upgrade Required )
  • How to generate Key and Key IV aes in C#
  • How can I display view inside content control in wpf
  • How can I wait for form2 to finish?
  • Is Application.DoEvents() a form of Multitasking?
  • In C#, how can I see if the first character in an account is a specific value?
  • C# Determine if a char at index is between two characters in a string
  • C# - Winform Timer - Disposing and emptying the timer
  • if else condition in Regex c# and return constant value
  • Ternary operator for 3 conditions
  • Fineuploader with Azure, Empty image problem
  • How do I unit test the event raised by class under test?
  • How to set up .net core service that uses a generic repository
  • ASP.NET Core 3.0 Endpoint Routing doesn't work for default route
  • use gmail API token on localhost and hosting at the same time
  • Try-catch FormatException
  • why do sql procedure retuns 0 rows when called from applciation?
  • .Net core 3: The namespace internal does not exist in Microsoft.Extensions.Logging.Internal
  • how to interpolate a variable into string method
  • I want to instantiate an object at the location of another object which is destroyed
  • How to display full path of ALL currently running process in C#?
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk