c# - custom conversion from T to a MultilingualValue<T> in JSON.Net -


i have lot of entities used have properties of type example string. need change these custom type - multilingualvalue<t>, t in case string. can convert string custom type. possible configure json.net such everywhere encounters conversion type, multilingualvalue<t>, custom code called rather 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 able deserialize stored productbefore, productafter automatically. multilingualvalue<string> can initialised string parameter in constructor, it's relatively easy create original string.

it possible through adding custom converter. these can added per below:

    private void createjsonserializer()     {         jsonserializersettings settingsserialize = new jsonserializersettings();          settingsserialize.converters.add(customconverter.instance);          _jsonseriazlier = newtonsoft.json.jsonserializer.create(settingsserialize);      } 

just in case of anyone, below custom converter itself:

    public class customconverter : jsonconverter     {         public customconverter()         {           }         public override bool canwrite         {             { return false; }         }          private static readonly customconverter _instance = new customconverter();          public static customconverter instance         {             { 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)         {            }     } 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -