C
C#2y ago
oe

✅ Best way to return Tuple<string, string> as an Array?

I have an interface called IRequest, with Tuple lists called:
public List<Tuple<string, string>> Headers { get; set; } = new List<Tuple<string, string>>();
public List<Tuple<string, string>> Cookies { get; set; } = new List<Tuple<string, string>>();
public List<Tuple<string, string>> Headers { get; set; } = new List<Tuple<string, string>>();
public List<Tuple<string, string>> Cookies { get; set; } = new List<Tuple<string, string>>();
Unfortunately, when I export a class that inherits IRequest as JSON, it's then hard to iterate through Headers and Cookies, because Tuples in C# are treated like classes outside of it. (I'm using Angular to read the JSON). What's the best solution to export that Tuple as an Array of strings in the JSON? Take into account I will have multiple classes that inherit IRequest, so a global solution that affects all classes would be perfect in that sense.
36 Replies
FusedQyou
FusedQyou2y ago
But it is a class. It's a class consisting of property Item1 and Item2 What you want is not possible because you can't put it in an array unless you would flatten the tuple, which seems unlogical What exactly do the tuples represent? A key and value? If yes, why isn't it a dictionary? Maybe that's what you want?
oe
oeOP2y ago
Well, not really. Because I'm storing HttpHeader information and sometimes certain header values are repeated. The reason I want it not to be a class, is because iterating through that information in javascript is harder, and I'd rather fix the source (API)
FusedQyou
FusedQyou2y ago
Dictionary<string, string[]> ?
oe
oeOP2y ago
Oh wait, can you repeat keys in dictionaries?
FusedQyou
FusedQyou2y ago
But what do you expect? Do you want to flatten the tuple into a single string? No, I'm suggesting you put the values of a header in an array
oe
oeOP2y ago
Like a multideminsional array Is what I was thinking
FusedQyou
FusedQyou2y ago
Of what type?
oe
oeOP2y ago
But using this solution means I can't repeat key values right? What if there's 2 repeated http header names String
FusedQyou
FusedQyou2y ago
You still have 2 values for a single entry, which you can't put in a single array entry That's why I'm asking what you are expecting exactly The first value is the key, so any duplicates are added to the array
oe
oeOP2y ago
Yeah, but sometimes you have 2 HTTP headers with different values but the same name/key
FusedQyou
FusedQyou2y ago
That's what I said lol
oe
oeOP2y ago
I was thinking of doing something like this @FusedQyou https://stackoverflow.com/a/59188152/20804457
Stack Overflow
How to turn a Tuple into an Array in C#?
I can't find anything about this so I'm not sure if it is possible but I have a tuple that contains the coordinates of an element in a two-dimensional array. I want to be able to find the distance
FusedQyou
FusedQyou2y ago
The key of the dictionary entry is the key of the header, and the values are stored in the array
oe
oeOP2y ago
Hmm, maybe I explained this badly. One sec
FusedQyou
FusedQyou2y ago
This is not required because you know the length of your tuple
oe
oeOP2y ago
If you have 2 headers like so: Accept-Language: en-Us Accept-Language: en-Sp Instead of storing both values in one key, I want them to be a separate key:value pairs in an array That's why the dictionary solution can't work for me
FusedQyou
FusedQyou2y ago
A D I C T I O N A R Y This is literally a dictionary
oe
oeOP2y ago
Ok wait I migth be being stupid right now but In a dictionary you cant have 2 keys with the same value
FusedQyou
FusedQyou2y ago
I don't get why you want to store them individually
oe
oeOP2y ago
So how would I have 2 entries of Accept-Language with that rule Because that's what I want
FusedQyou
FusedQyou2y ago
Then you're back with having a list of tuples You can't have what you want because it's also supposed to be unique in json
oe
oeOP2y ago
Would converting the tuple to a multidimensional array make it easier to iterate through those headers in a non C# context?
FusedQyou
FusedQyou2y ago
FusedQyou
FusedQyou2y ago
This is impossible Even if you would do this, you would get an issue with deserialization because converters do not expect duplicate keys They're not made for it That's why I'm suggesting you put it under an existing key in the dictionary and unwrap it later
oe
oeOP2y ago
Ahh! Ok
FusedQyou
FusedQyou2y ago
Glad we solved this confusion
oe
oeOP2y ago
My bad So, I guess the tuple is the best solution really? Or this?
FusedQyou
FusedQyou2y ago
I would vote dictionaries as they are more readable and compressed
oe
oeOP2y ago
Nah I think tuple has to be easier Hmm
FusedQyou
FusedQyou2y ago
Because in the end you're working with duplicate keys
oe
oeOP2y ago
But then that would mean more coding >:D
FusedQyou
FusedQyou2y ago
Yes, but sometimes more code is better
oe
oeOP2y ago
true In an enterprise app context I would probably go with dictionary solution (thanks for suggesting) but since im lazy and its a small sized app im going tuple thanks for the help
Angius
Angius2y ago
At least use ValueTuples not the old Tuple<T...>
ChucklesTheBeard
Per the spec: https://www.rfc-editor.org/rfc/rfc9110.html#section-5.2 This is equivalent to having a single header "key" and a comma separated list of values, Accept-Language: en-Us, en-Sp
RFC 9110: HTTP Semantics
The Hypertext Transfer Protocol (HTTP) is a stateless application-level protocol for distributed, collaborative, hypertext information systems. This document describes the overall architecture of HTTP, establishes common terminology, and defines aspects of the protocol that are shared by all versions. In this definition a...
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server