Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReadJson does not respect types when dealing with anonymous types #1

Open
PaulDevenney opened this issue Sep 11, 2014 · 1 comment
Open

Comments

@PaulDevenney
Copy link

The following example fails to convert successfully as during deserialization it creates the Expression as Expression<Func<Profile, Anonymous[string, string]>> rather than Expression<Func<Profile, object>>, even though that is the object type passed into the converter.ReadJson method

    static void Main(string[] args)
    {
        var profile = new Profile()
        {
            Email = "[email protected]",SiteCode = "www.testweb.com",FirstName = "bob"
        };

        var settings = new JsonSerializerSettings() {TypeNameHandling = TypeNameHandling.Objects};
        settings.Converters.Add(new ExpressionConverter<Expression<Func<Profile, object>>>());

        var indexDefinition2 = new FoundocIndex<Profile>()
        {
            IndexType = IndexType.ImmediatelyConsistent,
            IsUniqueConstraint = true,
            Name = "ByEmail",
            Map = (p => new { profile.SiteCode, profile.Email })
        };

        var test2 = JsonConvert.SerializeObject(indexDefinition2, settings);
        var indexDefinition3 = JsonConvert.DeserializeObject<FoundocIndex<Profile>>(test2, settings);//<--fails
    }


public class ExpressionConverter<T> : JsonConverter where T:class
{
    public override bool CanConvert(Type objectType)
    {
        bool canConvert = objectType == typeof (T);
        return canConvert;
    }
    public override bool CanRead
    {
        get
        {
            return true;
        }
    }
    public override bool CanWrite
    {
        get
        {
            return true;
        }
    }
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        var expression = (Expression)value;
        var converter = new Aq.ExpressionJsonSerializer.ExpressionJsonConverter(this.GetType().Assembly);
        converter.WriteJson(writer, expression, new JsonSerializer());
    }
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        var converter = new Aq.ExpressionJsonSerializer.ExpressionJsonConverter(this.GetType().Assembly);
        var result = converter.ReadJson(reader, objectType, existingValue, serializer);


        return result;
    }
}
@aquilae
Copy link
Owner

aquilae commented Sep 25, 2014

I'm sorry, but I don't have time to support this library atm, plus it's not relevant to my current field of work, unfortunately, so it is unlikely I will review the code any time soon.
You are welcome to fork and patch, though, I'm glad someone tried and used it.

aquilae pushed a commit that referenced this issue Apr 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants