using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Newtonsoft.Json;
namespace Artemis.Core
{
///
/// A static helper class that serializes and deserializes JSON with the Artemis Core JSON settings
///
public static class CoreJson
{
#region Serialize
///
/// Serializes the specified object to a JSON string.
///
/// The object to serialize.
/// If set to true sets TypeNameHandling to
/// A JSON string representation of the object.
[DebuggerStepThrough]
public static string SerializeObject(object? value, bool handleTypeNames = false)
{
return JsonConvert.SerializeObject(value, handleTypeNames ? Constants.JsonConvertTypedSettings : Constants.JsonConvertSettings);
}
#endregion
#region Deserialize
///
/// Deserializes the JSON to a .NET object.
///
/// The JSON to deserialize.
/// If set to true sets TypeNameHandling to
/// The deserialized object from the JSON string.
[DebuggerStepThrough]
public static object? DeserializeObject(string value, bool handleTypeNames = false)
{
return JsonConvert.DeserializeObject(value, handleTypeNames ? Constants.JsonConvertTypedSettings : Constants.JsonConvertSettings);
}
///
/// Deserializes the JSON to the specified .NET type.
///
/// The JSON to deserialize.
/// The of object being deserialized.
/// If set to true sets TypeNameHandling to
/// The deserialized object from the JSON string.
[DebuggerStepThrough]
public static object? DeserializeObject(string value, Type type, bool handleTypeNames = false)
{
return JsonConvert.DeserializeObject(value, type, handleTypeNames ? Constants.JsonConvertTypedSettings : Constants.JsonConvertSettings);
}
///
/// Deserializes the JSON to the specified .NET type.
///
/// The type of the object to deserialize to.
/// The JSON to deserialize.
/// If set to true sets TypeNameHandling to
/// The deserialized object from the JSON string.
[DebuggerStepThrough]
[return: MaybeNull]
public static T DeserializeObject(string value, bool handleTypeNames = false)
{
return JsonConvert.DeserializeObject(value, handleTypeNames ? Constants.JsonConvertTypedSettings : Constants.JsonConvertSettings);
}
#endregion
}
}