I am facing a problem while mapping a Gremlin result set to a class in C#. I am trying to get vertices along with properties. Here is the code:
public IList<T> All<T>() where T : class, new()
{
Type type = typeof(T);
string query = "g.V().hasLabel('" + type.Name.ToString().ToLower() + "').valueMap(true)";
var resultSet = _gremlinClient.SubmitAsync<dynamic>(query).Result;
List<T> list = JsonConvert.DeserializeObject<List<T>>(JsonConvert.SerializeObject(resultSet));
return list;
}
And here is the User entity, which I am passing to this generic method.
public class User
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}
When I run the code, it throws an error while deserializing.
'Unexpected character encountered while parsing value: [. Path '[0].FirstName', line 1, position 37.'
When I inspected it, I found that the JSON string has property values with square brackets, like this:
[
{
"id": 0,
"label": "user",
"FirstName": [ "Azhar" ],
"LastName": [ "Rahi" ]
},
{
"id": 3,
"label": "user",
"FirstName": [ "John" ],
"LastName": [ "Doe" ]
}
]
You can see some properties have square brackets like "FirstName":["Azhar"]
. I have tested it without square brackets and it is working fine. So the reason for the error is because the strings are represented as arrays in the JSON.
In the above JSON string, id
and label
are auto-generated properties by Gremlin.Net. The rest are actually User
object properties . I don't know why Gremlin adds brackets while adding properties to Vertex
and if there is any possibility to avoid it.
Please suggest any solution, either in Gremlin.Net or by somehow changing the mapping of the JSON to the class.