using jsonconvert in a TypeConverter (recursion)

Apr 7, 2010 at 3:50 AM

I need to create a TypeConverter for a custom type.  In Converting to string I was looking to use Jsonconvert.  But as it tuns out the simple case, JSON convert is using typeConverter too, which gives me an recursion (stack overflow).

Any suggestions?  It recurses in the ConvertTo call.

thanks, jeff

 

 

    public class GridLayoutDataTypeConverter : TypeConverter
    {
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value == null)
                return new GridLayoutData();

            GridLayoutData gridLayoutData = JsonConvert.DeserializeObject<GridLayoutData>((String)value);
            return gridLayoutData;
        }
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType != typeof(string))
                return false;

            return true;
        }
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (value == null)
                return value;

            String result = JsonConvert.SerializeObject( (GridLayoutData)value, Formatting.None);

            return result;
        }
    }