Azure-functions-host: TypeConverter ignored in Azure Functions

Created on 11 Mar 2017  路  4Comments  路  Source: Azure/azure-functions-host

I am trying to serialize/deserialize an object to store in DocumentDB in my Azure Function. This object contains a Dictionary with a complex key: a Point. I implemented a TypeConverter for the point (actually, the MSDN example of a TypeConverter is for a Point class: https://msdn.microsoft.com/en-us/library/ayybcxe5.aspx)
When converting this dictionary with JsonConvert.SerializeObject in a console application, JsonConvert picks up correctly the TypeConverter and the key values are written correctly. However, when I do the same in an Azure Function, it looks like the TypeConverter is ignored.

Point.csx:

using System;
using System.ComponentModel;
using System.Globalization;

[TypeConverter(typeof(PointConverter))]
public class Point
{
    public Point() { }
    public Point (int x, int y)
    {
        X = x;
        Y = y;
    }

    public int X { get; set; }
    public int Y { get; set; }
}

public class PointConverter : TypeConverter
{
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        if (sourceType == typeof(string))
        {
            return true;
        }
        return base.CanConvertFrom(context, sourceType);
    }

    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
    {
        if (value is string)
        {
            string[] v = ((string)value).Split(new char[] { ',' });
            return new Point(int.Parse(v[0]), int.Parse(v[1]));
        }
        return base.ConvertFrom(context, culture, value);
    }

    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        if (destinationType == typeof(string))
        {
            return ((Point)value).X + "," + ((Point)value).Y;
        }
        return base.ConvertTo(context, culture, value, destinationType);
    }
}

Code to convert the Dictionary:

Dictionary<Point, string> test = new Dictionary<Point, string>();
test[new Point(0, 0)] = "foo";
string output = JsonConvert.SerializeObject(test);

Repro steps

  1. run the code in an Azure Function

Expected behavior

The output string looks like this: "{"0,0":"foo"}"

Actual behavior

The output string looks like this: "{"Submission#0+Point":"foo"}"

Known workarounds

N/A

Related information

  • Programming language used: C#
needs-investigation

Most helpful comment

We encountering this problem currently, a typeconvertor in a compiled function that is not being used when de/serialising. Same binaries work correctly when in a webapp. All using Newtonsoft 9.0.1
Has anyone found a resolution or workaround?

All 4 comments

@helgemahrt -- I'm assuming you're using an output binding to insert the document. Can you share what your function code looks like? I'm specifically curious about the method signature (I want to see what type you're using for the document).

Has any progress been made on this? We're likely dealing with a type binding mismatch (likely different versions of JSON.NET) causing the TypeConverter type used to be different than what the runtime/SDK are looking for.

Closing due to inactivity

We encountering this problem currently, a typeconvertor in a compiled function that is not being used when de/serialising. Same binaries work correctly when in a webapp. All using Newtonsoft 9.0.1
Has anyone found a resolution or workaround?

Was this page helpful?
0 / 5 - 0 ratings