I'm using a neo4jclient and one of my data classes has a Collection<string>
property.
On save, I got the following error:
"Unable to set property 'SomeArrayPropertyName' to an empty array, because, since there are no values of any type in it, and no pre-existing collection to infer type from, it is not possible to determine what type of array to store."
Is there a way to save an empty array without changing it to Null ?
Edit:
Example:
public class BaseData
{
public Guid Id { get; set; }
...
...
public Collection<string> Subjects { get; set; }
public BaseData()
{
Subjects = new Collection<string>();
}
//This method lets Json.Net know whether to serialize 'Subjects'
public virtual bool ShouldSerializeSubjects()
{
return Subjects != null && Subjects.Any();
}
}
public class ParentData : BaseData
{
}
public class ChildData : ParentData
{
public String Name { get; set; }
public String Description { get; set; }
...
...
}
Neo4jclient uses Json.net to parse the data classes, so you can add a method to your Data class which Json.net will look for, in the format ShouldSerializePROPERTYNAME
(see the example below).
public class Actor
{
public string Name { get;set;}
public Collection<string> Alternatives { get;set; }
//This method lets Json.Net know whether to serialize 'Alternatives'
public bool ShouldSerializeAlternatives()
{
return Alternatives != null && Alternatives.Any();
}
}
This will serialize new Actor{Name = "Jeff", Alternatives=new Collection<string>()}
as: {"Name":"Jeff"}
- no Alternatives
at all.
Obviously the downside here is that your class when deserialised will have a null collection - which you might be ok with, or not - if not - you could just stick an initializer in the constructor:
public Actor() { Alternatives = new Collection<string>(); }
which will be called when Json.Net deserializes the object.
There is another way using an IContractResolver but if you have access to the data classes, the above way is easiest.
There is no way to get around the empty collection other than not serialize it, as neo4j won't allow you to send the data in.
--EDIT--
public class BaseData
{
public Guid Id { get; set; }
public Collection<string> Subjects { get; set; }
public bool ShouldSerializeSubjects()
{
return Subjects != null && Subjects.Any();
}
public BaseData()
{
Subjects = new Collection<string>();
}
}
public class ParentData :BaseData {}
public class ChildData : ParentData
{
public string Name { get; set; }
public string Description { get; set; }
}
class Program
{
static void Main()
{
var client = new GraphClient(new Uri("http://localhost.:7474/db/data/"));
client.Connect();
var cd = new ChildData { Name = "c1", Description = "des", Id = Guid.NewGuid() };
var nodeRef = client.Create(cd);
var cdRetrieved = client.Get<ChildData>(nodeRef);
Console.WriteLine("Name: {0}, Description: {1}, Id: {2}, Subjects: {3}", cd.Name, cd.Description, cd.Id, cd.Subjects.Count);
Console.WriteLine("Name: {0}, Description: {1}, Id: {2}, Subjects: {3}", cdRetrieved.Data.Name, cdRetrieved.Data.Description, cdRetrieved.Data.Id, cdRetrieved.Data.Subjects.Count);
var json = JsonConvert.SerializeObject(cd);
Console.WriteLine(json);
}
Gives the following output:
Name: c1, Description: des, Id: ff21b0fe-0a51-4269-a1d4-e148c905a6d7, Subjects: 0
Name: c1, Description: des, Id: ff21b0fe-0a51-4269-a1d4-e148c905a6d7, Subjects: 0
{"Name":"c1","Description":"des","Id":"ff21b0fe-0a51-4269-a1d4-e148c905a6d7"}
The last line is the actual JSON output by Json.Net, with no 'subjects' serialized.