Skip to content

Instantly share code, notes, and snippets.

@cameronsjo
Last active October 31, 2016 15:59
Show Gist options
  • Select an option

  • Save cameronsjo/9fef5ef39fc712a301b5c0b1b4d73cf7 to your computer and use it in GitHub Desktop.

Select an option

Save cameronsjo/9fef5ef39fc712a301b5c0b1b4d73cf7 to your computer and use it in GitHub Desktop.
Class for generating standardized JSON API responses in C#.
using Newtonsoft.Json;
/// <summary>
/// This is a slightly modified C# implementation of the JSON API format.
/// Documentation is available here: http://jsonapi.org/format/
/// </summary>
public class JsonResult
{
/// <summary>
/// </summary>
/// <param name="message"></param>
public JsonResult(string message) : this(true, message)
{
}
/// <summary>
/// </summary>
/// <param name="success"></param>
/// <param name="message"></param>
public JsonResult(bool success, string message)
{
Success = success;
Message = message;
}
/// <summary>
/// </summary>
/// <param name="errorCode"></param>
/// <param name="errorMessage"></param>
[SuppressMessage("ReSharper", "VirtualMemberCallInConstructor",
Justification = "JsonResult or JsonResult<T> will and should be used exclusively. However, in order to preserve the ability to cast down a JsonResult<T> to a JsonResult " +
"we have to allow the virtual member call in order to preserve the value of the JsonResult<T>'s ErrorInfo property.")]
public JsonResult(string errorCode, string errorMessage)
{
Error = new ErrorInfo() { Code = errorCode, Message = errorMessage };
Message = errorMessage;
Success = false;
}
/// <summary>
/// </summary>
[JsonProperty("message")]
public string Message { get; set; }
/// <summary>
/// </summary>
[JsonProperty("error")]
public virtual ErrorInfo Error { get; set; }
/// <summary>
/// </summary>
[JsonProperty("Success")]
public bool Success { get; set; }
/// <summary>
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="data"></param>
/// <returns></returns>
public static JsonResult<T> Create<T>(T data)
{
return new JsonResult<T>(data);
}
/// <summary>
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="data"></param>
/// <param name="message"></param>
/// <returns></returns>
public static JsonResult<T> Create<T>(T data, string message)
{
return new JsonResult<T>(data, message);
}
/// <summary>
/// </summary>
/// <param name="errorCode"></param>
/// <param name="errorMessage"></param>
/// <returns></returns>
public static JsonResult Create(string errorCode, string errorMessage)
{
return new JsonResult(errorCode, errorMessage);
}
/// <summary>
/// </summary>
public class ErrorInfo
{
/// <summary>
/// </summary>
[JsonProperty("code")]
public string Code { get; set; }
/// <summary>
/// </summary>
[JsonProperty("message")]
public string Message { get; set; }
}
}
public class JsonResult<T> : JsonResult
{
private T _data;
private ErrorInfo _error;
/// <summary>
/// </summary>
/// <param name="data"></param>
public JsonResult(T data) : this(data, null)
{
}
/// <summary>
/// </summary>
/// <param name="data"></param>
/// <param name="message"></param>
public JsonResult(T data, string message) : base(true, message)
{
Data = data;
}
/// <summary>
/// </summary>
[JsonProperty("error")]
public override ErrorInfo Error
{
get { return _error; }
set
{
if (_data != null)
{
throw new InvalidOperationException("Object cannot have both error and data fields.");
}
_error = value;
}
}
/// <summary>
/// </summary>
[JsonProperty("data")]
public T Data
{
get { return _data; }
set
{
if (Error != null)
{
throw new InvalidOperationException("Object cannot have both error and data fields.");
}
_data = value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment