3

I'm writing simple API which returns array of JSON objects:

[
    {
        "Id": 134649,
        "Type_id": 6,
        "Latitude": 56.904220,
        "Longitude":14.823440
    },
    {
        "Id": 134660,
        "Type_id": 6,
        "Latitude": 56.884040,
        "Longitude":14.761320
    }
]

This is generated by Response.MapEntries model I wrote:

class MapEntries
{
    public ulong Id { get; set; }
    public int Type_id { get; set; }
    public decimal Latitude { get; set; }
    public decimal Longitude { get; set; }
}

And filled and returned like this:

List<Response.MapEntries> entries = new List<Response.MapEntries>();
using (IDbConnection db = Connection.Instance())
{
    db.Open();
    entries = db.Query<Response.MapEntries>(query.ToString(), parameters).ToList();
}

return entries;

PROBLEM

JSON response has useless information like JSON object's names. By one request it can return up to 20000(1.2MB) records (demo image: https://i.sstatic.net/Corw5.jpg).

I think I can save around 40% of data transfer, if I change JSON to not associative arrays. But I don't know how to do that, because I'm new with C# and strict typed languages.

Response I want: [[134649, 6, 56.884040, 14.761320],[134649, 6, 56.884040, 14.761320]]

1 Answer 1

5

As you are mixing data types, return each item as an array of objects:

List<object[]> entries;
using (IDbConnection db = Connection.Instance()) {
  db.Open();
  entries = db.Query<Response.MapEntries>(query.ToString(), parameters)
    .Select(e => new object[] { e.Id, e.Type_id, e.Latitude, e.Longitude })
    .ToList();
}

return entries;
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, Linq. YES! How I haven't thought about it... Thanks mate!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.