I need to recover JSON stored in RavenDb database knowing its Id
. What's tricky here, I need to get it before it is deserialized into an actual object. Reason for that is, I need it in exact same form it was first stored in, regardless what happens to the CLR class (it can drastically change or even be removed), because it is an audit of previous state (it's only going to be displayed at this point, I won't use it for anything else).
If I go with
using (var session = Store.OpenSession())
{
return JsonConvert.SerializeObject(session.Load<object>(id));
}
I get JSON reflecting current state of underlying type, probably because it's stored in @metadata.Raven-Clr-Type
. So I don't see removed properties, and I do see empty properties that didn't exist when it was first stored.
I wanted to use DocumentStore.DatabaseCommands.Get()
to get RavenJObject
, but it is already removed in 4.1 in favor of Advanced.DocumentQuery
and I couldn't find a way to do what I want using it.
I've also tried using own AbstractIndexCreationTask
like this:
class Object_AsJson : AbstractIndexCreationTask<JsonObject>
{
public Configuration_AsJson()
{
Map = configuration => configuration.Select(x => AsJson(x).Select(y => y.Value));
}
}
But session.Load
doesn't accept TIndexCreator
, and in session.Query
I can't get Id
because I don't have any properties of an object to make query from.
Example:
If I have this class:
public class Person
{
string FullName { get; set; }
int Age { get; set; }
}
And I store a new document new Person { FullName = "John Samson", Age = 42 }
, under Id
person/A-1
, yet half year later I decide to modify this class to:
public class Person
{
string FirstName { get; set; }
string LastName { get; set; }
int Age { get; set; }
}
Now I want to see how JSON of person/A-1
looks like, but an attempt to load it maps it to current state of Person
, so I will get:
{
"FirstName": null,
"LastName": null,
"Age": 42
}
And I want to get
{
"FullName": "John Samson",
"Age": 42
}
I can see my desired result in RavenDb Studio GUI, but I need to get it in my code to display it in my application.
Does anyone have any idea how to handle that? Just a hint in right direction would be appreciated.