Skip to content

Custom JsonConverter implementation #446

@flieks

Description

@flieks

Hi,

I am getting an error when this cypher is executed which without a custom JsonConverter worked fine.

                 var c = graphClient.Cypher
                        .Match("(q:Variable {Id: $id})")
                        .WithParam("id", variable.Id)
                        .Set("q = $variable")
                        .WithParam("variable", variable);

The error:
Type mismatch for parameter 'variable': expected Map, Node or Relationship but was List<T> (line 2, column 9 (offset: 38)) "SET q = $variable" ^

The writeJSON implementation that executes fine:

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            var twd = value as Variable;
            if (twd == null)
                return;

            JToken t = JToken.FromObject(value);
            if (t.Type != JTokenType.Object)
            {
                t.WriteTo(writer);
            }
            else
            {
                var o = (JObject)t;

                Variable variable = (Variable)value;

                //Store original token in a temporary var
                var dictionary = variable.TestProp;
                //Remove original from the JObject
                o.Remove("TestProp");

                foreach (var item in dictionary)
                {
                    //Add a new 'InsString' property 'stringified'
                    o.Add("TF_" + item.Key, item.Value);
                }

                //Write away!
                o.WriteTo(writer);
            }
        }

The TestProp property on class Variable:

    public class Variable
    {
      [Neo4jIgnore]
        public Dictionary<string, int> TestProp { get; set; } = new Dictionary<string, int>()
        {
            { "5", 60 },
            { "1", 600 }
        };
   }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions