/// <summary> /// Hook for storing data members by calling the various AddMember() overloads. /// </summary> /// <param name="builder">The Jsonizer instance to use for storing data members.</param> public void Jsonize(Jsonizer builder) { builder.AddMember("cdrid", CDRID.ToString(), true); builder.AddMember("dictionary", Dictionary.ToString(), true); builder.AddMember("language", Language.ToString(), true); builder.AddMember("audience", Audience.ToString(), true); }
/// <summary> /// Called by an implementation of IJsonizable to store a data member containing an array /// of objects, each of which implements IJsonizable. /// </summary> /// <param name="name">The object member's name</param> /// <param name="item">The object to store</param> /// <param name="isFinal">Is this the last member the caller will add?</param> internal void AddMember(string name, IJsonizable[] itemArray, bool isFinal) { builder.AppendFormat("\"{0}\": ", name); List <Jsonizer> subJsonizers = new List <Jsonizer>(itemArray.Length); foreach (IJsonizable item in itemArray) { Jsonizer subItem = new Jsonizer(item); subJsonizers.Add(subItem); } // Output a possibly empty array of IJsonizable objects. int length = subJsonizers.Count; int lastIndex = length - 1; builder.Append("["); for (int i = 0; i < itemArray.Length; i++) { builder.Append(subJsonizers[i].ToJsonString()); if (i != lastIndex) { builder.Append(","); } } builder.Append("]"); if (!isFinal) { builder.Append(","); } }
/// <summary> /// Called by an implementation of IJsonizable to store a data member which implements IJsonizable. /// </summary> /// <param name="name">The object member's name</param> /// <param name="item">The object to store</param> /// <param name="isFinal">Is this the last member the caller will add?</param> internal void AddMember(string name, IJsonizable item, bool isFinal) { Jsonizer subJsonizer = new Jsonizer(item); builder.AppendFormat("\"{0}\": {1}", name, subJsonizer.ToJsonString()); if (!isFinal) { builder.Append(","); } }
/// <summary> /// Entry point for handling the HTTP request. ProcessRequest manages the process of /// determining which method was requested, invoking it, and returning a JSON response. /// Part of IHttpHandler /// </summary> /// <param name="context">Object containing the details of current HTTP request and response.</param> public void ProcessRequest(HttpContext context) { HttpRequest request = context.Request; HttpResponse response = context.Response; try { // Get the particular method being invoked. ApiMethodType method = ParseApiMethod(request); // Get object for invoking the specific dictionary method. Invoker invoker = Invoker.Create(method, request); // Invoke the requested dictionary method. IJsonizable result = invoker.Invoke(); // Put together the JSON response. Jsonizer json = new Jsonizer(result); response.ContentType = "application/json"; response.Write(json.ToJsonString()); } // There was something wrong with the request that prevented us from // being able to invoke a method. catch (HttpParseException ex) { response.Status = ex.Message; response.StatusCode = 400; } // Something went wrong in our code. catch (Exception ex) { log.ErrorFormat(errorProcessFormat, ex, request.RawUrl); response.StatusDescription = "Error processing dictionary request."; response.StatusCode = 500; } }