2012-12-18 9 views
10

Um serialize ein Wörterbuch mit NodaTime.Instance JSon json.net mit feinen funktioniert, aber auf Deserialisierung wirft es Newtonsoft.Json. JsonSerializationException. Der Test zeigt das Problem:Wie ein Wörterbuch mit NodaTime.Instant deserialize mit Json.net ohne Ausnahme zu bekommen?

[Test] 
public void DeserializeDictionaryThowsException() { 
    JsonConverter[] converters = { NodaConverters.IntervalConverter, NodaConverters.InstantConverter }; 

    var dictionary = new Dictionary<Instant, int>() { 
     {Instant.FromUtc(2012, 1, 2, 3, 4, 5), 0} 
    };    
    var json = JsonConvert.SerializeObject(dictionary, Formatting.None, converters); 
    Assert.AreEqual("{\"2012-01-02T03:04:05Z\":0}", json); //ok 
    var result = JsonConvert.DeserializeObject<Dictionary<Instant, int>>(json, converters); // throws 
} 

DeserializeObject wirft:

Newtonsoft.Json.JsonSerializationException: Kann nicht konvertieren string '2012-01-02T03: 04: 05Z' Schlüsselart ‚Zum Wörterbuch NodaTime .Sofortig'. Erstellen Sie einen TypeConverter, um von der Zeichenfolge in das Schlüsseltypobjekt zu konvertieren. Zeile 1, Position 24. ----> Newtonsoft.Json.JsonSerializationException: Fehler beim Konvertieren des Werts "2012-01-02T03: 04: 05Z" in "NodaTime.Instant". Zeile 1, Position 24. ----> System.Exception: Konnte nicht von System.String in NodaTime.Instant konvertieren oder konvertieren.

Als eine Randnotiz funktioniert Deserialisierung eines Dictionary of DateTime gut. Ich denke, da String einen Konverter für DateTime hat.

[Test] 
public void DeserializeDiciotnaryOfDateTime() // OK 
{ 
    var expected = new DateTime(2012, 1, 2, 3, 4, 5, DateTimeKind.Utc); 
    var dictionary = new Dictionary<DateTime, int>() { { expected, 0 } }; 
    var json = JsonConvert.SerializeObject(dictionary);  
    var result = JsonConvert.DeserializeObject<Dictionary<DateTime, int>>(json); 
    Assert.AreEqual(expected, dictionary.Keys.First()); // OK 
} 
+1

Leider dies noch nie zuvor gesehen zu haben. Ich weiß nicht genug über Json.NET, um Ihnen sofort eine Antwort zu geben, aber könnten Sie einen Bug auf http://noda-time.googlecode.com einreichen? –

+1

Entschuldigung, wir haben das vorher verpasst und danke für die Beschreibung. Wir verfolgen dieses Problem [hier] (https://code.google.com/p/noda-time/issues/detail?id=237). –

Antwort

0

Sie brauchen mehr JSON.NET Konverter hinzufügen NodaTime.Instance Zeit serialisiert werden wie unten gezeigt.

public void DeserializeDictionaryThowsException() 
{ 
    var dtzProvider = DateTimeZoneCache.GetSystemDefault(); 
    JsonConverter[] converters = { NodaConverters.IntervalConverter, 
            NodaConverters.InstantConverter, 
            NodaConverters.LocalDateConverter, 
            NodaConverters.LocalDateTimeConverter, 
            NodaConverters.LocalTimeConverter, 
            NodaConverters.OffsetConverter, 
            NodaConverters.DurationConverter, 
            NodaConverters.RoundtripPeriodConverter, 
            NodaConverters.OffsetDateTimeConverter, 
            NodaConverters.CreateDateTimeZoneConverter(dtzProvider), 
            NodaConverters.CreateZonedDateTimeConverter(dtzProvider) 
           }; 

    var dictionary = new Dictionary<Instant, int>() { { Instant.FromUtc(2012, 1, 2, 3, 4, 5), 0 } }; 
    var json = JsonConvert.SerializeObject(dictionary, Formatting.None, converters); 
    Assert.AreEqual("{\"2012-01-02T03:04:05Z\":0}", json); 
    var result = JsonConvert.DeserializeObject<Dictionary<Instant, int>>(json, converters); 
} 
+0

'var dtzProvider = DateTimeZoneCache.GetSystemDefault();' gibt: ** ein Objektverweis wird für das nicht statische Feld, die Methode oder die Eigenschaft benötigt 'DateTimeZoneCache.GetSystemDefault()' ** –

+0

verwenden Sie vielleicht 'var dtzProvider = DateTimeZoneProviders.Tzdb ; 'oder BCL? –

+0

Das funktioniert nicht für mich. Ich bekomme immer noch die selbe Ausnahme wie das Poster. – Steven

0

Dieses Problem wird bei https://github.com/nodatime/nodatime.serialization/issues/2

gehefteten Ich habe von http://stackoverflow.com/q/6845364/634824 eine Arbeit um geändert. Dies ist keineswegs performant und wurde nicht vollständig getestet.

public class DictionaryWithNodaTimeKeyConverter : JsonConverter 
{ 
    private static IDateTimeZoneProvider dtzProvider = DateTimeZoneProviders.Tzdb; 
    private JsonSerializerSettings _settings; 

    public DictionaryWithNodaTimeKeyConverter(IDateTimeZoneProvider dtzProvider) 
     : base() 
    { 
     _settings = new JsonSerializerSettings().ConfigureForNodaTime(dtzProvider); 
    } 

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) 
    { 
     IDictionary dictionary = (IDictionary)value; 
     writer.WriteStartObject(); 

     foreach (object key in dictionary.Keys) 
     { 
      writer.WritePropertyName(ConvertToPropertyKey(key)); 
      serializer.Serialize(writer, dictionary[key]); 
     } 

     writer.WriteEndObject(); 
    } 

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) 
    { 
     if (reader.TokenType == JsonToken.Null) 
     { 
      return null; 
     } 

     Type keyType = objectType.GetGenericArguments()[0]; 
     Type valueType = objectType.GetGenericArguments()[1]; 

     Type intermediateDictionaryType = typeof(Dictionary<,>).MakeGenericType(typeof(string), valueType); 
     IDictionary intermediateDictionary = (IDictionary)Activator.CreateInstance(intermediateDictionaryType); 
     serializer.Populate(reader, intermediateDictionary); 

     IDictionary finalDictionary = (IDictionary)Activator.CreateInstance(objectType); 
     foreach (DictionaryEntry pair in intermediateDictionary) 
     { 
      object parsedObject; 
      if (TryConvertKey(pair.Key.ToString(), keyType, out parsedObject)) 
      { 
       finalDictionary.Add(parsedObject, pair.Value); 
      } 
     } 

     return finalDictionary; 
    } 

    public override bool CanConvert(Type objectType) 
    { 
     bool canConvert = objectType.IsA(typeof(IDictionary<,>)); 

     if (canConvert) 
     { 
      Type keyType = objectType.GetGenericArguments()[0]; 
      canConvert = canConvert && IsNodaTimeType(keyType); 
     } 

     return canConvert; 
    } 

    private bool IsNodaTimeType(Type type) 
    { 
     return type.IsA(typeof(Instant)) 
       || type.IsA(typeof(OffsetDateTime)) 
       || type.IsA(typeof(DateTimeZone)) 
       || type.IsA(typeof(ZonedDateTime)) 
       || type.IsA(typeof(LocalDateTime)) 
       || type.IsA(typeof(LocalDate)) 
       || type.IsA(typeof(LocalTime)) 
       || type.IsA(typeof(Offset)) 
       || type.IsA(typeof(Duration)) 
       || type.IsA(typeof(Period)); 
     // Interval is not Support because Interval is serialized as a compound object. 
    } 

    private string ConvertToPropertyKey(object property) 
    { 
     if (!IsNodaTimeType(property.GetType())) 
     { 
      throw new InvalidOperationException(); 
     } 

     string result = JsonConvert.SerializeObject(property, _settings); 
     if (!string.IsNullOrWhiteSpace(result)) 
     { 
      // Remove the "" from JsonConvert 
      int first = result.IndexOf('"'); 
      int last = result.LastIndexOf('"'); 
      if (first != -1 && last != -1 && first < last) 
      { 
       result = result.Substring(first + 1, last - (first + 1)); 
      } 
     } 

     return result; 
    } 

    private bool TryConvertKey(string text, Type keyType, out object value) 
    { 
     if (!IsNodaTimeType(keyType)) 
     { 
      throw new InvalidOperationException(); 
     } 

     value = keyType.CreateDefault(); 

     try 
     { 
      value = JsonConvert.DeserializeObject($"\"{text}\"", keyType, _settings); 
      return true; 
     } 
     catch 
     { 
      return false; 
     } 
    } 
} 

I definiert auch einige Erweiterungen

public static class TypeExtensions 
{ 
    public static bool IsA(this Type type, Type typeToBe) 
    { 
     if (!typeToBe.IsGenericTypeDefinition) 
      return typeToBe.IsAssignableFrom(type); 

     List<Type> toCheckTypes = new List<Type> { type }; 
     if (typeToBe.IsInterface) 
      toCheckTypes.AddRange(type.GetInterfaces()); 

     Type basedOn = type; 
     while (basedOn.BaseType != null) 
     { 
      toCheckTypes.Add(basedOn.BaseType); 
      basedOn = basedOn.BaseType; 
     } 

     return toCheckTypes.Any(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeToBe); 
    } 

    public static object CreateDefault(this Type type) 
    { 
     return type.IsValueType ? Activator.CreateInstance(type) : null; 
    } 
} 

es zu benutzen:

IDateTimeZoneProvider provider = DateTimeZoneProviders.Tzdb; 
JsonConverter[] converters = { NodaConverters.IntervalConverter, NodaConverters.InstantConverter, new DictionaryWithNodaTimeKeyConverter(provider) }; 

var dictionary = new Dictionary<Instant, int> { 
    { Instant.FromUtc(2012, 1, 2, 3, 4, 5), 0 } 
}; 

var json = JsonConvert.SerializeObject(dictionary, Formatting.None, converters); 
Console.WriteLine(json); 
var result = JsonConvert.DeserializeObject<Dictionary<Instant, int>>(json, converters); 
Verwandte Themen