gpt4 book ai didi

c# - 将 JsonSerializerSettings 从 JsonSerializer 复制到新的 JsonSerializer

转载 作者:可可西里 更新时间:2023-11-01 07:57:23 26 4
gpt4 key购买 nike

有什么方法可以从 JsonSerializer 类中取出设置并在新的 JsonSerializer 中重新实现它们?

似乎没有任何方法可以做那样的事情。我发现最好的是通过反射调用的私有(private)方法,ApplySerializerSettings

我正在尝试从 WriteJson 方法中获取序列化器并将其复制到一个新的序列化器中,稍微调整一下。具体来说,我想替换 ContractResolver

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)

最佳答案

似乎最好的方法是将所有设置复制到一个新对象中。它们有很多,所以这里有一个很好的扩展方法(从 8.0.3 开始)。

public static class JsonSerializerExtensions
{
public static JsonSerializer DeepCopy(this JsonSerializer serializer)
{
var copiedSerializer = new JsonSerializer
{
Context = serializer.Context,
Culture = serializer.Culture,
ContractResolver = serializer.ContractResolver,
ConstructorHandling = serializer.ConstructorHandling,
CheckAdditionalContent = serializer.CheckAdditionalContent,
DateFormatHandling = serializer.DateFormatHandling,
DateFormatString = serializer.DateFormatString,
DateParseHandling = serializer.DateParseHandling,
DateTimeZoneHandling = serializer.DateTimeZoneHandling,
DefaultValueHandling = serializer.DefaultValueHandling,
EqualityComparer = serializer.EqualityComparer,
FloatFormatHandling = serializer.FloatFormatHandling,
Formatting = serializer.Formatting,
FloatParseHandling = serializer.FloatParseHandling,
MaxDepth = serializer.MaxDepth,
MetadataPropertyHandling = serializer.MetadataPropertyHandling,
MissingMemberHandling = serializer.MissingMemberHandling,
NullValueHandling = serializer.NullValueHandling,
ObjectCreationHandling = serializer.ObjectCreationHandling,
PreserveReferencesHandling = serializer.PreserveReferencesHandling,
ReferenceResolver = serializer.ReferenceResolver,
ReferenceLoopHandling = serializer.ReferenceLoopHandling,
StringEscapeHandling = serializer.StringEscapeHandling,
TraceWriter = serializer.TraceWriter,
TypeNameHandling = serializer.TypeNameHandling,
SerializationBinder = serializer.SerializationBinder,
TypeNameAssemblyFormatHandling = serializer.TypeNameAssemblyFormatHandling
};
foreach (var converter in serializer.Converters)
{
copiedSerializer.Converters.Add(converter);
}
return copiedSerializer;
}
}

虽然很丑,但至少你只需要写一次。稍微小心一点,因为属性本​​身不会被深度复制。

下面的答案取决于您的实现情况,尤其是在解决契约(Contract)时。保留它以防万一它对某人有帮助。


所以,我不能完全复制设置,但我发现了一个可能需要考虑的很好的解决方法。您可以在锁定的上下文中简单地设置要更改的属性,然后再重置它们。

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var thing = value as IThing;

if (thing == null)
throw new ArgumentException($"Writing Json failed because " +
"value was not a 'Thing' of type, {typeof(IThing).FullName}");

JObject jsonThing;
//If your solution is multithreaded,
//and is using a shared serializer (which you probably are),
//you should lock the serializer so that it doesn't accidentally use
//the "CustomObjectResolver"
lock (serializer)
{
//Hold the original value(s) to reset later
var originalContractResolver = serializer.ContractResolver;
//Set custom value(s)
serializer.ContractResolver = new CustomObjectResolver();
//Serialization with custom properties
jsonThing = JObject.FromObject(value, serializer);
//Reset original value(s)
serializer.ContractResolver = originalContractResolver;
}

//Finish serializing and write to writer.
}

关于c# - 将 JsonSerializerSettings 从 JsonSerializer 复制到新的 JsonSerializer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38230326/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com