hemtros
5/8/2020 - 9:46 PM

Custom JSON Converter

public class UserAssociationJSONConverter<T> : JsonConverter where T : new()
    {
        private string _entity;
        private List<string> _validEntities = new List<string>() { "role", "market", "wirecenter" };
        public UserAssociationJSONConverter(string entity)
        {
            if (!_validEntities.Contains(entity))
            {
                throw new InvalidOperationException("entity is not valid");
            }
            _entity = entity;
        }
        public override bool CanConvert(Type objectType)
        {
            return typeof(List<T>).IsAssignableFrom(objectType);
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            List<T> resultAssociation = new List<T>();

            if (reader.TokenType == JsonToken.StartObject)
            {
                JObject item = JObject.Load(reader);

                string root = string.Empty;
                string rootChild = string.Empty;

                if (_entity == "role") //typeof(T) == typeof(AppSecureUserRole)
                {
                    root = "roles";
                    rootChild = "role";
                }
                else if (_entity == "market") //typeof(T) == typeof(AppSecureUserMarket)
                {
                    root = "markets";
                    rootChild = "market";
                }
                else if (_entity == "wirecenter") //typeof(T) == typeof(AppSecureUserWirecenter)
                {
                    root = "wirecenters";
                    rootChild = "wirecenter";
                }
                

                if (item[root] != null)
                {
                    var associations = item[root][rootChild];
                    var rdr = associations.CreateReader();
                    int cnt = associations.Count();
                    if (associations is JObject)
                    {
                        //resultAssociation = new List<T>
                        //{
                        //    associations.ToObject<T>(serializer)
                        //};
                        T obj = new T();
                        serializer.Populate(rdr, obj);
                        resultAssociation.Add(obj);
                    }
                    else if (associations is JArray)
                    {
                        //associations.ToObject<List<T>>(serializer) --This doesn't work as it call the canconvert and readjson again with jsontoken of startarray
                        serializer.Populate(rdr, resultAssociation);
                    }
                    else
                    {
                        throw new JsonException("Couldn't deserialize as association is not JObject or JArray");
                    }

                    return resultAssociation;
                }
            }
            throw new JsonException("Couldn't deserialize the JSON to object");
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }
    }