/// <summary> /// Initializes a new instance with the provided formatter resolver. /// </summary> /// <param name="options"></param> public MessagePackMediaTypeFormatter(MessagePackSerializerOptions?options) { SupportedMediaTypes.Add(new MediaTypeHeaderValue(MediaType)); _options = options ?? MessagePackSerializerOptions.Standard .WithCompression(MessagePackCompression.Lz4BlockArray) .WithResolver(StandardResolverAllowPrivate.Instance); }
public void Serialize(ref MessagePackWriter writer, TypeRequiringCustomFormatter value, MessagePackSerializerOptions options) { this.LastObservedOptions = options; writer.WriteArrayHeader(2); writer.Write(value.Prop1); writer.Write(value.Prop2); }
public CacheFlowMessagePackSerializer(MessagePackSerializerOptions?options = null, params IFormatterResolver[]?resolvers) { resolvers ??= new IFormatterResolver[] { StandardResolver.Instance }; var resolver = CompositeResolver.Create(resolvers); options ??= MessagePackSerializerOptions.Standard; _options = options.WithResolver(resolver); }
public static MemoryStream ToStream <TValue>(TValue?value, MessagePackSerializerOptions?options = null, CancellationToken cancellationToken = default) { var ms = new MemoryStream(); Pack(value, ms, options, cancellationToken); return(ms); }
public static void Pack <TValue>(TValue?value, Stream?stream, MessagePackSerializerOptions?options = null, CancellationToken cancellationToken = default) { if (stream is null) { return; } MessagePackSerializer.Serialize(stream, value, options, cancellationToken); stream.TrySeek(0, SeekOrigin.Begin); }
public TypeRequiringCustomFormatter Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options) { this.LastObservedOptions = options; Assert.Equal(2, reader.ReadArrayHeader()); return(new TypeRequiringCustomFormatter { Prop1 = reader.ReadInt32(), Prop2 = reader.ReadInt32(), }); }
public MessagePackDocumentSerializer(MessagePackSerializerOptions?options = default) { this.options = options; #if NET6_0_OR_GREATER this.options = (options ?? MessagePackSerializerOptions.Standard) .WithResolver(CompositeResolver.Create( StandardResolver.Instance, DateOnlyFormatterResolver.Instance)); #endif }
public static async Task PackAsync(Type type, object?value, Stream?stream, MessagePackSerializerOptions?options = null, CancellationToken cancellationToken = default) { if (stream is null) { return; } await MessagePackSerializer.SerializeAsync(type, stream, value, options, cancellationToken); stream.TrySeek(0, SeekOrigin.Begin); }
public static INetCacheBuilder UseMessagePack(this INetCacheBuilder builder, MessagePackSerializerOptions?options = null) #endif { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } #if NETFRAMEWORK builder.Services.Replace(new ServiceDescriptor(typeof(IValueSerializer), new MessagePackSerializer())); #else builder.Services.Replace(new ServiceDescriptor(typeof(IValueSerializer), new MessagePackSerializer(options))); #endif return(builder); }
public MsgPackContentNestedDataSerializer(IPropertyCacheCompression propertyOptions) { _propertyOptions = propertyOptions ?? throw new ArgumentNullException(nameof(propertyOptions)); MessagePackSerializerOptions?defaultOptions = ContractlessStandardResolver.Options; IFormatterResolver? resolver = CompositeResolver.Create( // TODO: We want to be able to intern the strings for aliases when deserializing like we do for Newtonsoft but I'm unsure exactly how // to do that but it would seem to be with a custom message pack resolver but I haven't quite figured out based on the docs how // to do that since that is part of the int key -> string mapping operation, might have to see the source code to figure that one out. // There are docs here on how to build one of these: https://github.com/neuecc/MessagePack-CSharp/blob/master/README.md#low-level-api-imessagepackformattert // and there are a couple examples if you search on google for them but this will need to be a separate project. // NOTE: resolver custom types first // new ContentNestedDataResolver(), // finally use standard resolver defaultOptions.Resolver); _options = defaultOptions .WithResolver(resolver) .WithCompression(MessagePackCompression.Lz4BlockArray) .WithSecurity(MessagePackSecurity.UntrustedData); }
public static IServiceCollection AddCacheFlowMessagePackSerialization(this IServiceCollection services, MessagePackSerializerOptions?options = null, params IFormatterResolver[]?resolvers) => services.AddSingleton <ISerializer, CacheFlowMessagePackSerializer>(serviceProvider => new CacheFlowMessagePackSerializer(options, resolvers));
public static MemoryStream ToStream(this object?value, Type type, MessagePackSerializerOptions?options = null, CancellationToken cancellationToken = default) => MessagePackHelper.ToStream(type, value, options, cancellationToken);
public static MemoryStream ToStream <TValue>(this TValue?value, MessagePackSerializerOptions?options = null, CancellationToken cancellationToken = default) => MessagePackHelper.ToStream(value, options, cancellationToken);
public static ValueTask <object?> FromStreamAsync(this Stream?stream, Type type, MessagePackSerializerOptions?options = null, CancellationToken cancellationToken = default) => MessagePackHelper.FromStreamAsync(type, stream, options, cancellationToken);
private static async Task <T?> ReadFromMessagePackAsyncCore <T>(this HttpContent content, MessagePackSerializerOptions?options, CancellationToken cancellationToken = default) { var bytes = await content.ReadAsByteArrayAsync(cancellationToken); return(MessagePackSerializer.Deserialize <T>(bytes, options)); }
private static async Task <T?> GetFromMessagePackAsyncCore <T>(Task <HttpResponseMessage?> taskResponse, MessagePackSerializerOptions?options, CancellationToken cancellationToken) { using (HttpResponseMessage response = await taskResponse.ConfigureAwait(false)) { response.EnsureSuccessStatusCode(); // Nullable forgiving reason: // GetAsync will usually return Content as not-null. // If Content happens to be null, the extension will throw. return(await response.Content !.ReadFromMessagePackAsync <T>(options, cancellationToken).ConfigureAwait(false)); } }
public static object?FromBytes(this ReadOnlyMemory <byte> bytes, Type type, MessagePackSerializerOptions?options = null, CancellationToken cancellationToken = default) => MessagePackHelper.FromBytes(type, bytes, options, cancellationToken);
public static TValue?FromStream <TValue>(Stream?stream, MessagePackSerializerOptions?options = null, CancellationToken cancellationToken = default) { if (stream is null || stream.CanSeek && stream.Length is 0) { return(default);
public static object?FromBytes(Type type, ReadOnlyMemory <byte> bytes, MessagePackSerializerOptions?options = null, CancellationToken cancellationToken = default) => bytes.Length is 0 ? default : MessagePackSerializer.Deserialize(type, bytes, options, cancellationToken);
public static void PackBy(this Stream?stream, Type type, object?value, MessagePackSerializerOptions?options = null, CancellationToken cancellationToken = default) => MessagePackHelper.Pack(type, value, stream, options, cancellationToken);
public static void PackBy <TValue>(this Stream?stream, TValue?value, MessagePackSerializerOptions?options = null, CancellationToken cancellationToken = default) => MessagePackHelper.Pack(value, stream, options, cancellationToken);
public static Task PackToAsync <TValue>(this TValue?value, Stream?stream, MessagePackSerializerOptions?options = null, CancellationToken cancellationToken = default) => MessagePackHelper.PackAsync(value, stream, options, cancellationToken);
public static Task PackToAsync(this object?value, Type type, Stream?stream, MessagePackSerializerOptions?options = null, CancellationToken cancellationToken = default) => MessagePackHelper.PackAsync(type, value, stream, options, cancellationToken);
public static TValue?FromBytes <TValue>(this ReadOnlyMemory <byte> bytes, MessagePackSerializerOptions?options = null, CancellationToken cancellationToken = default) => MessagePackHelper.FromBytes <TValue>(bytes, options, cancellationToken);
public static Task <T?> ReadFromMessagePackAsync <T>(this HttpContent content, MessagePackSerializerOptions?options, CancellationToken cancellationToken = default) { if (content == null) { throw new ArgumentNullException(nameof(content)); } return(content.ReadFromMessagePackAsyncCore <T>(options, cancellationToken)); }
public static byte[] ToBytes(Type type, object?value, MessagePackSerializerOptions?options = null, CancellationToken cancellationToken = default) => MessagePackSerializer.Serialize(type, value, options, cancellationToken);
public static ValueTask <TValue?> FromStreamAsync <TValue>(this Stream?stream, MessagePackSerializerOptions?options = null, CancellationToken cancellationToken = default) => MessagePackHelper.FromStreamAsync <TValue>(stream, options, cancellationToken);
public static TValue?FromBytes <TValue>(ReadOnlyMemory <byte> bytes, MessagePackSerializerOptions?options = null, CancellationToken cancellationToken = default) => bytes.Length is 0 ? default : MessagePackSerializer.Deserialize <TValue>(bytes, options, cancellationToken);
public static byte[] ToBytes <TValue>(TValue?value, MessagePackSerializerOptions?options = null, CancellationToken cancellationToken = default) => MessagePackSerializer.Serialize(value, options, cancellationToken);
public Serializer(MessagePackSerializerOptions?options = null) =>