Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Monday, November 4, 2019

Creating JSON in C# With Dynamic

I'm excited to share a new (to me) way of creating JSON on-the-fly.  It uses the dynamic type in C#, which I knew about, but hadn't used too much.  Now I see how powerful it really is.

I ran across a problem where I had to build a JSON string to send to a web service as a parameter.  It was short but nested several layers deep.  I also needed it to be highly configurable.  I could build it with a string, or better yet, a StringBuffer.  Or I could build an object, then serialize it to JSON.  The former seems fragile, the later feels like overkill (And, there is nothing worse than trying to decode what another programmer has done with a thousand little classes you have to hunt down.)

Enter the dynamic type.  I knew it was very late-binding, thus avoiding compile-time type checking.  It gets its type at run-time.  So, it infers the type when the program runs and is able to do this:

dynamic nVal = 23.4;
dynamic sVal = "Hello";
Console.WriteLine(nVal.GetType().ToString());   // System.Double
Console.WriteLine(sVal.GetType().ToString());   // System.String

However, a little talked about feature is that it is an object that can be a container too.  And it can contain different types including arrays and objects.  This sets us up to be able to nest JSON very nicely.  Check out this example:

dynamic message = new JObject();
product.to = "UserToken";
product.priority = 5;
product.notification = new JObject();
product.notification.message = "You have a new order.";
product.notification.sound = "default";

Console.WriteLine(message.ToString());

// Output JSON
{
    "to": "UserToken",
    "priority": 5,
    "notification": {
        "message": "You have a new order.",
        "sound": "default"
    }
}

Hopefully, this will help you create simpler JSON data too.

Friday, June 22, 2018

Regenerating XSD Dataset After Change

I find myself constantly having to maintain projects built with XSD datasets.  The thing I always *strongly disliked* about XSD's is their tendency to be fragile.  Whenever I make a change everything breaks and I'd end up fooling around for a 1/2 day getting it to work again.

Well, I think I've finally figured out how to make changes to an XSD, then regenerate the schema correctly.  Here are the steps that are working well for me now:

Open the XSD and make whatever changes you need.  In this sample, I'm just adding a new column to a SQL Server stored procedure.  Once I've made the change in the stored procedure, it's time to synchronize the XSD.

Go to the XSD view in Visual Studio, right-click on the object you need to change and click "Configure."


Go through all the steps of configuring that item...


You will notice that there is a file called "...Designer.cs".  This is the file auto-generated by the DataSetGenerator tool (or at least it's supposed to be auto-generated).


If this file isn't being changed, you can force it to make a new one.  First, delete the Designer.cs file (Right click => Delete).

Then right-click on the .XSD file and select "Run Custom Tool".  This will regenerate the Designer.cs file and you should be off and running.



What?  You don't have a menu item that says "Run Custom Tool"?  Then you need to add it to the XSD file type.  You do that by left-clicking on the XSD file, then go to the properties.



Under custom tool, make sure it says MSDataSetGenerator.  If it doesn't, add it.

Happy coding!