0

I have a lot of entities which previously used to have properties of type for example string. I need to change these to a custom type - MultilingualValue<T>, where T in this case would be string. I can easily convert from string to the custom Type. Is it possible to configure JSON.Net such that everywhere it encounters a conversion from any type, to a MultilingualValue<T>, some custom code is called rather than it's native conversion?

Sample code

public class ProductBefore
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class ProductAfter
{
    public int Id { get; set; }
    public MultilingualValue<string> Name { get; set; }
}

I would like to be able to deserialize anything which was stored as ProductBefore, into ProductAfter automatically. The MultilingualValue<string> can be initialised with a string parameter in the constructor, so it's relatively easy to create it from the original string.

4

1 回答 1

0

可以通过添加 Custom Converter。这些可以按以下方式添加:

    private void createJsonSerializer()
    {
        JsonSerializerSettings settingsSerialize = new JsonSerializerSettings();

        settingsSerialize.Converters.Add(CustomConverter.Instance);

        _jsonSeriazlier = Newtonsoft.Json.JsonSerializer.Create(settingsSerialize);

    }

以防万一它对任何人都有帮助,下面是自定义转换器本身:

    public class CustomConverter : JsonConverter
    {
        public CustomConverter()
        {


        }
        public override bool CanWrite
        {
            get { return false; }
        }

        private static readonly CustomConverter _instance = new CustomConverter();

        public static CustomConverter Instance
        {
            get { return _instance; }
        }
        public override bool CanConvert(Type objectType)
        {

            return (objectType.IsAssignableFrom(typeof(MultilingualValue<MultilingualValueMetaData<string>>)));

        }

           public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
           {
               MultilingualValue<MultilingualValueMetaData<string>> result = new MultilingualValue<MultilingualValueMetaData<string>>();
               JsonToken firstToken = reader.TokenType;
               reader.Read();//skip first Token
               while (reader.TokenType != JsonToken.EndObject)
               {
                   string languageType = (string) reader.Value;
                   reader.Read();
                   MultilingualValueMetaData<string> metaData = null;
                   if (reader.TokenType == JsonToken.StartObject)
                   {
                       metaData = serializer.Deserialize<MultilingualValueMetaData<string>>(reader);
                   }
                   else
                   {
                       metaData = new MultilingualValueMetaData<string>();
                       metaData.AutoTranslation = (string) reader.Value;


                   }
                   result[languageType] = metaData;
                   reader.Read();
               }
               return result;


           }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {



        }
    }
于 2013-04-18T11:31:59.027 回答