在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:Tornhoof/SpanJson开源软件地址:https://github.com/Tornhoof/SpanJson开源编程语言:C# 99.6%开源软件介绍:SpanJsonSee https://github.com/Tornhoof/SpanJson/wiki/Performance for Benchmarks What is supported
How to use itSynchronous API:
var result = JsonSerializer.Generic.Utf16.Serialize(input);
var result = JsonSerializer.NonGeneric.Utf16.Serialize(input);
var result = JsonSerializer.Generic.Utf16.Deserialize<Input>(input);
var result = JsonSerializer.NonGeneric.Utf16.Deserialize(input, typeof(Input));
var result = JsonSerializer.Generic.Utf8.Serialize(input);
var result = JsonSerializer.NonGeneric.Utf8.Serialize(input);
var result = JsonSerializer.Generic.Utf8.Deserialize<Input>(input);
var result = JsonSerializer.NonGeneric.Utf8.Deserialize(input, typeof(Input));
// The following methods return an ArraySegment from the ArrayPool, you NEED to return it yourself after working with it.
var result = JsonSerializer.Generic.Utf16.SerializeToArrayPool(input);
var result = JsonSerializer.NonGeneric.Utf16.SerializeToArrayPool(input);
var result = JsonSerializer.Generic.Utf8.SerializeToArrayPool(input);
var result = JsonSerializer.NonGeneric.Utf8.SerializeToArrayPool(input);
Asynchronous API:
ValueTask result = JsonSerializer.Generic.Utf16.SerializeAsync(input, textWriter, cancellationToken);
ValueTask result = JsonSerializer.NonGeneric.Utf16.SerializeAsync(input, textWriter, cancellationToken);
ValueTask<Input> result = JsonSerializer.Generic.Utf16.DeserializeAsync<Input>(textReader,cancellationToken);
ValueTask<object> result = JsonSerializer.NonGeneric.Utf16.DeserializeAsync(textReader,typeof(Input),cancellationToken);
ValueTask result = JsonSerializer.Generic.Utf8.SerializeAsync(input, stream, cancellationToken);
ValueTask result = JsonSerializer.NonGeneric.Utf8.SerializeAsync(input, stream, cancellationToken);
ValueTask<Input> result = JsonSerializer.Generic.Utf8.DeserializeAsync<Input>(input, stream, cancellationToken);
ValueTask<object> result = JsonSerializer.NonGeneric.Utf8.DeserializeAsync(input, stream, typeof(Input) cancellationToken);
To use other resolvers use the appropriate overloads,e.g.:
var serialized = JsonSerializer.NonGeneric.Utf16.Serialize<Input, IncludeNullsOriginalCaseResolver<char>>(includeNull);
Pretty Printing:
var pretty = JsonSerializer.PrettyPrinter.Print(serialized); // this works by reading the JSON and writing it out again with spaces and line breaks
Minify:
var minified = JsonSerializer.Minifier.Minify(serialized); // this works by reading the JSON and writing it out again without spaces and line breaks
Full example: using System;
using SpanJson;
namespace Test
{
public class Program
{
private static void Main(string[] args)
{
var input = new Input { Text = "Hello World" };
var serialized = JsonSerializer.Generic.Utf16.Serialize(input);
var deserialized = JsonSerializer.Generic.Utf16.Deserialize<Input>(serialized);
}
}
public class Input
{
public string Text { get; set; }
}
} using System;
using SpanJson;
namespace Test
{
// This JsonConstructorAttribute assumes that the constructor parameter names are the same as the member names (case insensitive comparison, order is not important)
public class DefaultDO
{
[JsonConstructor]
public DefaultDO(string key, int value)
{
Key = key;
Value = value;
}
public string Key { get; }
public int Value { get; }
}
// This JsonConstructorAttribute allows overwriting the matching names of the constructor parameter names to allow for different member names vs. constructor parameter names, order is important here
public readonly struct NamedDO
{
[JsonConstructor(nameof(Key), nameof(Value))]
public NamedDO(string first, int second)
{
Key = first;
Value = second;
}
public string Key { get; }
public int Value { get; }
}
} // Type with a custom serializer to (de)serialize the long value into/from string
public class TestDTO
{
[JsonCustomSerializer(typeof(LongAsStringFormatter), "Hello World")]
public long Value { get; set; }
}
// Serializes the Long into a string
public sealed class LongAsStringFormatter : ICustomJsonFormatter<long>
{
public static readonly LongAsStringFormatter Default = new LongAsStringFormatter();
public object Arguments {get;set;} // the Argument from the attribute will be assigned
public void Serialize(ref JsonWriter<char> writer, long value)
{
StringUtf16Formatter.Default.Serialize(ref writer, value.ToString(CultureInfo.InvariantCulture));
}
public long Deserialize(ref JsonReader<char> reader)
{
var value = StringUtf16Formatter.Default.Deserialize(ref reader);
if (long.TryParse(value, out long longValue))
{
return longValue;
}
throw new InvalidOperationException("Invalid value.");
}
public void Serialize(ref JsonWriter<byte> writer, long value)
{
StringUtf8Formatter.Default.Serialize(ref writer, value.ToString(CultureInfo.InvariantCulture));
}
public long Deserialize(ref JsonReader<byte> reader)
{
var value = StringUtf8Formatter.Default.Deserialize(ref reader);
if (long.TryParse(value, out long longValue))
{
return longValue;
}
throw new InvalidOperationException("Invalid value.");
}
} // It's possible to annotate custom types a custom formatter to always use the custom formatter
[JsonCustomSerializer(typeof(TwcsCustomSerializer))]
public class TypeWithCustomSerializer : IEquatable<TypeWithCustomSerializer>
{
public long Value { get; set; }
}
// Instead of copying the implementation of for serialize/deserialize for utf8/utf16
// it is possible to use the writer/reader methods which support both, there is no or only a very minor performance difference
public sealed class TwcsCustomSerializer : ICustomJsonFormatter<TypeWithCustomSerializer>
{
public static readonly TwcsCustomSerializer Default = new TwcsCustomSerializer();
public object Arguments { get; set; }
private void SerializeInternal<TSymbol>(ref JsonWriter<TSymbol> writer, TypeWithCustomSerializer value) where TSymbol : struct
{
if (value == null)
{
writer.WriteNull();
return;
}
writer.WriteBeginObject();
writer.WriteName(nameof(TypeWithCustomSerializer.Value));
writer.WriteInt64(value.Value);
writer.WriteEndObject();
}
public void Serialize(ref JsonWriter<byte> writer, TypeWithCustomSerializer value)
{
SerializeInternal(ref writer, value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private TypeWithCustomSerializer DeserializeInternal<TSymbol>(ref JsonReader<TSymbol> reader) where TSymbol : struct
{
if (reader.ReadIsNull())
{
return null;
}
reader.ReadBeginObjectOrThrow();
var result = new TypeWithCustomSerializer {Value = reader.ReadInt64()};
reader.ReadEndObjectOrThrow();
return result;
}
public TypeWithCustomSerializer Deserialize(ref JsonReader<byte> reader)
{
return DeserializeInternal(ref reader);
}
public void Serialize(ref JsonWriter<char> writer, TypeWithCustomSerializer value)
{
SerializeInternal(ref writer, value);
}
public TypeWithCustomSerializer Deserialize(ref JsonReader<char> reader)
{
return DeserializeInternal(ref reader);
}
} // Below class will serialize Key and Value and any additional key-value-pair from the dictionary
public class ExtensionTest
{
public string Key;
public string Value;
[JsonExtensionData]
public IDictionary<string, object> AdditionalValues { get; set; }
} ASP.NET Core 3.1 FormatterYou can enable SpanJson as the default JSON formatter in ASP.NET Core 3.1 by using the Nuget package SpanJson.AspNetCore.Formatter.
To enable it, add one of the following extension methods to the
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().AddSpanJson();
}
Custom ResolverAs each option is a concrete class it is infeasible to supply concrete classes for each possible option combination. To support a custom combination implement your own custom formatter resolvers public sealed class CustomResolver<TSymbol> : ResolverBase<TSymbol, CustomResolver<TSymbol>> where TSymbol : struct
{
public CustomResolver() : base(new SpanJsonOptions
{
NullOption = NullOptions.ExcludeNulls,
NamingConvention = NamingConventions.CamelCase,
EnumOption = EnumOptions.Integer
})
{
}
} and pass this type just the same as e.g. TODO
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论