public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        var list  = (IList <LinkedListNode <T> >)value;
        var table = LinkedListNodeOrderTable <T> .FromList(list);

        serializer.Serialize(writer, table);
    }
    public static LinkedListNodeOrderTable <T> FromList(IList <LinkedListNode <T> > nodeList)
    {
        if (nodeList == null)
        {
            return(null);
        }
        var list  = nodeList.Select(n => n.List).Distinct().SingleOrDefault();
        var table = new LinkedListNodeOrderTable <T>(list);

        if (list == null)
        {
            return(table);
        }
        var dictionary = list.EnumerateNodes().Select((n, i) => new KeyValuePair <LinkedListNode <T>, int>(n, i)).ToDictionary(p => p.Key, p => p.Value);

        table.Indices = nodeList.Select(n => (n == null ? -1 : dictionary[n])).ToList();
        return(table);
    }
 public static LinkedListNodeOrderTable <T> FromList(IList <LinkedListNode <T> > nodeList)
 {
     if (nodeList == null)
     {
         return(null);
     }
     try
     {
         var list       = nodeList.Where(n => n != null).Select(n => n.List).Distinct().SingleOrDefault();
         var table      = new LinkedListNodeOrderTable <T>(list);
         var dictionary = list == null ? null : list.EnumerateNodes().Select((n, i) => new KeyValuePair <LinkedListNode <T>, int>(n, i)).ToDictionary(p => p.Key, p => p.Value);
         table.Indices = nodeList.Select(n => (n == null ? -1 : dictionary[n])).ToList();
         return(table);
     }
     catch (Exception ex)
     {
         throw new JsonSerializationException(string.Format("Failed to construct LinkedListNodeOrderTable<{0}>", typeof(T)), ex);
     }
 }