Giving back to the world

RaySearch Laboratories

When I quit my job at Aptitud in December 2016 I had no clue what I was going to do or where I was going to do it. I found myself in an endless world of opportunities and that kind of scared me a bit.

When I finally got my bearings back in early January 2017 (after a very relaxing Christmas vacation) I decided I would try my luck and post my CV at several job sites.

To my huge surprise It didn’t take more than a couple of days before I had whole January booked with interviews at interesting product companies in Stockholm area.

At first I kept a really open-minded approach to every opportunity and tried hard not to filter out any opportunity. But along the way some values came to stand out more than others:

  • My next workplace should be a product company and not a consultant company.
  • My next workplace should be on a journey, i.e. I wouldn’t want to come to a product company where everything is already done. Both organisational and technical.
  • My next workplace should make the world a little better. Looking back at my professional life, had I contributed to any important difference to the world? Hardly, going forward this would have to change!

A couple of companies checked all my boxes but only one made it to the finish line.

The 27th of March 2017 I’m very happy and honoured to join RaySearch Laboratories as a Software Developer. (http://www.raysearchlabs.com)

RaySearch Laboratories

See you around,

Hugo

Den där bloggposten om när Hugo sa upp sig från Aptitud

Tidigt igår imorse meddelade jag alla härliga kollegor på Aptitud att jag säger upp mig.

Tanken om uppsägning har jag burit med mig sen början på året men det har varit otroligt svårt att fullfölja eftersom kollegorna och kulturen på Aptitud är så fantastisk(a)!

Hur skulle jag kunna lämna något som jag själv har varit en del i att bygga upp och som blivit så bra?

Så varför sluta?

Jag har jobbat med mjukvaruutveckling sedan 1998 och som konsult sedan 2007; att jag vill göra något nytt är ett svar på frågan. 

Att ständigt utmana mig själv och att inte stanna upp och nöja mig är ett annat svar. 

Att magen säger att det är dags är ett tredje svar.

Så vad ska du göra?

Jag vet inte, kanske något helt annat såsom ambulanshelikopter pilot eller något mer bekant, såsom att starta ett produktbolag som bidrar till en bättre mer jämställd och hållbar värld.

Min sista dag på Aptitud blir någon gång i slutet på Mars och efter det vet jag som sagt inte vad som väntar – det känns spännande!

God Jul och Gott Nytt År!

Hugo

Nancy and the issue with Enums as strings

enum_as_string

Bakground

The other day I was trying to show an enum property as a string in an Nancy based API.

And I just couldn’t do it at first but later on I found a way. Just so I remember until next time I thought I’d write a step by step instruction.

The initial Code

I’ve created an Empty ASP.Net project and added Nancy.Hosting.Aspnet Nuget package with the following command:

Install-Package Nancy.Hosting.Aspnet

In this project I’ve created a very simple NancyModule that returns a list of a class called TeamMember. Each TeamMember has a Name and an Avatar property that is an enum.

public class Index : NancyModule
{
    public Index()
    {
        Get["/"] = _ =>
        {
            var teamMembers = new List()
            {
                new TeamMember { Name = "Marcus", Avatar = StarWarsCharacter.Luke  },
                new TeamMember { Name = "Hugo", Avatar = StarWarsCharacter.Chewbacca }
            };
 
            return Response.AsJson(teamMembers);
        };
    }
}

Above shows my very basic NancyModule. And the code below shows the TeamMember class and the enum called StarWarsCharacter.

public class TeamMember
{
    public string Name { get; set; }
    public StarWarsCharacter Avatar { get; set; }
}
public enum StarWarsCharacter
{
    Leia,
    Luke,
    Chewbacca
}

Above the enum used in my example. This generates the output as shown below.

 
[ 
 { 
   name: "Marcus", 
   avatar: 2 
 }, 
 { 
   name: "Hugo", 
   avatar: 6 
 } 
] 

As you can see the enum is represented by an integer in the json-output.

First solution

If you do a google search for Enum as string then you probably will find people mentioning that you need to decorate your enum properties with the [JsonConverter(typeof(StringEnumConverter))] attribute:

    [JsonConverter(typeof(StringEnumConverter))]
    public StarWarsCharacter Avatar { get; set; }

So now I can’t build because I’m missing some references. I Install the Newtonsoft.Json Nuget package with this command:
Install-Package Newtonsoft.Json

and I hit the rebuild.

Still the json-output is representing the enum as an integer.

Why? I still haven’t figured out that part yet but if you install the Nuget package called Nancy.Serialization.JsonNet with this command:

Install-Package Nancy.Serialization.JsonNet

Shabaam, presto!

[
 {
  Name: "Marcus",
  Avatar: "Luke"
 },
 {
  Name: "Hugo",
  Avatar: "Chewbacca"
 }
]

Next challange

This is awesome! But wait a minute, now my json properties aren’t formatted as I like. The properties start with a capital letter, not like I want.

Solution continued

Don’t panic Hugo, you’ll only need to add a CustomSerializer and a Bootstraper and then it will be alright.

Below is the CustomSerializer class.

public class CustomSerializer : JsonSerializer
{
    public CustomSerializer()
    {
        ContractResolver = new CamelCasePropertyNamesContractResolver();
    }
}

And below you can see the BootStraper.

public class BootStraper : DefaultNancyBootstrapper
{
    protected override void ConfigureApplicationContainer(TinyIoCContainer container)
    {
        base.ConfigureApplicationContainer(container);
        container.Register(typeof(JsonSerializer), typeof(CustomSerializer));
    }
}

The final solution

For a final touch you can remove the attribute JsonConverter all together and configure everything in the CustomSerializer by adding a StringEnumConverter to the Converters.

public class CustomSerializer : JsonSerializer
{
    public CustomSerializer()
    {
        ContractResolver = new CamelCasePropertyNamesContractResolver();
        Converters.Add(new StringEnumConverter
        {
            CamelCaseText = false
        });
    }
}

And lastly remove the attribute from the TeamMember class as all enums will be converted to strings as defined in the BootStraper above.

public class TeamMember
{
    public string Name { get; set; }
    public StarWarsCharacter Avatar { get; set; }
}

Conclusion

It’s always easy when you know how to solve issues, so here goes the perfect recipe for solving the enum as string issue on Nancy.

The code is as always here. You can find the different solutions as different branches.

Cheers,

Hugo