d3990c1219
- Improves load speed - Lowers memory overhead
158 lines
5.8 KiB
C#
158 lines
5.8 KiB
C#
using System.Text.Encodings.Web;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using Core.Models;
|
|
using Core.Models.Eft.Common.Tables;
|
|
using Core.Models.Eft.Ws;
|
|
using Core.Models.Enums;
|
|
using Core.Models.Spt.Dialog;
|
|
using Core.Utils.Json.Converters;
|
|
using SptCommon.Annotations;
|
|
|
|
namespace Core.Utils;
|
|
|
|
[Injectable(InjectionType.Singleton)]
|
|
public class JsonUtil
|
|
{
|
|
private static readonly JsonSerializerOptions jsonSerializerOptionsNoIndent = new()
|
|
{
|
|
WriteIndented = false,
|
|
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
|
|
UnmappedMemberHandling = JsonUnmappedMemberHandling.Disallow,
|
|
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
|
|
Converters =
|
|
{
|
|
new ListOrTConverterFactory(),
|
|
new DictionaryOrListConverter(),
|
|
new EftEnumConverter<SptAirdropTypeEnum>(),
|
|
new EftEnumConverter<GiftSenderType>(),
|
|
new EftEnumConverter<SeasonalEventType>(),
|
|
new EftEnumConverter<ProfileChangeEventType>(),
|
|
new EftEnumConverter<QuestStatusEnum>(),
|
|
new EftEnumConverter<RewardType>(),
|
|
new EftEnumConverter<SideType>(),
|
|
new EftEnumConverter<BonusSkillType>(),
|
|
new EftEnumConverter<NotificationEventType>(),
|
|
new EftEnumConverter<QuestTypeEnum>(),
|
|
new EftEnumConverter<RewardType>(),
|
|
new EftEnumConverter<ExitStatus>(),
|
|
new EftEnumConverter<MemberCategory>(),
|
|
new EftEnumConverter<PinLockState>(),
|
|
new EftEnumConverter<PlayerSideMask>(),
|
|
new EftEnumConverter<DamageEffectType>(),
|
|
new EftEnumConverter<RepairStrategyType>(),
|
|
new EftEnumConverter<ThrowWeapType>(),
|
|
new EftEnumConverter<EventType>(),
|
|
new EftEnumConverter<TraderServiceType>(),
|
|
new EftEnumConverter<CurrencyType>(),
|
|
new EftEnumConverter<RadioStationType>(),
|
|
new EftEnumConverter<ArmorMaterial>(),
|
|
new EftEnumConverter<RequirementState>(),
|
|
new EftEnumConverter<ExfiltrationType>(),
|
|
new EftEnumConverter<EquipmentSlots>(),
|
|
new EftListEnumConverter<EquipmentSlots>(),
|
|
new BaseInteractionRequestDataConverter()
|
|
}
|
|
};
|
|
|
|
private static readonly JsonSerializerOptions jsonSerializerOptionsIndented = new(jsonSerializerOptionsNoIndent)
|
|
{
|
|
WriteIndented = true
|
|
};
|
|
|
|
|
|
/// <summary>
|
|
/// Convert JSON into an object
|
|
/// </summary>
|
|
/// <typeparam name="T">The type of the object to deserialize to</typeparam>
|
|
/// <param name="json">The JSON string to deserialize</param>
|
|
/// <returns>Deserialized object or null</returns>
|
|
public T? Deserialize<T>(string? json)
|
|
{
|
|
return string.IsNullOrEmpty(json) ? default : JsonSerializer.Deserialize<T>(json, jsonSerializerOptionsNoIndent);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert JSON into an object
|
|
/// </summary>
|
|
/// <param name="json">The JSON string to deserialize</param>
|
|
/// <param name="type">The type of the object to deserialize to</param>
|
|
/// <returns></returns>
|
|
public object? Deserialize(string? json, Type type)
|
|
{
|
|
return string.IsNullOrEmpty(json) ? null : JsonSerializer.Deserialize(json, type, jsonSerializerOptionsNoIndent);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert JSON into an object from a file
|
|
/// </summary>
|
|
/// <param name="file">The JSON File to read</param>
|
|
/// <returns></returns>
|
|
public T? DeserializeFromFile<T>(string file)
|
|
{
|
|
if (!File.Exists(file))
|
|
{
|
|
return default;
|
|
}
|
|
|
|
using (FileStream fs = new(file, FileMode.Open, FileAccess.Read))
|
|
{
|
|
return JsonSerializer.Deserialize<T>(fs, jsonSerializerOptionsNoIndent);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert JSON into an object from a file
|
|
/// </summary>
|
|
/// <param name="file">The JSON File to read</param>
|
|
/// <param name="type">The type of the object to deserialize to</param>
|
|
/// <returns></returns>
|
|
public object? DeserializeFromFile(string file, Type type)
|
|
{
|
|
if (!File.Exists(file))
|
|
{
|
|
return default;
|
|
}
|
|
|
|
using (FileStream fs = new(file, FileMode.Open, FileAccess.Read))
|
|
{
|
|
return JsonSerializer.Deserialize(fs, type, jsonSerializerOptionsNoIndent);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert JSON into an object from a FileStream
|
|
/// </summary>
|
|
/// <param name="fs">The file stream to deserialize</param>
|
|
/// <param name="type">The type of the object to deserialize to</param>
|
|
/// <returns></returns>
|
|
public object? DeserializeFromFileStream(FileStream fs, Type type)
|
|
{
|
|
return JsonSerializer.Deserialize(fs, type, jsonSerializerOptionsNoIndent);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert an object into JSON
|
|
/// </summary>
|
|
/// <typeparam name="T">Type of the object being serialised</typeparam>
|
|
/// <param name="obj">Object to serialise</param>
|
|
/// <param name="indented">Should JSON be indented</param>
|
|
/// <returns>Serialised object as JSON, or null</returns>
|
|
public string? Serialize<T>(T? obj, bool indented = false)
|
|
{
|
|
return obj == null ? null : JsonSerializer.Serialize(obj, indented ? jsonSerializerOptionsIndented : jsonSerializerOptionsNoIndent);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert an object into JSON
|
|
/// </summary>
|
|
/// <param name="obj">Object to serialise</param>
|
|
/// <param name="type">Type of object being serialsied</param>
|
|
/// <param name="indented">Should JSON be indented</param>
|
|
/// <returns></returns>
|
|
public string? Serialize(object? obj, Type type, bool indented = false)
|
|
{
|
|
return obj == null ? null : JsonSerializer.Serialize(obj, type, indented ? jsonSerializerOptionsIndented : jsonSerializerOptionsNoIndent);
|
|
}
|
|
}
|