Introduction

Yesterday I was adding a pdf feature to my Nancy service. And I was outputting my objects to the pdf like this.

C#
1
2
3
4
foreach (var prop in model.GetType().GetProperties())
{
  oDoc.Add(new Paragraph(prop.Name + ":" + prop.GetValue(model).ToString()));    
}
foreach (var prop in model.GetType().GetProperties())
{
  oDoc.Add(new Paragraph(prop.Name + ":" + prop.GetValue(model).ToString()));    
}

But we all know this is far from finished and will only work in the most simple of cases, once you add complex types to your type the results will be less than optimal.

Of course you are not alone in this world and other people have had to do this to. And Demis Bellot even made a nuget package out of his solution. So here is Servicestack.Text and the Dump extension method and friends.

Dump

So go ahead and look for servicestack.text on nuget and add it to your project.

First of all I would like to change my method above to use the Dump() method.

And so my method now looks like this.

protected virtual Action<Stream> GetPdfContents(TModel model)
        {
            return stream =>
                {
                    var oDoc = new Document(PageSize.A4);
                    PdfWriter.GetInstance(oDoc, stream);
                    oDoc.Open();
                    oDoc.Add(new Paragraph(model.Dump()));    
                    oDoc.Close();
                };
        }

So you just need you object and call the Dump() extension method on it and the output will look like this.

{
propertyname: value,
propertyname2: value
}

Nicely formatted JSV.

Yep that’s it.

The output now looks like this.

Or I could output a list of objects and that would look like this.

And that is very convenient. This is the the same as doing SerializeAndFormat().

PrintDump

And do you remember this code?

C#
1
2
3
4
5
6
7
8
9
10
11
var trees = http.Get("http://localhost:65367/trees");
            foreach (var t in trees.DynamicBody.Trees)
            {
                Console.WriteLine(t.Id);
                Console.WriteLine(t.Genus);
            }
            var result = http.Get("http://localhost:65367/trees/1");
            var tree = result.DynamicBody;
            Console.WriteLine(tree.Id);
            Console.WriteLine(tree.Genus);
            
var trees = http.Get("http://localhost:65367/trees");
            foreach (var t in trees.DynamicBody.Trees)
            {
                Console.WriteLine(t.Id);
                Console.WriteLine(t.Genus);
            }
            var result = http.Get("http://localhost:65367/trees/1");
            var tree = result.DynamicBody;
            Console.WriteLine(tree.Id);
            Console.WriteLine(tree.Genus);
            

Lots of Console.Writelines and not very robust.

C#
1
2
3
4
var trees = http.Get("http://localhost:65367/trees");
            trees.StaticBody<Object>().PrintDump();
            var result = http.Get("http://localhost:65367/trees/1");
            result.StaticBody<Object>().PrintDump();
var trees = http.Get("http://localhost:65367/trees");
            trees.StaticBody<Object>().PrintDump();
            var result = http.Get("http://localhost:65367/trees/1");
            result.StaticBody<Object>().PrintDump();

PrintDup won’t work on dynamic objects so I switched to a staticobject, but easyhttp doesn’t mind.

And I get this as the result.

{
Trees:
[
{
Id: 1,
Genus: Fagus
},
{
Id: 2,
Genus: Quercus
},
{
Id: 3,
Genus: Betula
},
{
Id: 4,
Genus: Fagus
},
{
Id: 5,
Genus: Quercus
},
{
Id: 6,
Genus: Betula
},
{
Id: 7,
Genus: Fagus
},
{
Id: 8,
Genus: Quercus
},
{
Id: 9,
Genus: Betula
},
{
Id: 10,
Genus: Fagus
},
{
Id: 11,
Genus: Quercus
},
{
Id: 12,
Genus: Betula
},
{
Id: 13,
Genus: Fagus
},
{
Id: 14,
Genus: Quercus
},
{
Id: 15,
Genus: Betula
}
],
NumberOfTrees: 15
}
{
Id: 1,
Genus: Fagus
}

Genius.

SerializeToString

And if you on’t like the formatting then you can use SerializeToString().

C#
1
2
3
4
var trees = http.Get("http://localhost:65367/trees");
Console.WriteLine(trees.StaticBody<Object>().SerializeToString());
var result = http.Get("http://localhost:65367/trees/1");
Console.WriteLine(result.StaticBody<Object>().SerializeToString());
var trees = http.Get("http://localhost:65367/trees");
Console.WriteLine(trees.StaticBody<Object>().SerializeToString());
var result = http.Get("http://localhost:65367/trees/1");
Console.WriteLine(result.StaticBody<Object>().SerializeToString());

Which will have this kind of output.

{“Trees”:[{"Id":1,"Genus":"Fagus"},{"Id":2,"Genus":"Quercus"},{"Id":3,"Genus":"B
etula"},{"Id":4,"Genus":"Fagus"},{"Id":5,"Genus":"Quercus"},{"Id":6,"Genus":"Bet
ula"},{"Id":7,"Genus":"Fagus"},{"Id":8,"Genus":"Quercus"},{"Id":9,"Genus":"Betul
a"},{"Id":10,"Genus":"Fagus"},{"Id":11,"Genus":"Quercus"},{"Id":12,"Genus":"Betu
la"},{"Id":13,"Genus":"Fagus"},{"Id":14,"Genus":"Quercus"},{"Id":15,"Genus":"Bet
ula"}],”NumberOfTrees”:15}
{“Id”:1,”Genus”:”Fagus”}

Conclusion

These are methods that come in very handy when you do a spike or they would even come in handy for debugging, sometimes even in production.

And there are even more extension methods in that little package. You can find them all on their github page. Just look at ToJson and ToXml.