-
-
Notifications
You must be signed in to change notification settings - Fork 347
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get licenses from embedded schema, skip bad modules in deserialize
- Loading branch information
Showing
11 changed files
with
203 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
using log4net; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace CKAN | ||
{ | ||
/// <summary> | ||
/// [De]serializes a dictionary that might have some questionably | ||
/// valid data in it. | ||
/// If exceptions are thrown for any key/value pair, leave it out. | ||
/// Removes CkanModule objects from AvailableModule.module_version | ||
/// if License throws BadMetadataKraken. | ||
/// </summary> | ||
public class JsonLeakySortedDictionaryConverter<K, V> : JsonConverter | ||
{ | ||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
{ | ||
var dict = new SortedDictionary<K, V>(); | ||
foreach (var kvp in JObject.Load(reader)) | ||
{ | ||
try | ||
{ | ||
dict.Add( | ||
(K)Activator.CreateInstance(typeof(K), kvp.Key), | ||
kvp.Value.ToObject<V>()); | ||
} | ||
catch (Exception exc) | ||
{ | ||
log.WarnFormat($"Failed to deserialize {kvp.Key}", exc); | ||
} | ||
} | ||
return dict; | ||
} | ||
|
||
/// <summary> | ||
/// Use default serializer for writing | ||
/// </summary> | ||
public override bool CanWrite => false; | ||
|
||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
/// <summary> | ||
/// We *only* want to be triggered for types that have explicitly | ||
/// set an attribute in their class saying they can be converted. | ||
/// By returning false here, we declare we're not interested in participating | ||
/// in any other conversions. | ||
/// </summary> | ||
/// <returns> | ||
/// false | ||
/// </returns> | ||
public override bool CanConvert(Type objectType) | ||
{ | ||
return false; | ||
} | ||
|
||
private static readonly ILog log = LogManager.GetLogger(typeof(JsonLeakySortedDictionaryConverter<K, V>)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.