Wednesday, September 19, 2012

Use C# to get JSON data from the web and map it to .NET class

using Json.net , available through Nuget package
Use C# to get JSON data from the web and map it to .NET class => made easy! - CodeProject

http://james.newtonking.com/projects/json/help/index.html?topic=html/SerializingJSON.htm

using native datacontractjsonserializer
http://msdn.microsoft.com/en-us/library/hh674188.aspx


using native .NET JavascriptSerializer

Example code for deserializing the following JSON :

string
res =
@" {
""exhibitions"": [
{
""id"": ""1056"",
""name"": ""Alfa Romeo Sustaining Beauty"",
""admission_fee"": false,
""opened_on"": ""2001-12-01"",
""closed_on"": ""2001-05-31"",
""closed"": false,
""website"": null
},

{
""id"": ""1082"",
""name"": ""Agriculture"",
""admission_fee"": false,
""opened_on"": null,
""closed_on"": null,
""closed"": false,
""website"": http://www.sciencemuseum.org.uk/visitmuseum/galleries/agriculture.aspx
} ] }";

// the class property names must match those JSON names
   
public class Exhibition
{
public int id {get; set;}
public string name { get; set; }
public bool admission_fee { get; set; }
public string opened_on { get; set; }
public string closed_on { get; set; }
public bool closed { get; set; }
public string website { get; set; }
}


public class Response {
public Exhibition[] exhibitions;
}


System.Web.Script.Serialization.JavaScriptSerializer json = new System.Web.Script.Serialization.JavaScriptSerializer();

Response  list = json.Deserialize(res);


Example code for serializing objects to JSON string

System.Web.Script.Serialization.JavaScriptSerializer json = new System.Web.Script.Serialization.JavaScriptSerializer();
var e1 = new Exhibition { name = "Test1", closed = true };
var e2 = new Exhibition { name = "Test2", id = 456 };
var r = new Response();
r.exhibitions = new Exhibition[2] { e1, e2 };
string s = json.Serialize(r);
 

   

 



 
 

Tuesday, September 18, 2012

A Cloud Storage Programming Interface

The SharpBox API has a bug with Box.Net storage provider see
A Cloud Storage Programming Interface - Store everything - sharpbox - View Issue #16934: BoxDotNet GetRoot(); always returning NULL.

--- sample console application showing how to list , upload and download dropbox and box.net files.
static void Main(string[] args)

{


CloudStorage dropBoxStorage = new CloudStorage();
CloudStorage boxNetStorage = new CloudStorage();

 
//credentials access
 
GenericNetworkCredentials cred = new GenericNetworkCredentials();

cred.UserName =
somebody@somemail.com;

cred.Password =
"somePassword";

 
//access token
ICloudStorageAccessToken accessToken = null;

using (FileStream fs = File.Open("dropbox", FileMode.Open, FileAccess.Read, FileShare.None))

{

accessToken = dropBoxStorage.DeserializeSecurityToken(fs);

}




var dropBoxConfig = CloudStorage.GetCloudConfigurationEasy(nSupportedCloudConfigurations.DropBox);
var boxNetConfig = CloudStorage.GetCloudConfigurationEasy(nSupportedCloudConfigurations.BoxNet);



var boxNetstorageToken = boxNetStorage.Open(boxNetConfig, cred);
 
var dropBoxstorageToken = dropBoxStorage.Open(dropBoxConfig, accessToken);
 
var boxNetRoot = boxNetStorage.GetRoot();

var dropBoxRoot = dropBoxStorage.GetRoot();


foreach (var fof in boxNetRoot)

{

Boolean bIsDir = fof is ICloudDirectoryEntry;
 
Console.WriteLine("{0}:{1}", bIsDir ? "DIR" : "FIL", fof.Name);

}


foreach (var fof in dropBoxRoot)
{


Boolean bIsDir = fof is ICloudDirectoryEntry;


Console.WriteLine("{0}:{1}", bIsDir ? "DIR" : "FIL", fof.Name);

}

string tmpFile = Environment.ExpandEnvironmentVariables("%temp%");
 
//Console.WriteLine("Downloading..");


//dropBoxStorage.DownloadFile(dropBoxRoot, "big_file",tmpFile, UploadDownloadProgress);


Console.WriteLine("Uploading..");

boxNetStorage.UploadFile(tmpFile+
"\\big_file", boxNetRoot, UploadDownloadProgress);


Console.WriteLine("Completed");

boxNetStorage.Close();

dropBoxStorage.Close();



}


static void UploadDownloadProgress(Object sender, FileDataTransferEventArgs e)

{


Console.Write("x");

e.Cancel =
false;

}

}