Archives

Archives / 2012
  • Monitoring Windows Azure with Foglight

    I do a lot of Azure development. While it is a great platform for setting up new environments, scaling instances and simple deployments; monitoring applications in Azure is difficult. It is a remote environment and because of the way Azure abstracts hosting for you there is no option to install your own software on the server.

    Foglight for Azure Apps is a hosted service that sets up gathering information from Windows Azure for you. In this blog post I'm going to try out Foglight with a demo application I've put together for simulating a broken website (demo source code available at the end of the blog post).

    jsonformat

    Getting Started

    Setting up Foglight for Azure Apps is really simple. Foglight has two configuration methods for adding an Azure deployment: manual configuration where you manually add details about each deployment you want to monitor, and automatic discovery where Foglight retrieves that information from Azure for you. I'm going to step through using the automatic discovery wizard.

    foglightconfigure

    With Foglight automatic discovery the only information you need is your role’s Azure Subscription ID. Subscription IDs can be found in the Windows Azure Management Portal by browsing to Settings -> Management Certificates.

    foglightsubscriptionid

    The next step after copy/pasting your Subscription ID is to add a management certificate from Foglight to Azure. The download link for the certificate is in the wizard and it is uploaded in the same place where you got your Subscription ID.

    foglightcertificate

    Finally select the deployment you want to monitor and you’re finished.

    foglightconfigurationcomplete

    Monitoring

    foglightdashboard

    The core of Foglight for Azure Apps are the monitors it provides to measure the health of your application. The first page you’ll come upon after setup is the dashboard which provides a nice summary of the state of your application. From this screen or from the menu you can drill into more detail about each monitor.

    availability

    The availability page shows you whether your application is accessible from various locations around the world – useful for times when a user or users from a country report they can’t access your application. In the screenshot above I've activated server errors on my demo website which you can see showing up in orange in the availability graphs.

    health

    The health page lets you view health details about individual rolls in your application. Here you can see details like CPU, memory and disk usage, and HTTP traffic and bandwidth being used by each role. One nice feature here is Foglight aggregates everything together and provides an indication of whether the application is healthy or not. Seeing the health of an application in a graph over time lets you match user reported errors with past server problems. In the screenshot above I've active high CPU and memory usage in my demo application.

    services

    The services page shows the status of the Windows Azure services your application depends on (e.g. compute, databases, storage) in your application's region and the status of worldwide services (e.g. management portal, CDN). This is a useful tool when debugging a broken application to double check whether the issue is yours or is being caused by a problem in Azure infrastructure.

    topurls

    The top URLs page shows what URLs in an application are creating problems. URLs can either be the slowest pages in your application or the pages with the highest number of 404 and server errors.

    Configuring Health and Alerts

    An awesome feature of Foglight is the control you have over configuring health thresholds and sending alerts.

    configurehealth

    Once you're happy with the health thresholds (the screenshot above shows the default) you can then configure at what health level alerts should be emailed and who they should be emailed to.

    Health alerts are one of the most important features when monitoring, letting you know about problems as soon as they happen rather than waiting until someone look at Foglight or hear about errors from your users.

    One thing I’ve done in the past with errors is to send an SMS message to my phone. Foglight doesn’t have built in SMS support but it is simple to set up using IFTTT.

    ifttt

    Wrapping Up

    I found Foglight's monitoring easy to understand and fast to update. Really impressive is how simple Foglight is to setup, taking just a couple of minutes and requiring no changes to your application. Finally the Foglight’s health alerts will ensure you know about critical issues as they happen.

    If you're deploying applications to Azure and it is important they are rock solid then I recommend you check Foglight for Azure Apps out.

     

    Click here to download the Windows Azure JsonFormat application source code

     

    Disclosure of Material Connection: I received one or more of the products or services mentioned above for free in the hope that I would mention it on my blog. Regardless, I only recommend products or services I use personally and believe my readers will enjoy. I am disclosing this in accordance with the Federal Trade Commission’s 16 CFR, Part 255: Guides Concerning the Use of Endorsements and Testimonials in Advertising.

  • Json.NET 4.5 Release 11 - Serialization Tracing

    Serialization Tracing

    The major new feature this release is serialization tracing. Using the ITraceWriter interface you can log and debug what is happening inside the Json.NET serializer when serializing and deserializing JSON.

    Staff staff = new Staff();
    staff.Name = "Arnie Admin";
    staff.Roles = new List<string> { "Administrator" };
    staff.StartDate = DateTime.Now;
     
    ITraceWriter traceWriter = new MemoryTraceWriter();
     
    JsonConvert.SerializeObject(
      staff,
      new JsonSerializerSettings { TraceWriter = traceWriter, Converters = { new JavaScriptDateTimeConverter() } });
     
    Console.WriteLine(traceWriter);
    // 2012-11-11T12:08:42.761 Info Started serializing Newtonsoft.Json.Tests.Serialization.Staff. Path ''.
    // 2012-11-11T12:08:42.785 Info Started serializing System.DateTime with converter Newtonsoft.Json.Converters.JavaScriptDateTimeConverter. Path 'StartDate'.
    // 2012-11-11T12:08:42.791 Info Finished serializing System.DateTime with converter Newtonsoft.Json.Converters.JavaScriptDateTimeConverter. Path 'StartDate'.
    // 2012-11-11T12:08:42.797 Info Started serializing System.Collections.Generic.List`1[System.String]. Path 'Roles'.
    // 2012-11-11T12:08:42.798 Info Finished serializing System.Collections.Generic.List`1[System.String]. Path 'Roles'.
    // 2012-11-11T12:08:42.799 Info Finished serializing Newtonsoft.Json.Tests.Serialization.Staff. Path ''.

    Json.NET has two implementations of ITraceWriter: MemoryTraceWriter which keeps messages in memory for simple debugging like the example above, and DiagnosticsTraceWriter which writes messages to any System.Diagnostics.TraceListeners your application is using.

    To write messages using your existing logging framework implement a custom version of ITraceWriter.

    Read more about trace writing here: Debugging with Serialization Tracing

    JSON String Escaping

    By default Json.NET only escapes control characters like new line when serializing text. New in this release is the StringEscapeHandling property on JsonTextWriter. Using StringEscapeHandling you can choose to escape HTML characters (<, >, &, ', ") or escape all non-ASCII characters.

    JToken.ToObject Performance

    The LINQ to JSON JToken class has a ToObject method on it for converting the JSON token to a .NET type. In previous versions of Json.NET this method always used the JsonSerializer behind the scenes to do the conversion which was unnecessary for converting simple types like strings, numbers, booleans, dates, etc.

    Json.NET 4.5 Release 11 now checks whether the object being converted to is a simple type and if so it skips using the JsonSerializer. The end result is ToObject is now 400% faster for most types.

    Changes

    Here is a complete list of what has changed since Json.NET 4.5 Release 10.

    • New feature - Added ITraceWriter, MemoryTraceWriter, DiagnosticsTraceWriter
    • New feature - Added StringEscapeHandling with options to escape HTML and non-ASCII characters
    • New feature - Added non-generic JToken.ToObject methods
    • New feature - Deserialize ISet<T> properties as HashSet<T>
    • New feature - Added implicit conversions for Uri, TimeSpan, Guid
    • New feature - Missing byte, char, Guid, TimeSpan and Uri explicit conversion operators added to JToken
    • New feature - Special case so Version type is correctly deserialized
    • Change - Silverlight and Windows Phone assemblies in NuGet are strong named again
    • Change - Improved CamelCasePropertyNamesContractResolver camel casing property names
    • Change – Explicit JValue conversions are more flexible when converting values
    • Fix - Fixed QuoteChar not being used when writing DateTimes, TimeSpans, Uris and Guids
    • Fix - Fixed JValue constructors for Uri, TimeSpan, Guid assigning incorrect JTokenType
    • Fix - Fixed ReferenceLoopHandling not being used when serializing ISerializable and dynamic values
    • Fix - Fixed potential null reference error when getting attributes
    • Fix - Fixed .NET 2.0 build incorrectly referencing DateTimeOffset

    Links

    Json.NET CodePlex Project

    Json.NET 4.5 Release 11 Download – Json.NET source code and assemblies

  • XAML Applications on Windows 8 with DXTREME

    XAML Applications on Windows 8 with DXTREME

    The public release date of Windows 8 is rapidly approaching and with it comes a brand new way to build and sell Windows applications via the new Windows Store.

    I first used the new Windows Store platform earlier this year when I ported my open source Json.NET library to it, and since then I have also built a couple Windows applications on it using JavaScript. In this blog post I am trying out something new to me for the first time: Windows 8 development using .NET and XAML with DXTREME’s XAML controls.

    Installation and first impressions

    The first thing you’ll notice after downloading DXTREME is its installer. The design is simple and to the point, and it looks completely at home in the Windows 8 UI.

    Installer8

    After installation is complete you are presented with a dialog that serves as a hub to developers getting started with DXTREME. The dialog links to two Visual Studio solutions: a demo application that shows off the Windows 8 XAML controls that come with the library, and a solution application that shows the controls used together in a great looking professionally designed Windows 8 app.

    Launcher6

    As someone completely new to DXTREME and Windows 8 XAML development I found both of these applications and their source code to be great resources when building my first Windows Store XAML app. Especially useful was a feature of the demo application that lets you view the source code for a control while the demo is running.

    Getting Started

    I find the best way to learn something new is to make something with it. The Windows 8 application I’m going to step through making in this blog post is called /r/Imgur and is an app that integrates the popular social websites Reddit and Imgur. It displays images from those websites within a Windows application. (a link to the final source code will be at the end of the blog post)

    newproject6

    As part of its installation DXTREME adds new Windows 8 project types to Visual Studio 2012. I used the DXTREME Grid Application project which sets up a basic Windows 8 tile based UI for you using DXTREME’s new controls.

    Fetching Data from Imgur

    The application’s data and images are driven from Imgur, an Internet image hosting service. They provide a simple API for getting album information as JSON for their website.

    public static async Task<Album> LoadAlbum(string subReddit)
    {
        Album album = new Album();
        album.Title = subReddit;
     
        HttpClient httpClient = new HttpClient();
        string json = await httpClient.GetStringAsync("http://imgur.com" + subReddit + "/top.json");
     
        JObject o = JObject.Parse(json);
        foreach (JToken i in o["data"])
        {
            Image image = i.ToObject<Image>();
            image.Album = album;
            image.ImageUrl = string.Format("http://i.imgur.com/{0}.jpg", image.Hash);
     
            album.Items.Add(image);
        }
     
        return album;
    }

    The code above utilizes the Web API Client library and Json.NET. It simply gets the album data as JSON and deserializes it to an Album object. These albums and their images are what the UI databinds to.

    Styling the UI

    Two of XAML’s most powerful features are its support for the model-view-view-model pattern (MVVM) and design time view data, both of which have great support in DXTREME. Once setup design time view data lets you style your Windows 8 application from within Visual Studio 2012 or Expression Blend against pretend data. You no longer need to constantly restart your application to see what the final result will be because it is instantly visible in the XAML designer. It’s a great timesaver.

    blend8

    The screenshot above is /r/Imgur in Expression Blend after I have finished styling it. The controls are being databound to hardcoded view model of Albums that I have created so that the look of the final application is visible in the Blend and Visual Studio designers without having to call off to the Imgur API.

    Adding New Albums

    As well as displaying some pre-set albums in the app we want to give users the ability to add new albums. To do this we’ll add a Windows 8 appbar button that will launch a Windows 8 style dialog with fields for the user to enter the new album name. Fortunately DXTREME has a Windows 8 style dialog that is perfect for the task.

    <Page.Resources>
        <DataTemplatex:Key="AddAlbumTemplate">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinitionWidth="Auto" />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <TextBlockText="/r/"FontSize="18"VerticalAlignment="Center" />
                <TextBoxGrid.Column="1"Text="{Binding Path=Title, Mode=TwoWay}"BorderThickness="1"BorderBrush="#FF3D3D3D"Margin="5 0 0 0" />
            </Grid>
        </DataTemplate>
    </Page.Resources>
    <dxcore:Interaction.Behaviors>
        <dxcore:ServiceContainerBehaviorServicesClient="{Binding}">
            <dxcore:DialogServiceKey="addDialog"ContentTemplate="{StaticResource AddAlbumTemplate}"Buttons="OKCancel"DefaultDialogButton="OK"CancelDialogButton="Cancel"Title="Add Reddit Album"/>
        </dxcore:ServiceContainerBehavior>
    </dxcore:Interaction.Behaviors>

    Wiring up a DialogService to a DataTemplate using the code above lets you launch a dialog without having to worry about recreating the look and feel of a Windows 8 dialog; that’s all handled for you by the dialog control.

    Album Rating

    The final piece to the application is to add the album rating to its detail page. Here we’ll add a pie graph using DXTREME’s graphing library to display the ratio of user up votes to down votes for the album.

    chart5

    The look and behaviour of the pie graph is defined in XAML, allowing us to design it inside the Visual Studio XAML designer without running the application, and then the up and down votes for the album are databound the graph when the application executed at runtime.

    Wrapping Up

    I found the controls I used while making /r/Imgur well thought out and easy to use. One important feature I always look for in controls and libraries when using XAML is how well they work with the rest of the framework. Everything has great MVVM support and the controls included in the library provide a nice experience to work with the Visual Studio 2012 and Expression Blend designers.

    Click here to download the /r/Imgur application source code

     

     

    Disclosure of Material Connection: I received one or more of the products or services mentioned above for free in the hope that I would mention it on my blog. Regardless, I only recommend products or services I use personally and believe my readers will enjoy. I am disclosing this in accordance with the Federal Trade Commission’s 16 CFR, Part 255: Guides Concerning the Use of Endorsements and Testimonials in Advertising.

  • Json.NET 4.5 Release 10 – Portable Class Library on NuGet

    Json.NET's major new feature this release is the portable class library assembly is available over NuGet. This is made possible by NuGet 2.1 supporting portable class libraries. Read more about NuGet 2.1 here.

    Case insensitive JObject GetValue and TryGetValue

    To simplify getting property values without worrying about the property name's case a couple of helper methods have been added to JObject. GetValue and TryGetValue will first attempt to get a property value with the exact case after which it will ignore case and the first matching property will be returned.

    JObject o = JObject.Parse(@"{
      'name': 'Lower',
      'NAME': 'Upper'
    }");
     
    string exactMatch = (string)o.GetValue("NAME", StringComparison.OrdinalIgnoreCase);
    // Upper
     
    string ignoreCase = (string)o.GetValue("Name", StringComparison.OrdinalIgnoreCase);
    // Lower

    In this example the first call to GetValue with "NAME" exactly matches the second property. The second call to GetValue with "Name" doesn’t exactly match any of the properties so a case insensitive search is made and the first property is returned.

    Changes

    Here is a complete list of what has changed since Json.NET 4.5 Release 9.

    • New feature - Added Portable build to NuGet package
    • New feature - Added GetValue and TryGetValue with StringComparison to JObject
    • Change - Improved duplicate object reference id error message
    • Fix - Fixed error when comparing empty JObjects
    • Fix - Fixed SecAnnotate warnings
    • Fix - Fixed error when comparing DateTime JValue with a DateTimeOffset JValue
    • Fix - Fixed serializer sometimes not using DateParseHandling setting
    • Fix - Fixed error in JsonWriter.WriteToken when writing a DateTimeOffset
    • Fix - Fixed error when serializing emitted classes that have null constructor parameter names
    • Fix - Fixed empty strings not correctly deserializing as null onto nullable properties

    Links

    Json.NET CodePlex Project

    Json.NET 4.5 Release 10 Download – Json.NET source code and assemblies

  • Json.NET vs Windows.Data.Json

    Windows 8 introduces a new way to work with JSON via the Windows.Data.Json namespace. Similar to LINQ to JSON in Json.NET it defines classes that can be used to parse values, strings, objects, and arrays from JSON text or serialize value types into JSON text.

    Below is a comparison of Json.NET’s LINQ to JSON to Window 8’s Windows.Data.Json.

    Creating JSON

    The big difference between the two libraries when creating JSON is Windows.Data.Json requires string/integer/double values to be explicitly converted to JsonValue objects.

    Note that there is a weird limitation to creating JSON with Windows.Data.Json: it doesn’t allow you to set properties to null or have null array values.

    // Windows.Data.Json
    // -----------------
    JsonObject jsonObject = new JsonObject
      {
        {"CPU", JsonValue.CreateStringValue("Intel")},
        {"Drives", new JsonArray {
            JsonValue.CreateStringValue("DVD read/writer"),
            JsonValue.CreateStringValue("500 gigabyte hard drive")
          }
        }
      };
    string json1 = jsonObject.Stringify();
     
    // LINQ to JSON
    // ------------
    JObject jObject = new JObject
      {
        {"CPU", "Intel"},
        {"Drives", new JArray {
            "DVD read/writer",
            "500 gigabyte hard drive"
          }
        }
      };
    string json2 = jObject.ToString();

    Querying JSON

    Windows.Data.Json requires a value to be cast to its exact type with the GetObject/GetArray methods before it can be used, making Windows.Data.Json’s code verbose compared to LINQ to JSON.

    string json = @"{
      ""channel"": {
        ""title"": ""James Newton-King"",
        ""link"": ""http://james.newtonking.com"",
        ""description"": ""James Newton-King's blog."",
        ""item"": [
          {
            ""title"": ""Json.NET 1.3 + New license + Now on CodePlex"",
            ""description"": ""Annoucing the release of Json.NET 1.3, the MIT license and the source being available on CodePlex"",
            ""link"": ""http://james.newtonking.com/projects/json-net.aspx"",
            ""category"": [
              ""Json.NET"",
              ""CodePlex""
            ]
          }
        ]
      }
    }";
     
    // Windows.Data.Json
    // -----------------
    JsonObject jsonObject = JsonObject.Parse(json);
    string itemTitle1 = jsonObject["channel"].GetObject()["item"].GetArray()[0].GetObject()["title"].GetString();
     
    // LINQ to JSON
    // ------------
    JObject jObject = JObject.Parse(json);
    string itemTitle2 = (string)jObject["channel"]["item"][0]["title"];

    Performance

    Json.NET is slightly slower at writing JSON than Windows.Data.Json but considerably faster parsing.

    Things aren’t as happy as they used to be down here at the unemployment office. Joblessness is no longer just for philosophy majors. Useful people are starting to feel the pinch.

    Converting Between LINQ to JSON and Windows.Data.Json

    Json.NET supports converting between the types of the two libraries. Use FromObject to convert a Windows.Data.Json value to LINQ to JSON and use ToObject to convert LINQ to JSON to Windows.Data.Json.

    JsonObject jsonObject = new JsonObject
      {
        {"CPU", JsonValue.CreateStringValue("Intel")},
        {"Drives", new JsonArray {
            JsonValue.CreateStringValue("DVD read/writer"),
            JsonValue.CreateStringValue("500 gigabyte hard drive")
          }
        }
      };
     
    // convert Windows.Data.Json to LINQ to JSON
    JObject o = JObject.FromObject(jsonObject);
     
    // convert LINQ to JSON to Windows.Data.Json
    JArray a = (JArray)o["Drives"];
    JsonArray jsonArray = a.ToObject<JsonArray>();
  • Json.NET 4.5 Release 8 – Multidimensional Array Support, Unicode Improvements

    Multidimensional Array Support

    Json.NET now supports serializing and deserializing multidimensional arrays. There isn't anything you need to do, if one of your types has a multidimensional array property It Just Works™.

    string[,] famousCouples = new string[,]
      {
        { "Adam", "Eve" },
        { "Bonnie", "Clyde" },
        { "Donald", "Daisy" },
        { "Han", "Leia" }
      };
     
    string json = JsonConvert.SerializeObject(famousCouples, Formatting.Indented);
    // [
    //   ["Adam", "Eve"],
    //   ["Bonnie", "Clyde"],
    //   ["Donald", "Daisy"],
    //   ["Han", "Leia"]
    // ]
     
    string[,] deserialized = JsonConvert.DeserializeObject<string[,]>(json);
     
    Console.WriteLine(deserialized[3, 0] + ", " + deserialized[3, 1]);
    // Han, Leia

    Unicode Performance Improvements

    Prior versions of Json.NET allocated a new array and string object to the heap for every Unicode character; a potentially expensive situation if your application is writing a lot of Unicode characters.

    This release improves Unicode serialization so that Json.NET will only allocate a single array for all Unicode text and then writes the text directly to the underlying output stream. These changes improve Json.NET performance of Unicode intensive objects by about 20% and significantly reduces the time spent doing garbage collection.

    Changes

    Here is a complete list of what has changed since Json.NET 4.5 Release 7.

    • New feature - Serialize and deserialize multidimensional arrays
    • New feature - Members on dynamic objects with JsonProperty/DataMember will now be included in serialized JSON
    • New feature - LINQ to JSON load methods will read past preceding comments when loading JSON
    • New feature - Improved error handling to return incomplete values upon reaching the end of JSON content
    • Change - Improved performance and memory usage when serializing Unicode characters
    • Change - The serializer now creates objects using GetUninitializedObject when deserializing a Serializable type
    • Fix - Added SecurityTransparent attribute to WinRT build
    • Fix - Fixed infinite loop error caused by error handling upon reaching the end of JSON content

    Links

    Json.NET CodePlex Project

    Json.NET 4.5 Release 8 Download – Json.NET source code and assemblies

  • Json.NET 4.5 Release 5 – JsonProperty enhancements

    JsonProperty enhancements

    JsonPropertyAttribute now has options on it to customize a property’s collection items. When ItemConverter, ItemIsReference, ItemTypeNameHandling or ItemReferenceLoopHandling is set on JsonProperty and the property’s type is a collection then those settings will be applied to every collection item.

    public class Event
    {
      public string EventName { get; set; }
      public string Venue { get; set; }
     
      [JsonProperty(ItemConverterType = typeof(JavaScriptDateTimeConverter))]
      public IList<DateTime> Performances { get; set; }
    }

    Usage:

    Event e = new Event
      {
        EventName = "Blackadder III",
        Venue = "Gryphon Theatre",
        Performances = new List<DateTime>
          {
            DateTime.Parse("8 Tue May 2012, 6:30pm"),
            DateTime.Parse("9 Wed May 2012, 6:30pm"),
            DateTime.Parse("10 Thu May 2012, 8:00pm")
          }
      };
     
    string json = JsonConvert.SerializeObject(e, Formatting.Indented);
    //{
    //  "EventName": "Blackadder III",
    //  "Venue": "Gryphon Theatre",
    //  "Performances": [
    //    new Date(1336458600000),
    //    new Date(1336545000000),
    //    new Date(1336636800000)
    //  ]
    //}    

    Changes

    Here is a complete list of what has changed since Json.NET 4.5 Release 4.

    • New feature - Added ItemIsReference, ItemReferenceLoopHandling, ItemTypeNameHandling, ItemConverterType to JsonPropertyAttribute
    • New feature - Added ItemRequired to JsonObjectAttribute
    • New feature - Added Path to JsonWriterException
    • Change - Improved deserializer call stack memory usage
    • Change - Moved the PDB files out of the NuGet package into a symbols package
    • Fix - Fixed infinite loop from an input error when reading an array and error handling is enabled
    • Fix - Fixed a base object error not being handled when deserializing

    Links

    Json.NET CodePlex Project

    Json.NET 4.5 Release 5 Download – Json.NET source code, documentation and binaries

  • Json.NET 4.5 Release 4 – Portable Class Library Build

    They took my dressing room, my parking space, even my writer, so I don't have a funny third item.Portable Class Library Build

    Json.NET now has a Portable Class Library build that can be found in the /Bin/Portable directory from the zip download on CodePlex. The portable build targets the absolute basics (.NET 4.0, SL4, WP7, Metro) and so hopefully should run on just about everything.

    Improved Collection Serialization

    JsonArrayAttribute and JsonDictionaryAttribute now have properties to customize the serialization of collection items. ItemConverter, ItemTypeNameHandling, ItemReferenceLoopHandling and ItemIsReference will apply to all the items of that list/dictionary when serializing and deserializing.

    Changes

    • New feature – Added Portable Class Library build
    • New feature - Added support for customizing the JsonConverter, type name handling and reference handling of collection items
    • New feature - Added Path to JsonReaderException/JsonSerializationException messages
    • New feature - Added DateParseHandling to JsonReader
    • New feature - Added JsonContainerContract
    • New feature - Added JsonDictionaryAttribute
    • Change – Instances of Exception have been changed to be a more specific exception
    • Fix – Fixed Windows Application Certification tool error by removing AllowPartiallyTrustedCallersAttribute
    • Fix - Fixed JsonWriters not using DateTimeZoneHandling

    Links

    Json.NET CodePlex Project

    Json.NET 4.5 Release 4 Download – Json.NET source code, documentation and binaries

  • Json.NET 4.5 Release 2 – Serializable support and bug fixes

    Serializable Support

    Json.NET now detects types that have the SerializableAttribute and serializes all the fields on that type, both public and private, and ignores the properties. This is useful when you’re interested in round-tripping the data on a type and don’t care what the JSON looks like.

    If you are serializing types that have the attribute and don’t want the new behaviour, it can either be overridden on a type using the JsonObjectAttribute or disabled globally by setting IgnoreSerializableAttribute on DefaultContractResolver to true.

    Update: IgnoreSerializableAttribute is true by default in release 3. If you want it you can still achieve this effect by either setting IgnoreSerializableAttribute to false or adding [JsonObject(MemberSerialization.Fields)] to your class.

    Bug fixes

    Interesting bug fixes this release include correctly reading JSON from slow streams (e.g. a network stream that is only returning one character at a time), fixing invalid Unicode surrogate characters, and ignoring property order when comparing two JObjects (JavaScript objects aren’t ordered and so could come back serialized in a different order).

    Changes

    • New feature - Added support for the SerializableAttribute and serializing a type's internal fields
    • New feature - Added MaxDepth to JsonReader/JsonSerializer/JsonSerializerSettings
    • New feature - Added support for ignoring properties with the NonSerializableAttribute
    • Fix - Fixed deserializing a null string throwing a NullReferenceException
    • Fix - Fixed JsonTextReader incorrectly reading from a slow stream
    • Fix - Fixed CultureInfo not being overridden on JsonSerializerProxy
    • Fix - Fixed full trust security check in .NET 2.0 & .NET 3.5
    • Fix - Fixed XmlNodeConverter not turning all attribute properties into attributes
    • Fix - Fixed comparing JObjects to ignore property order
    • Fix - Fixed reading invalid Unicode surrogate pairs

    Links

    Json.NET CodePlex Project

    Json.NET 4.5 Release 2 Download – Json.NET source code, documentation and binaries

  • Json.NET Strong Naming and Assembly Version Numbers

    In the Json.NET 4.5 release post I added a note about strong naming and assembly version numbers that is worth expanding on.

    The problem is a combination of factors: Json.NET is strong named, has frequent releases, the assembly version number changes with each release and many different libraries reference Json.NET.

    NuGet does its best to solve the issue of different assemblies referencing different versions by automatically inserting binding redirects for you, but having to rely on a tool to fix up problems is sub-optimal.

    The solution that I’m evaluating to improve the situation is to only update the assembly version number on major releases (update: now evaluating never updating the assembly version). For example Json.NET 4.5 R1 has an assembly version number of 4.5.0.0, R2 will have an the version number 4.5.0.0, R3 = 4.5.0.0, and so on. While the assembly version number will be static the assembly file version number will continue to be updated each release so you can identify exactly which version you are using, e.g. 4.5.1.14720.

    The .NET framework is doing something similar with .NET 4.5, replacing 4.0 assemblies with new files using the same version number.

  • Json.NET Metro Upgrade Kit

    Metro Color Conversion

    The Metro UI color scheme looks complex and hard to code for at first but Json.NET has everything under control with its advanced automatic Metro Color Enhancement Suite™.

    Before:

    Color[] colors = new []{ Color.Blue, Color.Red, Color.Yellow, Color.Green, Color.Black, Color.Brown };
     
    string json = JsonConvert.SerializeObject(colors);
    // ["Blue", "Red", "Yellow", "Green", "Black", "Brown"]

    Hey, what’s the matter? Oh, right. My grotesque appearence!

    Yuck! Color is so 2011.

    Json.NET Metro Enhanced:

    Color[] colors = new []{ Color.Blue, Color.Red, Color.Yellow, Color.Green, Color.Black, Color.Brown };
     
    string json = JsonConvert.SerializeObject(colors);
    //["Gray", "Gray", "Gray", "Gray", "Black", "Gray"]

    Call me a killjoy, but I think that because this is not to my taste, no one else should be able to enjoy it.

    Now my Metro application will fit right in!

    Metro Text Rendering

    The application now looks right but there is someone off about the content. Don’t fear, Json.NET’s Metro Integration automatically upgrades your existing content to be Windows 8 ready.

    Before:

    string json = JsonConvert.SerializeObject(product);
    //{
    //  "Name": "Apple",
    //  "ExpiryDate": "2012-04-01T00:00:00",
    //  "Price": 3.99,
    //  "Sizes": [ "Small", "Medium", "Large" ]
    //}

    Are these words? I can barely even read this!

    Json.NET Metro Enhanced:

    string json = JsonConvert.SerializeObject(product);
    //{
    //  ":::NAME:::": ":::APPLE:::",
    //  ":::EXPIRYDATE:::": "2012-04-01T00:00:00",
    //  ":::PRICE:::": 3.99,
    //  ":::SIZES:::": [ ":::SMALL:::", ":::MEDIUM:::", ":::LARGE:::" ]
    //}

    Oh that’s much better.

    In-place Upgrade Kit

    Json.NET Metro Integration will be included in the next release but until then an upgrade kit has been put together. Begin building tomorrow’s next generation UIs today!

    https://gist.github.com/2269465

    string json = JsonConvert.SerializeObject(value, new JsonSerializerSettings
    {
      ContractResolver = new MetroPropertyNameResolver(),
      Converters = { new MetroStringConverter(), new MetroColorConverter() },
      Formatting = Formatting.Indented
    });

    :::HAPPY CODING!!!!:::

  • Json.NET 4.5 Release 1 - ISO dates, Async, Metro build

    This is the first major release of Json.NET 4.5. Read on for the details.

    ISO Dates

    Json.NET now writes dates as an ISO 8601 string by default instead of Microsoft’s format.

    The reason for the change is the ISO format is becoming the standard for JSON libraries. All major browsers output an ISO date from the JSON.stringify function for example. Other benefits are the ISO 8601 standard is human readable unlike Unix ticks and the format is better at handling time zones.

    This is a breaking change but there is option to make dates go back to what they were called DateFormatHandling. Setting DateFormatHandling on either the JsonSerializer or JsonSerializerSettings to MicrosoftDateFormat will keep your dates nice and Unix ticky.

    Async Serialization

    There are new helper methods on JsonConvert for serializing and deserializing JSON asynchronously.

    private static async Task<ExpensiveViewModel> CreateExpensiveViewModel()
    {
      // these happen simultaneously
      var people = JsonConvert.SerializeObjectAsync(GetPeople());
      var products = JsonConvert.SerializeObjectAsync(GetProducts());
     
      return new ExpensiveViewModel
      {
        People = await people,
        Products = await products
      };
    }

    Metro Build

    Json.NET now supports Windows 8 Metro applications (i.e. WinRT). Since WinRT is still in beta, and because reflection has to be rewritten for this build (booo) there maybe bugs in more esoteric serialization scenarios.

    Assembly Versioning

    I’m going to experiment with only updating the assembly version at major version changes (i.e. 4.5.0.0) and letting the assembly file version keep the true version number, similar to what the ASP.NET team do with MVC assemblies. Breaking changes will only happen on major version changes.

    Hopefully this should reduce the amount of assembly binding redirecting going on for everyone.

    And other stuff

    A sweet bug fix means that a performance test now runs 70,000% faster, the path of the current JSON location is now on JsonReader and exceptions for easier debugging and a potential security hole when serializing ISerializable types in partial trust has been closed.

    Changes

    Here is a complete list of what has changed since Json.NET 4.0 Release 8.

    • New feature - Windows 8 Metro build
    • New feature - JsonTextReader automatically reads ISO strings as dates
    • New feature - Added DateFormatHandling to control whether dates are written in the MS format or ISO format, with ISO as the default
    • New feature - Added DateTimeZoneHandling to control reading and writing DateTime time zone details
    • New feature - Added async serialize/deserialize methods to JsonConvert
    • New feature - Added Path to JsonReader/JsonWriter/ErrorContext and exceptions with the JSON path of the current position
    • New feature - Added collection type to JsonArrayContract
    • New feature - Added dictionary key type and dictionary value type to JsonDictionaryContract
    • New feature - Added reader/writer specific Formatting, DateFormatHandling and DateTimeZoneHandling to JsonSerializerSettings
    • New feature - Added ReadAsDate and ReadAsString to JsonReader
    • New feature - Added IgnoreSerializableInterface to DefaultContractResolver
    • Change - Dates are now serialized as the ISO format by default
    • Change - The ReadAsXXX methods on JsonReader now return null at the end of an array instead of throwing an error
    • Change - JsonReaders now to set TokenType to JsonToken.None after finishing content
    • Change - Deserializing will fallback to use a private default constructor
    • Change - The error message when deserializing a JSON object/array onto the wrong kind of type is more descriptive
    • Change - Serializing ISerializable types under partial trust now errors to fix potential security issue
    • Fix - Fixed reading scientific notation numbers with no decimal point
    • Fix - Fixed LinqBridge collision error in .NET 2.0 by moving types to a different namespace
    • Fix - Fixed error when deserializing nullable types with no content
    • Fix - Fixed JObject.Keys null reference error when the object has no items
    • Fix - Fixed error handling when failing to parse array content
    • Fix - Fixed error handling when there are missing required properties
    • Fix - Fixed performance issue when building deeply nested JSON to LINQ to JSON objects

    Links

    Json.NET CodePlex Project

    Json.NET 4.5 Release 1 Download – Json.NET source code, documentation and binaries

  • Json.NET 4.0 Release 8 – Bug fixes

    This is a relatively minor release that fixes all outstanding bugs and adds a couple of enhancements.

    NuGet Silverlight/Windows Phone Assemblies No Longer Strong-Named

    The big change this release is that the Silverlight/Windows Phone NuGet assemblies are no longer strong-named.

    Unfortunately Silverlight/Windows Phone applications do not support redirecting strong-named assemblies like normal .NET apps do via <assemblyBinding> configuration. If a Silverlight or Windows Phone application uses two libraries that each reference a different version of Json.NET then the only solution is to get the source code for the libraries and recompile them yourself to use the same version of Json.NET – not ideal.

    From 4.0.8 the NuGet assemblies for Silverlight/Windows Phone are no longer strong-named. If you do need strong-named versions of Json.NET for SL/WP then they are still available in the Json.NET zip download from CodePlex in the Bin\Silverlight\Signed and Bin\WindowsPhone\Signed zip folders. I don’t recommend using them with reusable SL/WP libraries because of the reasons above.

    Json.NET Assembly Redirection

    In case you ever do end up with multiple libraries that reference different versions of Json.NET in a normal .NET application, here is the configuration to fix your application use one version of Json.NET:

    <runtime>
      <assemblyBindingxmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
          <assemblyIdentityname="Newtonsoft.Json"publicKeyToken="30ad4fe6b2a6aeed" />
          <bindingRedirectoldVersion="1.0.0.0-4.0.0.0"newVersion="4.0.8.0" />
        </dependentAssembly>
      </assemblyBinding>
    </runtime>

    Update newVersion as needed to the version of Json.NET you are using. This can be found in Visual Studio by right-clicking on Newtonsoft.Json reference and selecting Properties.

    Mr. Simpson, you're smarter than you look, or sound, or our best testing indicates.

    Changes

    Here is a complete list of what has changed since Json.NET 4.0 Release 6 (if you’re curious where v7 is, it was a minor release to fix a couple of urgent bugs).

    • New feature - Added VersionConverter for System.Version
    • New feature - Added a JSON schema IsValid overload that returns a list of error messages
    • Change - NuGet Silverlight/Windows Phone assembies are no longer strong-named
    • Fix - Fixed Json.NET attributes on nullable struct properties not being used
    • Fix - Fixed deserializing nullable enums
    • Fix - Fixed JsonConstructor incorrectly being allowed on properties
    • Fix - Fixed empty string being changed to null when deserializing object properties
    • Fix - Fixed not replacing ignored properties when resolving an object's contract
    • Fix - Fixed JsonReader.ReadAsDateTimeOffset throwing an incorrect error message
    • Fix - Fixed error when converting XML to JSON with a default namespace
    • Fix - Fixed JsonValidatingReader.ReadAsBytes throwing an exception
    • Fix - Fixed a unit test failing because of the running computer’s timezone

    Links

    Json.NET CodePlex Project

    Json.NET 4.0 Release 8 Download – Json.NET source code, documentation and binaries

  • Json.NET 4.0 Release 6 – Serialization Performance

    The big feature of this release is improved performance. For the first time in a couple of years I’ve sat down and benchmarked, profiled and tuned Json.NET.

    And now, in the spirit of the season: start shopping. And for every dollar of Krusty merchandise you buy, I will be nice to a sick kid. For legal purposes, sick kids may include hookers with a cold.

    For all its features Json.NET was already fast but there are improvements in JsonTextReader which I rewrote and object deserilization. Json.NET is faster than both .NET framework JSON serializers in all scenarios again.

    Improved error messages

    One of the nice features of JsonTextReader is that it keeps track of its current line number and line position and includes that information when it encounters bad JSON.

    This release adds that line number and position information to deserialization errors. Now deserialization should be much easier to troubleshoot.

    string json = @"[
      1,
      2,
      3,
      null
    ]";
     
    List<int> numbers = JsonConvert.DeserializeObject<List<int>>(json);
    // Error converting value {null} to type 'System.Int32'. Line 5, position 7.

    Worst. Bug. Ever. Fixed.

    Visual Studio Ultimate Edition has a feature called Intellitrace. Intellitrace and Json.NET didn’t get along. After a year of at first being unsure why only some people were getting the error, and then not knowing what exactly why Intellitrace was breaking Json.NET, I finally nailed it down to one three line method that Intellitrace didn’t like. Fixed... finally.

    Changes

    Here is a complete list of what has changed since Json.NET 4.0 Release 4 (if you’re curious where v5 is, it was just one fix for Windows Phone Mango and NuGet).

    • New feature - Added line number information to deserialization errors
    • New feature - Added ReadAsInt32 to JsonReader
    • New feature - Added BinaryReader/BinaryWriter constructor overloads to BsonReader/BsonWriter
    • Change - JsonTextReader error message when additional JSON content found is more descriptive
    • Fix - Removed unused utility methods
    • Fix - Fixed elusive Intellitrace runtime destabilization error
    • Fix - Fixed internal exception thrown when deserializing Decimal/DateTimeOffset/Byte lists
    • Fix - Fixed potential multi-threading serializing issue
    • Fix - Fixed serializing types that hides a base classes property with a proeprty of the same name
    • Fix - Fixed BsonReader to use BinaryReader instead of base stream
    • Fix - Fixed referencing the NuGet package from Windows Phone 7.1 projects

    Links

    Json.NET CodePlex Project

    Json.NET 4.0 Release 6 Download – Json.NET source code, documentation and binaries