
June 14, 2009 20:12 by
Simon
I was involved in a project on a customers ASP.Net CMS to embed a flex application which enables the user to enter some parameters and see how they affect the results which are displayed in a stacked bar-chart. The formula's that are used to calculate the results are highly sensitive therefore the logic could not be embedded in the flex application, therefore the only alternative was to expose some .net web services to provide the functionality. Although the data that is returned by the web services is basic and fairly minimal, from my experience of using flex, I was concerned that this wouldn't be as straight forward as it seemed. One of my concerns about consuming .net web services with flex was whether Microsoft actually adhere properly to web service standards, therefore I decided to return the data as JSON. This would mean that we wouldn't be relying on Microsoft to properly adhere to web service standards and my colleague who was writing the flex application had experience of consuming JSON, albeit from web services written using Ruby.
I had never used JSON before so went about finding a library to convert my .Net objects into JSON. I soon came across JSON.Net written by James Newton-King. I was plesently surprised by how straight forward it was to use. I wrote the following web service:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using Newtonsoft.Json;
using Sharpcoder.BusinessEntities;
namespace Sharpcoder.Model
{
[WebService(Namespace = "http://Sharpcoder.Model/", Name = "ModelService")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class ModelService : System.Web.Services.WebService
{
/// <summary>
/// Calculates the model results.
/// </summary>
/// <param name="modelParameters">The model parameters.</param>
/// <returns>The model results as JSON.</returns>
[WebMethod]
public string CalculateModelResults(string modelParameters)
{
ModelParameters parameters =
JavaScriptConvert.DeserializeObject<ModelParameters>(modelParameters);
return JavaScriptConvert.SerializeObject(Calculate(parameters));
}
private IList<ModelSeries> Calculate(ModelParameters parameters)
{
// Sensitive formualas
}
}
}
Yes it really is that simple to convert .net objects to JSON - one method call to serialize them and another to deserialize them! The only thing that isn't obvious from the example above or the libraries documentation, is that this library requires .Net 3.5 to be installed on the box it is running on.
For completness the definitions of ModelParameters and ModelSeries are below:
public class ModelParameters
{
private double _discountRate;
private double _inflation;
private double _lifeExpectancy;
private IList<AssetModelParameter> _assetParams;
public ModelParameters()
{ }
public ModelParameters(double discountRate, double inflation, double lifeExpectancy,
IList<AssetModelParameter> assetParams)
{
_discountRate = discountRate;
_inflation = inflation;
_lifeExpectancy = lifeExpectancy;
_assetParams = assetParams;
}
public double DiscountRate
{
get { return _discountRate; }
set { _discountRate = value; }
}
public double Inflation
{
get { return _inflation; }
set { _inflation = value; }
}
public double LifeExpectancy
{
get { return _lifeExpectancy; }
set { _lifeExpectancy = value; }
}
public IList<AssetModelParameter> AssetParams
{
get { return _assetParams; }
set { _assetParams = value; }
}
}
public class AssetModelParameter
{
private int _id;
private double _marketCondition;
public AssetModelParameter()
{ }
public AssetModelParameter(int id, double marketCondition)
{
_id = id;
_marketCondition = marketCondition;
}
public int Id
{
get { return _id; }
set { _id = value; }
}
public double MarketCondition
{
get { return _marketCondition; }
set { _marketCondition = value; }
}
}
public class ModelSeries
{
private string _seriesName;
private IList<ModelResult> _results;
public ModelSeries()
{ }
public ModelSeries(string seriesName, IList<ModelResult> results)
{
_seriesName = seriesName;
_results = results;
}
public string SeriesName
{
get { return _seriesName; }
set { _seriesName = value; }
}
public IList<ModelResult> Results
{
get { return _results; }
set { _results = value; }
}
}
public class ModelResult
{
private string _categoryName;
private DateTime _categoryDate;
private double _value;
public ModelResult()
{ }
public ModelResult(string categoryName, DateTime categoryDate, double value)
{
_categoryName = categoryName;
_categoryDate = categoryDate;
_value = value;
}
public string CategoryName
{
get { return _categoryName; }
set { _categoryName = value; }
}
public DateTime CategoryDate
{
get { return _categoryDate; }
set { _categoryDate = value; }
}
public double Value
{
get { return _value; }
set { _value = value; }
}
}
1f9a9806-7750-4f11-b75a-53d66341d957|0|.0