Neo4jClient now supports JsonProperty for Sending Cypher
Originally posted on: http://geekswithblogs.net/cskardon/archive/2015/09/17/neo4jclient-now-supports-jsonproperty-for-sending-cypher.aspx
So, you want a demo graph, and you fire up Neo4j, go to the admin (http://localhost:7474/) and then run
:play movies
This gets you a graph to play around with, and so you head into Visual Studio (or maybe even LinqPad), and you have an urge now to get all the movies in the DB released after the year 2000. A compulsion if you will. So you create your class to get the movies:
class Movie
{
public string Title { get; set; }
public int Released { get; set; }
}
And run the query you’ve been dreaming of since 5 minutes ago:
var movies = graphClient.Cypher
.Match("(m:Movie)")
.Where((Movie m) => m.Released > 2000)
.Return(m => m.As<Movie>())
.Results;
No results? A moment of realisation dawns – j. There’s a ‘j’. At the end of Neo4, they put a ‘j’ – does this, does this mean LOWER CASE PROPERTIES!!!??? You try it just to see – lowercasing your Movie properties – it works. Ah man.
Fear not traveller! Json.Net to the rescue (and Neo4jClient 1.1.0.9 or greater)!
We must adorn our classes with Json Properties, like so:
class Movie
{
[JsonProperty("title")]
public string Title { get; set; }
[JsonProperty("released")]
public int Released { get; set; }
}
Now, we can run our dream query and discover all the movies released post 2000.
Obviously you can use / abuse the JsonProperty attribute as you wish – fancy storing things in the database with a property called ‘Foo’ but retrieving it as ‘Bar’, no problem (except with you)
[JsonProperty("Foo")]
public string Bar { get; set; }
One thing you should note fellow .Net-iens – Neo4j has no problem with UpperCamelCase properties, this is mainly for use if you’re up against the demo code, or indeed you want to change things in your code, but leave the DB ‘as-is’
Anyhews – that’s all in Nuget now, so that’s good.