Serializing an object with an invalid enumeration value

I have a Windows Phone 7 project that connects to a .NET web service to retrieve data on demand. Both the WP7 project and the web service use a copy of the same C # class library. Part of this library is an enumeration EmployeeType:

Public enum EmployeeType
{
    Standard = 0,
    TeamLeader = 105
}

Since the application was released, a change was made to the web service class library — adding a new enumeration value. ( SeniorManager = 110)

Therefore, when I receive an object on the phone with a property containing the new enum value, I get it SerializationExceptionwhen I try to add it to IsolStorage. This is because the new enum value cannot be serialized because the WP7 class library did not have the same update as the enum value does not exist.

What I would like to achieve is to still be able to serialize the object, but ignore the invalid enum value or replace it with a valid (ideally Standard = 0) one.

This will allow me to handle any future additions made to the web service enumeration without disrupting the functionality of the application.

Notes:

  • The above values ​​are listed for the purpose of the question, not the actual listing in question.
  • The fact that the value will be serialized with the wrong value does not matter for the purpose of the application.

Thank!

0
source share
2 answers

JSON.NET (reader - StreamReader . TResponse - , .):

var serializer = new JsonSerializer();
serializer.Converters.Add(new JsonEnumTypeConverter());
this.Response = serializer.Deserialize<TResponse>(new JsonTextReader(reader));

JsonEnumTypeConverter , enum, , . , . ( ). :

private class JsonEnumTypeConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return objectType.IsEnum;
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        try
        {
            var value = Enum.Parse(objectType, reader.Value.ToString(), true);
            if (IsFlagDefined((Enum)value))
            {
                return value;
            }
            else
            {
                return 0;
            }
        }
        catch (Exception)
        {
            return 0;
        }
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        writer.WriteValue(value);
    }

    private static bool IsFlagDefined(Enum e)
    {
        decimal d;
        return (!decimal.TryParse(e.ToString(), out d));
    }
}

, IsFlagDefined , Enum.IsDefined, [Flags], Enum.IsDefined . (: Enum.IsDefined )

, , JSON.NET JsonConverter.

+2

, WebService WP7, .

, workaorund:

  • ,

    [NonSerialized]
    public EmployeeType RealType {get; set;} 
    
  • , , Standard, Realfield RealField

    public EmployeeType Type 
     {
      get{
           if (RealType== EmployeeType.TeamLeader)
              return  EmployeeType.TeamLeader;
           else 
              return  EmployeeType.Standard;
         } 
      set{RealType=value;}
     }     
    
+1

All Articles