1

I have created a Web API service that outputs JSON which is to be consumed by a KendoUI datagrid. Here's the rub: the KendoUI datagrid is VERY picky about the JSON code it consumes; it must not have any kind of carriage returns/line feeds. It has to be one big block of unformatted JSON.

We worked with someone from Telerik and they set up some kind of proxy (https://cors.io/?http://js01.consultwithus.us/odata/vw_FilesToBeProcessed_Dashboard), but it's just a band-aid.

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

        // enable CORS
        config.EnableCors();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        // we must set odata to version 2
        var vers = new Version(2, 0);

        ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
        builder.DataServiceVersion = vers;
        builder.MaxDataServiceVersion = vers;
        builder.EntitySet<vw_FilesToBeProcessed_Dashboard>("vw_FilesToBeProcessed_Dashboard");
        config.Routes.MapODataServiceRoute("odata", "odata", builder.GetEdmModel());
    }
}

I want my output to look like this, but right now it just looks like this.


UPDATE 12/28/2018

Here is the code for my controller:

[EnableCors(origins: "http://js01.consultwithus.us", headers: "*", methods: "*")]
public class vw_FilesToBeProcessed_DashboardController : ODataController
{
    private CSS_DevEntities db = new CSS_DevEntities();

    // GET: odata/vw_FilesToBeProcessed_Dashboard
    [EnableQuery]
    public IQueryable<vw_FilesToBeProcessed_Dashboard> Getvw_FilesToBeProcessed_Dashboard()
    {
        return db.vw_FilesToBeProcessed_Dashboard;
    }

    // GET: odata/vw_FilesToBeProcessed_Dashboard(5)
    [EnableQuery]
    public SingleResult<vw_FilesToBeProcessed_Dashboard> Getvw_FilesToBeProcessed_Dashboard([FromODataUri] int key)
    {
        return SingleResult.Create(db.vw_FilesToBeProcessed_Dashboard.Where(vw_FilesToBeProcessed_Dashboard => vw_FilesToBeProcessed_Dashboard.Files_PK == key));
    }

    // PUT: odata/vw_FilesToBeProcessed_Dashboard(5)
    public IHttpActionResult Put([FromODataUri] int key, Delta<vw_FilesToBeProcessed_Dashboard> patch)
    {
        Validate(patch.GetEntity());

        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        vw_FilesToBeProcessed_Dashboard vw_FilesToBeProcessed_Dashboard = db.vw_FilesToBeProcessed_Dashboard.Find(key);
        if (vw_FilesToBeProcessed_Dashboard == null)
        {
            return NotFound();
        }

        patch.Put(vw_FilesToBeProcessed_Dashboard);

        try
        {
            db.SaveChanges();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!vw_FilesToBeProcessed_DashboardExists(key))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }

        return Updated(vw_FilesToBeProcessed_Dashboard);
    }

    // POST: odata/vw_FilesToBeProcessed_Dashboard
    public IHttpActionResult Post(vw_FilesToBeProcessed_Dashboard vw_FilesToBeProcessed_Dashboard)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        db.vw_FilesToBeProcessed_Dashboard.Add(vw_FilesToBeProcessed_Dashboard);

        try
        {
            db.SaveChanges();
        }
        catch (DbUpdateException)
        {
            if (vw_FilesToBeProcessed_DashboardExists(vw_FilesToBeProcessed_Dashboard.Files_PK))
            {
                return Conflict();
            }
            else
            {
                throw;
            }
        }

        return Created(vw_FilesToBeProcessed_Dashboard);
    }

    // PATCH: odata/vw_FilesToBeProcessed_Dashboard(5)
    [AcceptVerbs("PATCH", "MERGE")]
    public IHttpActionResult Patch([FromODataUri] int key, Delta<vw_FilesToBeProcessed_Dashboard> patch)
    {
        Validate(patch.GetEntity());

        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        vw_FilesToBeProcessed_Dashboard vw_FilesToBeProcessed_Dashboard = db.vw_FilesToBeProcessed_Dashboard.Find(key);
        if (vw_FilesToBeProcessed_Dashboard == null)
        {
            return NotFound();
        }

        patch.Patch(vw_FilesToBeProcessed_Dashboard);

        try
        {
            db.SaveChanges();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!vw_FilesToBeProcessed_DashboardExists(key))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }

        return Updated(vw_FilesToBeProcessed_Dashboard);
    }

    // DELETE: odata/vw_FilesToBeProcessed_Dashboard(5)
    public IHttpActionResult Delete([FromODataUri] int key)
    {
        vw_FilesToBeProcessed_Dashboard vw_FilesToBeProcessed_Dashboard = db.vw_FilesToBeProcessed_Dashboard.Find(key);
        if (vw_FilesToBeProcessed_Dashboard == null)
        {
            return NotFound();
        }

        db.vw_FilesToBeProcessed_Dashboard.Remove(vw_FilesToBeProcessed_Dashboard);
        db.SaveChanges();

        return StatusCode(HttpStatusCode.NoContent);
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            db.Dispose();
        }
        base.Dispose(disposing);
    }

    private bool vw_FilesToBeProcessed_DashboardExists(int key)
    {
        return db.vw_FilesToBeProcessed_Dashboard.Count(e => e.Files_PK == key) > 0;
    }
}

I had Visual Studio scaffold this for me based on my EDM.

ShowJX1990
  • 77
  • 2
  • 11

3 Answers3

1

I believe you want to remove the formatting in the JSON string. You can do so using Newtonsoft.Json

   var jtoken = JToken.Parse(strValue);
   var formattedValue = jtoken.ToString(Newtonsoft.Json.Formatting.None);
Anu Viswan
  • 17,797
  • 2
  • 22
  • 51
0

You didn't show your controller method that is creating your JSON. However I can guess you have some kind object. Since you mention C# I'll vote for Newtonsoft too but instead suggest JsonConvert.SerializeObject() as the formatter. https://www.newtonsoft.com/json/help/html/SerializingJSON.htm

Something like:

var myResult = GetMyResult();
var response = JsonConvert.SerializeObject(myResult);
return response;
No Refunds No Returns
  • 8,092
  • 4
  • 32
  • 43
0

This behavior is controlled by the JsonSerializerSettings.Formatting setting. According to the documentation, the default value is None, meaning no line breaks or indents.

So if you are seeing new lines and indenting in your output, then that setting was changed to Newtonsoft.Json.Formatting.Indented somewhere. One of two things happened:

  1. That setting was set in that specific controller/action, or
  2. The default settings have been changed. It clearly hasn't been changed in the Register method, but it could be changed at any point by setting JsonConvert.DefaultSettings.

Basically, search your entire solution for any reference to Newtonsoft.Json.Formatting.Indented.

Update: You're using ODataController, which changes the serializer that is used. So it's not using Json.NET to serialize anymore. There is an answer here about how to make it use Json.NET. That example only shows deserialization, so you'll have to make the class for serialization too. Kind of annoying.

Gabriel Luci
  • 38,328
  • 4
  • 55
  • 84