Skip to content

Commit e107bbb

Browse files
committed
Add MessagePackSerializer.Get for usability.
1 parent dac84eb commit e107bbb

2 files changed

Lines changed: 313 additions & 12 deletions

File tree

src/MsgPack/Serialization/MessagePackSerializer.cs

Lines changed: 237 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static class MessagePackSerializer
6767
/// <returns>
6868
/// New <see cref="MessagePackSerializer{T}"/> instance to serialize/deserialize the object tree which the top is <typeparamref name="T"/>.
6969
/// </returns>
70-
[Obsolete( "Use SerializationContext.Default.GetSerializer<T>() instead." )]
70+
[Obsolete( "Use Get<T>() instead." )]
7171
public static MessagePackSerializer<T> Create<T>()
7272
{
7373
#if !UNITY
@@ -90,7 +90,7 @@ public static MessagePackSerializer<T> Create<T>()
9090
/// <exception cref="ArgumentNullException">
9191
/// <paramref name="context"/> is <c>null</c>.
9292
/// </exception>
93-
[Obsolete( "Use SerializationContext.GetSerializer<T>() instead." )]
93+
[Obsolete( "Use Get<T>(SerializationContext) instead." )]
9494
public static MessagePackSerializer<T> Create<T>( SerializationContext context )
9595
{
9696
if ( context == null )
@@ -101,6 +101,109 @@ public static MessagePackSerializer<T> Create<T>( SerializationContext context )
101101
return CreateInternal<T>( context );
102102
}
103103

104+
/// <summary>
105+
/// Gets existing or new <see cref="MessagePackSerializer{T}"/> instance with default context (<see cref="SerializationContext.Default"/>).
106+
/// </summary>
107+
/// <typeparam name="T">Target type.</typeparam>
108+
/// <returns>
109+
/// <see cref="MessagePackSerializer{T}"/>.
110+
/// If there is exiting one, returns it.
111+
/// Else the new instance will be created.
112+
/// </returns>
113+
/// <remarks>
114+
/// This method simply invokes <see cref="Get{T}(SerializationContext)"/> with <see cref="SerializationContext.Default"/> for the <c>context</c>.
115+
/// </remarks>
116+
public static MessagePackSerializer<T> Get<T>()
117+
{
118+
return Get<T>( SerializationContext.Default );
119+
}
120+
121+
/// <summary>
122+
/// Gets existing or new <see cref="MessagePackSerializer{T}"/> instance with default context (<see cref="SerializationContext.Default"/>).
123+
/// </summary>
124+
/// <typeparam name="T">Target type.</typeparam>
125+
/// <param name="providerParameter">A provider specific parameter. See remarks section for details.</param>
126+
/// <returns>
127+
/// <see cref="MessagePackSerializer{T}"/>.
128+
/// If there is exiting one, returns it.
129+
/// Else the new instance will be created.
130+
/// </returns>
131+
/// <remarks>
132+
/// This method simply invokes <see cref="Get{T}(SerializationContext,Object)"/> with <see cref="SerializationContext.Default"/> for the <c>context</c>.
133+
/// </remarks>
134+
public static MessagePackSerializer<T> Get<T>( object providerParameter )
135+
{
136+
return Get<T>( SerializationContext.Default, providerParameter );
137+
}
138+
139+
/// <summary>
140+
/// Gets existing or new <see cref="MessagePackSerializer{T}"/> instance with specified <see cref="SerializationContext"/>.
141+
/// </summary>
142+
/// <typeparam name="T">Target type.</typeparam>
143+
/// <param name="context">
144+
/// <see cref="SerializationContext"/> to store known/created serializers.
145+
/// </param>
146+
/// <returns>
147+
/// <see cref="MessagePackSerializer{T}"/>.
148+
/// If there is exiting one, returns it.
149+
/// Else the new instance will be created.
150+
/// </returns>
151+
/// <exception cref="ArgumentNullException">
152+
/// <paramref name="context"/> is <c>null</c>.
153+
/// </exception>
154+
/// <remarks>
155+
/// This method simply invokes <see cref="Get{T}(SerializationContext,Object)"/> with <c>null</c> for the <c>providerParameter</c>.
156+
/// </remarks>
157+
public static MessagePackSerializer<T> Get<T>( SerializationContext context )
158+
{
159+
return Get<T>( context, null );
160+
}
161+
162+
/// <summary>
163+
/// Gets existing or new <see cref="MessagePackSerializer{T}"/> instance with specified <see cref="SerializationContext"/>.
164+
/// </summary>
165+
/// <typeparam name="T">Target type.</typeparam>
166+
/// <param name="context">
167+
/// <see cref="SerializationContext"/> to store known/created serializers.
168+
/// </param>
169+
/// <param name="providerParameter">A provider specific parameter. See remarks section for details.</param>
170+
/// <returns>
171+
/// <see cref="MessagePackSerializer{T}"/>.
172+
/// If there is exiting one, returns it.
173+
/// Else the new instance will be created.
174+
/// </returns>
175+
/// <exception cref="ArgumentNullException">
176+
/// <paramref name="context"/> is <c>null</c>.
177+
/// </exception>
178+
/// <remarks>
179+
/// <para>
180+
/// This method simply invokes <see cref="SerializationContext.GetSerializer{T}(Object)"/>, so see the method description for details.
181+
/// </para>
182+
/// <para>
183+
/// Currently, only following provider parameters are supported.
184+
/// <list type="table">
185+
/// <listheader>
186+
/// <term>Target type</term>
187+
/// <description>Provider parameter</description>
188+
/// </listheader>
189+
/// <item>
190+
/// <term><see cref="EnumMessagePackSerializer{TEnum}"/> or its descendants.</term>
191+
/// <description><see cref="EnumSerializationMethod"/>. The returning instance corresponds to this value for serialization.</description>
192+
/// </item>
193+
/// </list>
194+
/// <note><c>null</c> is valid value for <paramref name="providerParameter"/> and it indeicates default behavior of parameter.</note>
195+
/// </para>
196+
/// </remarks>
197+
public static MessagePackSerializer<T> Get<T>( SerializationContext context, object providerParameter )
198+
{
199+
if ( context == null )
200+
{
201+
throw new ArgumentNullException( "context" );
202+
}
203+
204+
return context.GetSerializer<T>( providerParameter );
205+
}
206+
104207
internal static MessagePackSerializer<T> CreateInternal<T>( SerializationContext context )
105208
{
106209
#if XAMIOS || XAMDROID || UNITY
@@ -187,7 +290,7 @@ internal static MessagePackSerializer<T> CreateInternal<T>( SerializationContext
187290
/// <remarks>
188291
/// To avoid boxing and strongly typed API is prefered, use <see cref="Create{T}()"/> instead when possible.
189292
/// </remarks>
190-
[Obsolete( "Use SerializationContext.Default.GetSerializer(Type) instead." )]
293+
[Obsolete( "Use Get(Type) instead." )]
191294
public static IMessagePackSingleObjectSerializer Create( Type targetType )
192295
{
193296
return Create( targetType, SerializationContext.Default );
@@ -210,7 +313,7 @@ public static IMessagePackSingleObjectSerializer Create( Type targetType )
210313
/// <remarks>
211314
/// To avoid boxing and strongly typed API is prefered, use <see cref="Create{T}(SerializationContext)"/> instead when possible.
212315
/// </remarks>
213-
[Obsolete( "Use SerializationContext.GetSerializer(Type) instead." )]
316+
[Obsolete( "Use Get(Type,SerializationContext) instead." )]
214317
public static IMessagePackSingleObjectSerializer Create( Type targetType, SerializationContext context )
215318
{
216319
if ( targetType == null )
@@ -290,6 +393,136 @@ public static IMessagePackSingleObjectSerializer Create( Type targetType, Serial
290393
#endif // XAMIOS || XAMDROID || UNITY else
291394
}
292395

396+
/// <summary>
397+
/// Gets existing or new <see cref="IMessagePackSerializer"/> instance with default context (<see cref="SerializationContext.Default"/>).
398+
/// </summary>
399+
/// <param name="targetType">Target type.</param>
400+
/// <returns>
401+
/// <see cref="IMessagePackSingleObjectSerializer"/>.
402+
/// If there is exiting one, returns it.
403+
/// Else the new instance will be created.
404+
/// </returns>
405+
/// <exception cref="ArgumentNullException">
406+
/// <paramref name="targetType"/> is <c>null</c>.
407+
/// </exception>
408+
/// <remarks>
409+
/// <para>
410+
/// This method simply invokes <see cref="SerializationContext.GetSerializer(Type)"/>, so see the method description for details.
411+
/// </para>
412+
/// <para>
413+
/// Although <see cref="Get{T}()"/> is preferred,
414+
/// this method can be used from non-generic type or methods.
415+
/// </para>
416+
/// </remarks>
417+
public static IMessagePackSingleObjectSerializer Get(
418+
Type targetType )
419+
{
420+
return Get( targetType, SerializationContext.Default, null );
421+
}
422+
423+
/// <summary>
424+
/// Gets existing or new <see cref="IMessagePackSerializer"/> instance with default context (<see cref="SerializationContext.Default"/>).
425+
/// </summary>
426+
/// <param name="targetType">Target type.</param>
427+
/// <param name="providerParameter">A provider specific parameter. See remarks section for details.</param>
428+
/// <returns>
429+
/// <see cref="IMessagePackSingleObjectSerializer"/>.
430+
/// If there is exiting one, returns it.
431+
/// Else the new instance will be created.
432+
/// </returns>
433+
/// <exception cref="ArgumentNullException">
434+
/// <paramref name="targetType"/> is <c>null</c>.
435+
/// </exception>
436+
/// <remarks>
437+
/// <para>
438+
/// This method simply invokes <see cref="SerializationContext.GetSerializer(Type,Object)"/>, so see the method description for details.
439+
/// </para>
440+
/// <para>
441+
/// Although <see cref="Get{T}(Object)"/> is preferred,
442+
/// this method can be used from non-generic type or methods.
443+
/// </para>
444+
/// </remarks>
445+
public static IMessagePackSingleObjectSerializer Get(
446+
Type targetType,
447+
object providerParameter )
448+
{
449+
return Get( targetType, SerializationContext.Default, providerParameter );
450+
}
451+
452+
/// <summary>
453+
/// Gets existing or new <see cref="IMessagePackSerializer"/> instance with specified <see cref="SerializationContext"/>.
454+
/// </summary>
455+
/// <param name="targetType">Target type.</param>
456+
/// <param name="context">
457+
/// <see cref="SerializationContext"/> to store known/created serializers.
458+
/// </param>
459+
/// <returns>
460+
/// <see cref="IMessagePackSingleObjectSerializer"/>.
461+
/// If there is exiting one, returns it.
462+
/// Else the new instance will be created.
463+
/// </returns>
464+
/// <exception cref="ArgumentNullException">
465+
/// <paramref name="targetType"/> is <c>null</c>.
466+
/// Or, <paramref name="context"/> is <c>null</c>.
467+
/// </exception>
468+
/// <remarks>
469+
/// <para>
470+
/// This method simply invokes <see cref="SerializationContext.GetSerializer(Type)"/>, so see the method description for details.
471+
/// </para>
472+
/// <para>
473+
/// Although <see cref="Get{T}(SerializationContext)"/> is preferred,
474+
/// this method can be used from non-generic type or methods.
475+
/// </para>
476+
/// </remarks>
477+
public static IMessagePackSingleObjectSerializer Get(
478+
Type targetType,
479+
SerializationContext context )
480+
{
481+
return Get( targetType, context, null );
482+
}
483+
484+
/// <summary>
485+
/// Gets existing or new <see cref="IMessagePackSerializer"/> instance with specified <see cref="SerializationContext"/>.
486+
/// </summary>
487+
/// <param name="targetType">Target type.</param>
488+
/// <param name="context">
489+
/// <see cref="SerializationContext"/> to store known/created serializers.
490+
/// </param>
491+
/// <param name="providerParameter">A provider specific parameter. See remarks section for details.</param>
492+
/// <returns>
493+
/// <see cref="IMessagePackSingleObjectSerializer"/>.
494+
/// If there is exiting one, returns it.
495+
/// Else the new instance will be created.
496+
/// </returns>
497+
/// <exception cref="ArgumentNullException">
498+
/// <paramref name="targetType"/> is <c>null</c>.
499+
/// Or, <paramref name="context"/> is <c>null</c>.
500+
/// </exception>
501+
/// <remarks>
502+
/// <para>
503+
/// This method simply invokes <see cref="SerializationContext.GetSerializer(Type,Object)"/>, so see the method description for details.
504+
/// </para>
505+
/// <para>
506+
/// Although <see cref="Get{T}(SerializationContext,Object)"/> is preferred,
507+
/// this method can be used from non-generic type or methods.
508+
/// </para>
509+
/// </remarks>
510+
public static IMessagePackSingleObjectSerializer Get(
511+
Type targetType, SerializationContext context, object providerParameter )
512+
{
513+
if ( targetType == null )
514+
{
515+
throw new ArgumentNullException( "targetType" );
516+
}
517+
518+
if ( context == null )
519+
{
520+
throw new ArgumentNullException( "context" );
521+
}
522+
523+
return context.GetSerializer( targetType, providerParameter );
524+
}
525+
293526
#if XAMIOS || XAMDROID || UNITY
294527
private static readonly System.Reflection.MethodInfo CreateReflectionInternal_1 =
295528
typeof( MessagePackSerializer ).GetMethod(

0 commit comments

Comments
 (0)