C#, Code, Tips, Visual Studio

Visual Studio – Paste Special – JSON as Classes

This has been there since 2013 I believe, but I’m just now discovering it….wish I would have found it sooner as it’s an awesome feature.

Take a JSON (or XML as there is a Paste XML as Classes as well) like the following and copy it into the clipboard:

{
"People" :[{
    "First Name" : "Jason",
    "Last Name" : "Lautzenheiser", 
    "Address" : {
        "Street" : "1122 Main",
        "City" : "BigTown",
        "State" : "OH",
        "Zip" : 66554
    },
    "Phones" : [{
        "PhoneType" : "Home",
        "Number" : "330-555-7777"
      },
      {
        "PhoneType" : "Work",
        "Number" : "330-222-1111"
      }
    ]
  }]
}

Then under the Edit->Paste Special menu, there is an option for Paste JSON as Classes which will spit out the following code:

public class Rootobject
{
 public Person[] People { get; set; }
}

public class Person
{
 public string FirstName { get; set; }
 public string LastName { get; set; }
 public Address Address { get; set; }
 public Phone[] Phones { get; set; }
}

public class Address
{
 public string Street { get; set; }
 public string City { get; set; }
 public string State { get; set; }
 public int Zip { get; set; }
}

What a beautiful thing that is. I recently had to import and manipulate some JSON data for a report which was much more complex than this. Within minutes I had my object model setup and the JSON files being read in.

Small feature, but what a time saver.

.NET, C#, Tips

IEnumerable to Comma Delimited String

A few years ago I blogged about how to take a delimited string and convert it to a IEnumerable.  Today we ran across the opposite of this in that we wanted to take a list and convert it to a delimited string.   This can come in handy for a number of situations so it’s worth taking a look.

Given:

List<int> values = new List<int> {10, 20, 25};

In older versions of .NET (prior to 4.0 I believe) the easiest way was to do something like the following:

string newString = string.Join(",", values.ToArray());

However, starting in 4.0, the Join has some additional overloads that make this just crazy simple:

string newString = string.Join(",", values);

If you have a list of strings, there is still some benefit to converting this to an array. There is another overload of Join that allows you to be more selective in what you are converting.

Given a list of strings like the following:

List<string> strings = new List<string> {"a", "b", "c", "d", "e", "f"};

We can look at the last overload of; String.Join(delimiter, string[], startIndex, count)

This gives us a delimited string, starting at the element in the array at startIndex and going for the number of count.

string delimited = string.Join(",", strings.ToArray(), 2, 2);

will return “c,d”

I love how the framework provides these methods, often with multiple overloads to give us a lot of power with very little code. Enjoy!

.NET, C#, Code, PetaPoco

PetaPoco Needs a Parameterless Constructor

I’ve been using PetaPoco for a few years as my micro-orm of choice.  It’s lightweight, simple to use, yet powerful enough for most of my needs.  Let me share one thing that I tend to forget and it seems to trip me up all the time.

In short, PetaPoco requires a parameterless constructor on the classes it’s trying to integrate with.

  public class Customer 
  {
    public Customer() 
    {
    }
  }

Or if there is no other constructor defined, the compiler will create that default constructor for you essentially and you don’t have to explicitly code it. This is where I usually run into trouble. I’ll depend on the compiler to give me the default constructor and code as follows.

  public class Customer 
  {
    public string Name {get;set;}
    public string Address {get;set;}
    // more properties and methods
  }

With this PetaPoco works flawlessly. No problems here. Now let’s say I create a constructor for one reason or another. For example, we want to pass the customer’s name in the constructor.

  public class Customer 
  {
    public string Name {get;set;}
    public string Address {get;set;}

    public Customer(string _name)
    {
      Name = _name;
    }
    // more properties and methods
  }

Now if we try to read in a list of customers with PetaPoco, we suddenly get a Null argument exception and it comes from deep inside PetaPoco where it’s not at first obvious what is going on. In fact, every time I get this error, I forget that I’ve seen it before and scratch my head for a few minutes…..thus this post…hopefully I’ll remember now.

Anyway, PetaPoco needs that parameterless constructor, which if we have no other constructor defined, will happily get created by the compiler. However, if we do provide a constructor like in the last example above, the compiler does not create that default constructor and we have to define it explicitly.

  public class Customer 
  {
    public string Name {get;set;}
    public string Address {get;set;}

    public Customer() 
    {
    }

    public Customer(string _name)
    {
      Name = _name;
    }
    // more properties and methods
  }

Once we do that, PetaPoco works beautifully once more.