Gremlinq / ExRam.Gremlinq

A .NET object-graph-mapper for Apache TinkerPop™ Gremlin enabled databases.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Serialize/Deserialize nested objects as JSON string to a vertex string property?

ruthoferroman opened this issue · comments

The classes that I want to store as a Vertex have some simple nested objects. My Idea would be to store them to a Vertex string property by serializing them to a json string - and back.

Is there a way to achive this?

Yes just

Product product = new Product();

product.Name = "Apple";
product.ExpiryDate = new DateTime(2008, 12, 28);
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" };

string output = JsonConvert.SerializeObject(product);

https://www.newtonsoft.com/json/help/html/SerializingJSON.htm

I think I need to describe my issue a little more detailed.
Let's say I have the following class Person with property Address of type PersonAddress

public class PersonsAddress 
{
        public string Country { get; set; }
        ...
}    
public class Person: Vertex
{
        public int Age { get; set; }
        public string Name { get; set; }
        ...
        public PersonsAddress Address { get; set; } // <<----
 }

I'm going to add it to my gremlindb by:

  var marko = await _g
        .AddV(new Person { Name = "Marko", Age = 29, Address = new { Country = "Austria" } })
        .FirstAsync();

Now here is the thing. This won't work since the gremlindb only supports let's say standard property types like 'string', etc.. So i would like to tell ExRam to Json Serialize the 'Address' property to a 'string' property in gremlindb

Aha, yup, you might have to build an Extension method to do it.? That operates on a class? and then looks for any other Class properties and convert them into the string for it?

@DocGreenRob I'm not sure if I got you right. Do you mean to build my own Extension Method for "AddV"?

Is there a chance to tell ExRam to Ignore some properties when adding the vertex to the db (something like a JsonIgnore attribute). I know there is the .ConfigureElement<VertexType>(conf => conf.IgnoreAlways(x => x.PartitionKey) option, but since i have a whole bunch of Types derived from VertexType this won't be a generic way.

One idea would be if I could do something like:

public class Person: Vertex
{
        public int Age { get; set; }
        public string Name { get; set; }
        ...
        **[ExramIgnore]**
        public PersonsAddress Address { get; set; } // <<----
        **public string AddressString { get { JsonConvert.SerializeObject(...) .....}**
 }

@ruthoferroman sorry for the delayed response (didn't see your response).

  1. Yes, I think you should add an extension.
  2. Yeah, you have to do something here. In my case I don't express the relationships using polymorphism, I express the relationships/hierarchy via the edges. So in your Vertex type classes you should flatten it out (avoid the object properties). Maybe you can express the relationships/hierarchy in your DTOs. In my case I'm building a WebApi so I have DTOs that I accept (as JSON payloads) and they express - in many cases - many Vertexes and have the relationships as you have in your Person class, but my DataAccess.Entities are not as such. At some point in the process I parse the DTO and extract the individual Vertexes and create them. Then I relate them using Edges. This way I'm not trying to tell ExRam how to interpret properties that are classes.

@DocGreenRob no worries ;) I'm happy to find someone to help me with some ideas.

According to your answer I'm now ready to take the path to go for Model, Domain and DTO Objects and use edges to express the hirarchy. I wanted to avoid this since - yeah right I'm a lazy boy :D - but it is the right approach and a clear architecture as well. So next step: T4 Templates for the rescue - hopefully the will support my laziness

thanks for all your help!