Skip to main content

Converting to JSON and accessing metadata

Entities passed to a transformer can be converted to JSON using the AsDocument method; there is also a possibility to access metadata for a specified object using MetadataFor method.

AsDocument - converting to JSON

public class Products_AsDocument : AbstractTransformerCreationTask<Product>
{
public class Result
{
public RavenJObject RawDocument { get; set; }
}

public Products_AsDocument()
{
TransformResults = products => from product in products
select new
{
RawDocument = AsDocument(product)
};
}
}
IList<Products_AsDocument.Result> results = session
.Query<Product>()
.TransformWith<Products_AsDocument, Products_AsDocument.Result>()
.ToList();

MetadataFor - accessing metadata

public class Products_WithMetadata : AbstractTransformerCreationTask<Product>
{
public class Result
{
public Product Product { get; set; }

public string EntityName { get; set; }

public string ClrType { get; set; }
}

public Products_WithMetadata()
{
TransformResults = products => from product in products
let metadata = MetadataFor(product)
select new
{
Product = product,
EntityName = metadata.Value<string>("Raven-Entity-Name"),
ClrType = metadata.Value<string>("Raven-Clr-Type")
};
}
}
IList<Products_WithMetadata.Result> results = session
.Query<Product>()
.TransformWith<Products_WithMetadata, Products_WithMetadata.Result>()
.ToList();