JSON Copy version 1.0 for Xamarin Studio

Few months back I published an Addin for MonoDevelop and Xamarin Studio to allow developer to paste JSON string as C# classes. The functionality was primitive and experimental, we did not have any control over the code formatting or class structure.

In v1.0 I have fixed most of those limitations in the Addin, now we have more control over how C# structured. You can find download and installation instruction here.

Here is what’s new in version 1.0:

  1. Choose between Pascal Casing and default lowercase letters
  2. Mark classes as internal or public
  3. Create Properties or Fields for JSON properties.
  4. New Preferences Panel

Imgur

I would love to hear your feedback, please create an issue in Github repo or leave a comment

Paste JSON as Classes in Xamarin Studio

Update: Now compatible with Xamarin Studio/MonoDevelop 5

As a C# mobile developer I spend lot of time writing classes to serialise or deserialise JSON string when integrating apps with third-party REST APIs.Visual Studio has an amazing feature to rescue us from this tedious job, Paste JSON As Classes command, where it automatically generates the strongly typed classes for JSON string.

I was motivated to create a similar add-in for my primary development IDE, Xamarin Studio. I started writing the C# code generator with T4 templates, and quickly realised that it will take too long to finish the development with limited time I get to spend on side projects.

Luckily, I stumbled upon this awesome open source project on codeplex to generate the C# classes for JSON text. It supports typed arrays, typed objects, integers, floats, booleans, strings and nullable types. It helped me quickly put together an Add-in to paste JSON from clipboard to C# classes in Xamarin Studio

image

Installation

You need to follow these three easy steps to install it on our Xamarin Studio.

  1. Download the add-in package from Github
  2. Open Xamarin Studio -> Add-in Manager
  3. Click on Install from file… button and select the downloaded package to install

Once you install the add-in the Paste JSON as classes command will appear in Edit menu, that enable you to paste C# classes directly from the text in your clipboard.

The complete code for the add-in is available on the Github here, please add an issue if you find any bug or send me a pull request :)

Magic of NSLinguistic​Tagger

This evening one of my friends sent me the link of iA Writer’s video featuring their latest update to the app.

I was blown away by the syntax highlighter in edit mode, where it highlights words based on selection of parts of speech.


(Click on the image to enlarge)

At first, I thought they are doing some sophisticated natural language processing, I’m not expert but supporting multiple languages sounds like a tedious/impossible job to do in an year, unless there is easy way.

Apple provides us NSLinguistic​Tagger class as part of the iOS 5 SDK, it allows us to split up natural language text and tag it with information. As per the documentation it can identify languages, scripts and stem forms of words! NSLinguistic​Tagger is quite easy to use.

We can start by creating the instance of NSLinguisticTagger with tag schemes for a language and bunch of NSLinguisticTaggerOptions, options are used to tell tagger to ignore words, white spaces, punctuations etc.

var options = NSLinguisticTaggerOptions.JoinNames 
              | NSLinguisticTaggerOptions.OmitWhitespace;

var tagger = new NSLinguisticTagger (
    NSLinguisticTagger.GetAvailableTagSchemesForLanguage ("en"), 
    options);

Once we create the instance, we will assign it a string to analyse

tagger.AnalysisString = "A quick movement of the enemy will jeopardise six gunboats";

And finally, fetch the tags with EnumerateTagsInRange method and print them out.

tagger.EnumerateTagsInRange (new NSRange (0, statement.Length), NSLinguisticTag.SchemeLexicalClass, options, TaggerEnumerator);

...

void TaggerEnumerator (NSString tag, NSRange tokenRange, NSRange sentenceRange, ref bool stop)
{
    var word = statement.Substring (tokenRange.Location,
      tokenRange.Length);
    words.Add (new Tuple<string, string> (tag, word));
}

The result after processing A quick movement of the enemy will jeopardise six gunboats looks something like this:

 Adjective: quick
 Noun: movement, enemy, gunboats
 Preposition: of
 Verb: will, jeopardise
 Number: six

it even identifies the number, that’s pretty amazing!

PS: You can find the complete code here – https://gist.github.com/prashantvc/8039121

Modal UIAlertView (iOS 7)

It’s common practice to use NSRunLoop to show modal UIAlertView to block code execution until user input is processed in iOS.

The below code worked perfectly on previous versions of iOS (5 & 6)


int ShowModalAletView (string title, string message, params string[] buttons)
{
int clicked = -1;
var alertView = new UIAlertView (title, message, null, null, buttons);
alertView.Show ();
bool done = false;
alertView.Clicked += (sender, buttonArgs) => {
Console.WriteLine ("User clicked on {0}", buttonArgs.ButtonIndex);
clicked = buttonArgs.ButtonIndex;
};
while (clicked == -1){
NSRunLoop.Current.RunUntil (NSDate.FromTimeIntervalSinceNow (0.5));
}
return clicked;
}

until Apple released the iOS7, it broke few existing apps 1. Fortunately, there is a better way of implementing it with C#’s async/await 

Here’s the new code taking advantage of async/await pattern to show modal UIAlertView:


Task ShowModalAletViewAsync (string title, string message, params string[] buttons)
{
var alertView = new UIAlertView (title, message, null, null, buttons);
alertView.Show ();
var tsc = new TaskCompletionSource ();

alertView.Clicked += (sender, buttonArgs) => {
Console.WriteLine (“User clicked on {0}”, buttonArgs.ButtonIndex);
tsc.TrySetResult(buttonArgs.ButtonIndex);
};
return tsc.Task;
}

//Usage
async Task PromptUser() {
var result = await ShowModalAletViewAsync (“Alert”, “Do you want to continue?”, “Yes”, “No”);
//process the result
}

Image Effects with Xamarin.Android

A while ago I was working on a sample app to demonstrate basic image editing abilities using ColorFilter to adjust an image brightness, contrast and saturation.

Color filters are great, but they are comparatively slow on large images, while looking for better options I learned that we could use Effects that are available in Android API Level 14

The effects in Android uses GPU and OpenGL textures to perform all the processing for maximum performances. The EffectFactory provides the list of various effects that can be applied to an image not just basic adjustments like brightness and contrast.

I have ported one of the Google’s sample to Xamarin.Android to demonstrate these effects, this should be a useful getting started sample for developers like me :)

The GitHub repository includes the full solution for the app in the screenshot if you want to run from it: monodroid-samples