using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using StardewModdingAPI.Framework.Models;
namespace StardewModdingAPI.Framework.Serialisation.SmapiConverters
{
/// Handles deserialisation of arrays.
internal class ManifestDependencyArrayConverter : JsonConverter
{
/*********
** Accessors
*********/
/// Whether this converter can write JSON.
public override bool CanWrite => false;
/*********
** Public methods
*********/
/// Get whether this instance can convert the specified object type.
/// The object type.
public override bool CanConvert(Type objectType)
{
return objectType == typeof(IManifestDependency[]);
}
/*********
** Protected methods
*********/
/// Read the JSON representation of the object.
/// The JSON reader.
/// The object type.
/// The object being read.
/// The calling serializer.
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
List result = new List();
foreach (JObject obj in JArray.Load(reader).Children())
{
string uniqueID = obj.Value(nameof(IManifestDependency.UniqueID));
string minVersion = obj.Value(nameof(IManifestDependency.MinimumVersion));
bool required = obj.Value(nameof(IManifestDependency.IsRequired)) ?? true;
result.Add(new ManifestDependency(uniqueID, minVersion, required));
}
return result.ToArray();
}
/// Writes the JSON representation of the object.
/// The JSON writer.
/// The value.
/// The calling serializer.
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new InvalidOperationException("This converter does not write JSON.");
}
}
}