Introduction

Sample JSDL

This sample JSDL is served from http://rsuter.com/Services/Jsdl/SampleService.php?operation=jsdl using the PHP JSDL generator (sample PHP code can be found in the project download).

SampleService PHP implementation: https://jsdl.svn.codeplex.com/svn/-%20Generators/PHP/SampleService.php

See also: JSDL specification

{
	"operations": {
		"sum": {
			"transport": "post",
			"target": "SampleService.php?operation=sum",
			"inputType": "json",
			"parameters": [{
				"type": "number",
				"name": "a"
			},
			{
				"type": "number",
				"name": "b"
			}],
			"returns": {
				"type": "number"
			},
			"description": "Adds two numbers"
		},
		"getPerson": {
			"transport": "post",
			"target": "SampleService.php?operation=getPerson",
			"inputType": "json",
			"parameters": [{
				"type": "number",
				"name": "id"
			}],
			"returns": {
				"type": "Person"
			}
		}
	},
	"types": [{
		"type": "object",
		"name": "Person",
		"properties": {
			"firstname": {
				"type": "string"
			},
			"lastname": {
				"type": "string"
			},
			"age": {
				"type": "number",
				"description": "Person's age in years."
			}
		}
	}]
}

Code Generators

Code generators generate boiler plate code for a specific language which can be used to access the web service in a typed manner.

JSDL Generators

JSDL generators generate JSDL from a given web service class or implementation in a given programming language or framework.
  • PHP: Generates JSDL from PHP class, see PHP JSDL generator
  • Csharp: Generates JSDL from a .NET WCF service class (see sample project in JSDL download)

Installation

Download the project from the Source Code tab and start the console application or use the .NET library from the "- Bin" directory in the downloaded ZIP archive.

Generate code using the JSDL .NET library

From the previous JSDL we can generate C# service client code using the JSDL .NET library:

var url = "http://rsuter.com/Services/Jsdl/SampleService.php?operation=jsdl";

var gen = CodeGenerator.Create("Csharp");
var code = gen.GenerateFromUrl(url, "SampleServiceClient", "ServicesNamespace");

File.WriteAllText("SampleServiceClient.cs", code);

This creates the following two classes:

public class SampleServiceClient
{
	public Task<double> SumAsync(double a, double b)
	{
		...
	}
	
	Task<Person> GetPersonAsync(double id)
	{
		...
	}
}

public class Person : INotifyPropertyChanged
{
	... // Properties with INotifyPropertyChanged support
}

Generate code using the JSDL command line tool

The C# web service client and domain classes can be generated using the command line tool:

jsdl --type Csharp --jsdl "url_or_path" --output SampleServiceClient.cs

See also: Command line usage

Generate code using T4 templates

To fully integrate the generation process into Visual Studio, you can create a T4 template which automatically downloads the JSDL and generates the C# classes (or other code). Every time the web service has changed, the T4 template can be reevaluated and the web service client class is immediately updated.

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="$(SolutionDir)\Jsdl\Jsdl.Library.dll" #>
<#@ assembly name="$(SolutionDir)\Jsdl\Newtonsoft.Json.dll" #>
<#@ import namespace="Jsdl" #>
<#@ import namespace="Jsdl.Generators" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="Microsoft.CSharp" #>
<#@ output extension=".cs" #>
<#
	// CONFIGURATION
	var jsdlFile = @"SampleService.json"; // relative to the T4 template's location
	var gen = (CsharpCodeGenerator)CodeGenerator.Create("Csharp");
	// -------------

	var provider = new CSharpCodeProvider();
	var clientClassName = provider.CreateEscapedIdentifier(
		Path.GetFileNameWithoutExtension(Host.TemplateFile));
	var clientClassNamespace = Host.ResolveParameterValue(
		"directiveId", "namespaceDirectiveProcessor", "namespaceHint");

	jsdlFile = Host.ResolvePath(jsdlFile);
	var code = generator.Generate(
		File.ReadAllText(jsdlFile), clientClassName, clientClassNamespace);
#>
<#= code #>

See also: T4 templates

Last edited Aug 23, 2013 at 2:27 PM by rsuter, version 8

Comments

No comments yet.