-1

I am using Newtosoft.Json Library. Here is an example of my class.

 public class TableItemsClass
{
    public int InventoryTransTempID { get; set; }
    public string Description { get; set; }
    public string Extras { get; set; }
    public decimal Quantity { get; set; }
    public decimal ItemPrice { get; set; }
    public decimal TotalPrice { get; set; }
    public decimal SumPrice { get; set; }
    public decimal SumDisPrice { get; set; }
    public int Situation { get; set; }
    public string TemporaryText { get; set; } 
    public bool TableIsClosed { get; set; }
    public decimal KitchenQuantity { get; set; }
    public string KitchenTime { get; set; }
    public int InventoryItemID { get; set; }
}

I am sending my data through an http client. So i am using the function.

 var jsonString1 = JsonConvert.SerializeObject(mItems);

So my question is doest it worth performance and speed, for usinge GZip after my string's serialization? Or json automatically is compressing my string when i send a request through an http Client?

 var client = new HttpClient();
 await client.PostAsync("https://www.ddd.com",new StringContent( jsonString1,Encoding.UTF8,"application/json"));

Does it worth to use GZip Example

  var jsonString1 = Zip(JsonConvert.SerializeObject(mItems));

  public static void CopyTo(Stream src, Stream dest)
    {
        byte[] bytes = new byte[4096];

        int cnt;

        while ((cnt = src.Read(bytes, 0, bytes.Length)) != 0)
        {
            dest.Write(bytes, 0, cnt);
        }
    }
    public static byte[] Zip(string str)
    {
        var bytes = Encoding.UTF8.GetBytes(str);

        using (var msi = new MemoryStream(bytes))
        using (var mso = new MemoryStream())
        {
            using (var gs = new GZipStream(mso, CompressionMode.Compress))
            {
                //msi.CopyTo(gs);
                CopyTo(msi, gs);
            }

            return mso.ToArray();
        }
    }

Also is there any method to remove duplicate key values foreach property? For redusing my string's length?

DmO
  • 357
  • 2
  • 14
  • You should not be gzipping like with. It would be much better to let the server handle the compression. Serializing to JSON and returning is perfectly fine. Also, I would checkout Brotli as it’s a much better compression algorithm, although support is not as good as gzip. – rhys_stubbs Mar 14 '20 at 22:13
  • Thank you rhys_stubbs. I need to ask if i need to care about property's name length. Should i use JsonProperty for big property names? Or difference is insignificant? – DmO Mar 14 '20 at 22:14
  • use a naming convention that you find appropriate. As a starting point take a look at [General Naming Conventions for .NET](https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/general-naming-conventions) – rhys_stubbs Mar 14 '20 at 22:18
  • 2
    Please focus on one question at a time, and also try to keep it on-topic. No, JsonConvert.SerializeObject does not in any way compress your JSON. It returns it as a string. Is it worth it to compress? Probably, but only you can tell depending on the data you're sending. The last question, remove duplicate key values foreach property, I have no idea what this means, can you elaborate (in a separate question). – Lasse V. Karlsen Mar 14 '20 at 22:22
  • Thank you Lasse V. Karlsen. You have right i will open another question for this. – DmO Mar 14 '20 at 22:23
  • So my question is should i use an zip for my class above? or Not? – DmO Mar 14 '20 at 22:24
  • _"json automatically is compressing my string when i send a request through an http Client?"_ -- how would it do that? Is `JsonConvert` participating in the request somehow? Please explain where you think that's happening. Your basic question has, I think, already been addressed...but the fact is, it's really hard to understand what it is precisely you are really asking, because the apparent question seems so obviously answered by the code itself. – Peter Duniho Mar 14 '20 at 22:33
  • You're two unrelated questions here: 1) Can Json.NET serialize directly to a zip stream? 2) Is there any method to remove duplicate key values foreach property? The preferred format here is [one question per post](https://meta.stackexchange.com/q/222735), so you might want to split that into two questions. #1 looks to be a duplicate of [Can I decompress and deserialize a file using streams?](https://stackoverflow.com/q/32943899/3744182). – dbc Mar 14 '20 at 23:10
  • But Json.NET doesn't support async serialization or deserialization so you need to completely serialize before calling `PostAsync()`. – dbc Mar 14 '20 at 23:15

1 Answers1

1

Json is just text, formatted accordingly so it won't be compressed by itself, so yes, use gzip, however not in the backend but in the webserver config. You must send text to the client not a byte array.

For your other question about duplicate keys, you should filter those when you query your db, but you can do it with linq on backend:

List<TableItemsClass> items = getdata();
List<TableItemsClass> distinctTtems = items.Distinct().ToList();

However for this to work you have to override the equality comparer in your TableitemsClass.

zsolt
  • 1,233
  • 8
  • 18
  • Thank you. I saw an example.Instead of ID:1,Name: test,ID:2,Name:test2. They use one key Example ID:[1,2],Name:[test1,test2]. I wonder if there is such an option for this library, – DmO Mar 14 '20 at 22:27