Skip to content

Getting Started With JSON in 3.x

Scott Offen edited this page Jul 6, 2016 · 1 revision

JSON is a fantastic data exchange format, and working with it can be super easy with Json.NET.

For a version of Grapevine with JSON support baked in, try new Grapevine Plus!

While it is outside the scope of this document to give you the complete run down on either of those two technologies, here is a great way to extend all your RESTResource implementation to handle those lovely JObjects seamlessly.

Extend RESTResource

Start by creating a new abstract class that extends RESTResource.

public abstract class MyRESTResource : RESTResource
{
...
}

Have all your resources extend MyRESTResource instead of RESTResource, and all the methods we add to this class become available, in addition to the ones already there!

Parsing JSON From The Request

To your new MyRESTResource class, add this splendid method to parse the JSON payload for you.

protected JObject GetJsonPayload(HttpListenerRequest request)
{
	try
	{
		string data = this.GetPayload(request);
		if (!object.ReferenceEquals(data, null))
		{
			JObject json = JObject.Parse(data);
			return json;
		}
	}
	catch (Exception e)
	{
		EventLogger.Log(e);
	}

	return null;
}

Responding With JSON

Because you should never respond with anger - kids and animals can sense that, you know - add these methods to make it easy to send a JSON response.

protected void SendJsonResponse(HttpListenerContext context, object obj)
{
	var json = JsonConvert.SerializeObject(obj, Formatting.Indented);
	var buffer = context.Request.ContentEncoding.GetBytes(json);
	var length = buffer.Length;

	context.Response.ContentType = ContentType.JSON.ToValue();
	context.Response.ContentEncoding = Encoding.UTF8;
	context.Response.ContentLength64 = length;
	context.Response.OutputStream.Write(buffer, 0, length);
	context.Response.OutputStream.Close();
	context.Response.Close();
}

protected void SendJsonResponse(HttpListenerContext context, JObject json)
{
	var buffer = context.Request.ContentEncoding.GetBytes(json.ToString(Formatting.Indented));
	var length = buffer.Length;

	context.Response.ContentType = ContentType.JSON.ToValue();
	context.Response.ContentEncoding = Encoding.UTF8;
	context.Response.ContentLength64 = length;
	context.Response.OutputStream.Write(buffer, 0, length);
	context.Response.OutputStream.Close();
	context.Response.Close();
}

Put It All Together

Too much work to copy and paste each method? Grab it from this gist here.


Why didn't you do this in the first place?

A reasonable question, and one I expected I would be asked at some point in time, so I might as well answer it now.

Earlier versions of Grapevine could do this, but it requires an additional dependency on Json.NET. As I figured not everyone would want to use JSON to transport data - and of those, not everyone would want to use Json.NET - it seemed unnecessary to require everyone to download it.

So, ultimately, I didn't do it in the first place so as to allow you the freedom you deserve!