/// <summary> /// Helper method to write the data wrapper around a JSON payload. /// </summary> /// <param name="jsonWriter">The <see cref="JsonWriter"/> to write to.</param> /// <param name="writingResponse">A flag indicating whether we are writing a response; data wrappers are only added to response messages.</param> /// <param name="payloadWriterAction">The action that writes the actual JSON payload that is being wrapped.</param> internal static void WriteDataWrapper(JsonWriter jsonWriter, bool writingResponse, Action payloadWriterAction) { DebugUtils.CheckNoExternalCallers(); Debug.Assert(jsonWriter != null, "jsonWriter != null"); Debug.Assert(payloadWriterAction != null, "payloadWriterAction != null"); if (writingResponse) { // If we're writing a response payload the entire JSON should be wrapped in { "d": } to guard against XSS attacks // it makes the payload a valid JSON but invalid JScript statement. jsonWriter.StartObjectScope(); jsonWriter.WriteDataWrapper(); } payloadWriterAction(); if (writingResponse) { // If we were writing a response payload the entire JSON is wrapped in an object scope, which we need to close here. jsonWriter.EndObjectScope(); } }