本文整理汇总了C#中Unpacker类的典型用法代码示例。如果您正苦于以下问题:C# Unpacker类的具体用法?C# Unpacker怎么用?C# Unpacker使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Unpacker类属于命名空间,在下文中一共展示了Unpacker类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: UnpackDictionary
private static Dictionary<int, string> UnpackDictionary(Unpacker unpacker)
{
int length = unpacker.UnpackMap();
var dict = new Dictionary<int, string>(length);
for (int i = 0; i < length; i++)
{
dict.Add(unpacker.UnpackInt(), unpacker.UnpackString());
}
return dict;
}
示例2: UnpackFromCore
protected internal override object UnpackFromCore( Unpacker unpacker )
{
if ( !unpacker.IsArrayHeader )
{
throw SerializationExceptions.NewIsNotArrayHeader();
}
var instance = Array.CreateInstance( this.Traits.ElementType, UnpackHelpers.GetItemsCount( unpacker ) );
this.UnpackToCore( unpacker, instance );
return instance;
}
示例3: UnpackNullableBooleanValue
public static Boolean? UnpackNullableBooleanValue( Unpacker unpacker, Type objectType, String memberName )
{
try
{
Boolean? result;
if ( !unpacker.ReadNullableBoolean( out result ) )
{
throw SerializationExceptions.NewFailedToDeserializeMember( objectType, memberName, null );
}
return result;
}
catch ( MessageTypeException ex )
{
throw SerializationExceptions.NewFailedToDeserializeMember( objectType, memberName, ex );
}
}
示例4: UnpackBooleanValue
public static Boolean UnpackBooleanValue( Unpacker unpacker, Type objectType, String memberName )
{
if ( unpacker == null )
{
SerializationExceptions.ThrowArgumentNullException( "unpacker" );
}
if ( objectType == null )
{
SerializationExceptions.ThrowArgumentNullException( "objectType" );
}
if ( memberName == null )
{
SerializationExceptions.ThrowArgumentNullException( "memberName" );
}
#if ASSERT
Contract.Assert( unpacker != null );
Contract.Assert( objectType != null );
Contract.Assert( memberName != null );
#endif // ASSERT
// ReSharper disable once RedundantAssignment
var ctx = default( UnpackerTraceContext );
InitializeUnpackerTrace( unpacker, ref ctx );
try
{
Boolean result;
if ( !unpacker.ReadBoolean( out result ) )
{
SerializationExceptions.ThrowFailedToDeserializeMember( objectType, memberName, null );
}
Trace( ctx, "ReadDirect", unpacker, memberName );
return result;
}
catch ( MessageTypeException ex )
{
SerializationExceptions.ThrowFailedToDeserializeMember( objectType, memberName, ex );
return default( Boolean ); // never reaches.
}
}
示例5: TestReadFromFile
public void TestReadFromFile()
{
using (var stream = new FileStream(@"TestFiles\test.mpac", FileMode.Open))
{
var unpacker = new Unpacker(stream);
Assert.AreEqual(
new List<long>
{
0, 1, -1, -123, 12345678, -31, -128, -65535, -(1L << 16), -(1L << 24), 31, 255, 256, (1L << 16),
(1L << 24), (1L << 32)
}, unpacker.UnpackListOfObjects());
Assert.AreEqual(
new List<double>
{
0.0, -0.0, 1.0, -1.0, 3.40282347E+38, -3.40282347E+38, 1.7976931348623157E+308, -1.7976931348623157E+308,
double.NaN, double.PositiveInfinity, double.NegativeInfinity
}, unpacker.UnpackListOfObjects());
Assert.AreEqual(0xffffffffffffffff, unpacker.UnpackULong());
Assert.AreEqual(null, unpacker.UnpackNull());
Assert.AreEqual(true, unpacker.UnpackBool());
Assert.AreEqual(false, unpacker.UnpackBool());
Assert.AreEqual(Enumerable.Range(1, 65535).ToList(), unpacker.UnpackListOfObjects());
Assert.AreEqual(Enumerable.Range(1, 66000).ToList(), unpacker.UnpackListOfObjects());
TestPackUnpack.AssertDictionariesEqual(
new Dictionary<int, bool> {{1, true}, {10, false}, {-127, true}}, unpacker.UnpackDictionary());
TestPackUnpack.AssertDictionariesEqual(
Enumerable.Range(1, 65536).ToDictionary(v => v, v => v * 2), unpacker.UnpackDictionary());
TestPackUnpack.AssertDictionariesEqual(
new Dictionary<int, string> { { 1, "qwerty" }, { 2, "zxc" }, { 5, "" }, {6, null} }, UnpackDictionary(unpacker));
}
}
示例6: UnpackFrom
/// <summary>
/// Deserialize object with specified <see cref="Unpacker" />.
/// </summary>
/// <param name="unpacker"><see cref="Unpacker" /> which unpacks values of resulting object tree.</param>
/// <returns>
/// The deserialized object.
/// </returns>
/// <seealso cref="Capabilities" />
public object UnpackFrom( Unpacker unpacker )
{
return this.InternalUnpackFrom( unpacker );
}
示例7: InternalUnpackToAsync
internal abstract Task InternalUnpackToAsync( Unpacker unpacker, object collection, CancellationToken cancellationToken );
示例8: UnpackToCore
/// <summary>
/// Deserializes collection items with specified <see cref="Unpacker"/> and stores them to <paramref name="collection"/>.
/// </summary>
/// <param name="unpacker"><see cref="Unpacker"/> which unpacks values of resulting object tree. This value will not be <c>null</c>.</param>
/// <param name="collection">Collection that the items to be stored. This value will not be <c>null</c>.</param>
/// <exception cref="SerializationException">
/// Failed to deserialize object due to invalid unpacker state, stream content, or so.
/// </exception>
/// <exception cref="NotSupportedException">
/// The type of <paramref name="collection" /> is not a collection.
/// </exception>
protected internal virtual void UnpackToCore( Unpacker unpacker, object collection )
{
throw SerializationExceptions.NewUnpackToIsNotSupported( this._targetType, null );
}
示例9: AddUnpacker
static void AddUnpacker(Unpacker unpacker, params System.Type[] sql_types)
{
foreach (var sql_type in sql_types) {
unpackers[sql_type] = unpacker;
}
}
示例10: UnpackTo
public void UnpackTo(Unpacker unpacker, object collection)
{
if (unpacker == null)
{
throw new ArgumentNullException("unpacker");
}
if (collection == null)
{
throw new ArgumentNullException("collection");
}
throw new NotSupportedException(String.Format(CultureInfo.CurrentCulture, "This operation is not supported by '{0}'.", this.GetType()));
}
示例11: UnpackMemberInMap
private void UnpackMemberInMap(Unpacker unpacker, ref object instance, int index)
{
if (this._memberSetters[index] == null)
{
// Use null as marker because index mapping cannot be constructed in the constructor.
this._memberSerializers[index].UnpackTo(unpacker, this._memberGetters[index](instance));
}
else
{
this._memberSetters[index](ref instance, this._memberSerializers[index].UnpackFrom(unpacker));
}
}
示例12: UnpackFromMap
private void UnpackFromMap(Unpacker unpacker, ref object instance)
{
while (unpacker.Read())
{
var memberName = GetMemberName(unpacker);
int index;
if (!this._indexMap.TryGetValue(memberName, out index))
{
// Drains unused value.
if (!unpacker.Read())
{
throw SerializationExceptions.NewUnexpectedEndOfStream();
}
// TODO: unknown member handling.
continue;
}
// Fetches value
if (!unpacker.Read())
{
throw SerializationExceptions.NewUnexpectedEndOfStream();
}
if (unpacker.LastReadData.IsNil)
{
switch (this._nilImplications[index])
{
case NilImplication.Null:
{
this._memberSetters[index](ref instance, null);
continue;
}
case NilImplication.MemberDefault:
{
continue;
}
case NilImplication.Prohibit:
{
throw SerializationExceptions.NewNullIsProhibited(this._memberNames[index]);
}
}
}
if (unpacker.IsArrayHeader || unpacker.IsMapHeader)
{
using (var subtreeUnpacker = unpacker.ReadSubtree())
{
this.UnpackMemberInMap(subtreeUnpacker, ref instance, index);
}
}
else
{
this.UnpackMemberInMap(unpacker, ref instance, index);
}
}
}
示例13: UnpackFromCore
protected internal override object UnpackFromCore(Unpacker unpacker)
{
// Assume subtree unpacker
var instance = this._createInstance();
if (this._unpackFromMessage != null)
{
this._unpackFromMessage(ref instance, unpacker);
}
else
{
if (unpacker.IsArrayHeader)
{
this.UnpackFromArray(unpacker, ref instance);
}
else
{
this.UnpackFromMap(unpacker, ref instance);
}
}
return instance;
}
示例14: UnpackBinaryValue
public static byte[] UnpackBinaryValue( Unpacker unpacker, Type objectType, String memberName )
{
try
{
byte[] result;
if ( !unpacker.ReadBinary( out result ) )
{
throw SerializationExceptions.NewFailedToDeserializeMember( objectType, memberName, null );
}
return result;
}
catch ( MessageTypeException ex )
{
throw SerializationExceptions.NewFailedToDeserializeMember( objectType, memberName, ex );
}
}
示例15: UnpackInt32ValueAsync
public static async Task< Int32> UnpackInt32ValueAsync( Unpacker unpacker, Type objectType, String memberName, CancellationToken cancellationToken )
{
if ( unpacker == null )
{
SerializationExceptions.ThrowArgumentNullException( "unpacker" );
}
if ( objectType == null )
{
SerializationExceptions.ThrowArgumentNullException( "objectType" );
}
if ( memberName == null )
{
SerializationExceptions.ThrowArgumentNullException( "memberName" );
}
#if ASSERT
Contract.Assert( unpacker != null );
Contract.Assert( objectType != null );
Contract.Assert( memberName != null );
#endif // ASSERT
// ReSharper disable once RedundantAssignment
var ctx = default( UnpackerTraceContext );
InitializeUnpackerTrace( unpacker, ref ctx );
try
{
AsyncReadResult<Int32> result =
await unpacker.ReadInt32Async( cancellationToken ).ConfigureAwait( false );
if ( !result.Success )
{
SerializationExceptions.ThrowFailedToDeserializeMember( objectType, memberName, null );
}
Trace( ctx, "ReadDirect", unpacker, memberName );
return result.Value;
}
catch ( MessageTypeException ex )
{
SerializationExceptions.ThrowFailedToDeserializeMember( objectType, memberName, ex );
return default( Int32 ); // never reaches.
}
}