✅ Best way to return Tuple<string, string> as an Array?
I have an interface called
IRequest
, with Tuple lists called:
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
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?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)
Dictionary<string, string[]>
?Oh wait, can you repeat keys in dictionaries?
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
Like a multideminsional array
Is what I was thinking
Of what type?
But using this solution means I can't repeat key values right? What if there's 2 repeated http header names
String
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
Yeah, but sometimes you have 2 HTTP headers with different values but the same name/key
That's what I said lol
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
The key of the dictionary entry is the key of the header, and the values are stored in the array
Hmm, maybe I explained this badly.
One sec
This is not required because you know the length of your tuple
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
A D I C T I O N A R Y
This is literally a dictionary
Ok wait I migth be being stupid right now but
In a dictionary you cant have 2 keys with the same value
I don't get why you want to store them individually
So how would I have 2 entries of Accept-Language with that rule
Because that's what I want
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
Would converting the tuple to a multidimensional array make it easier to iterate through those headers in a non C# context?
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
Ahh!
Ok
Glad we solved this confusion
My bad
So, I guess the tuple is the best solution really?
Or this?
I would vote dictionaries as they are more readable and compressed
Nah I think tuple has to be easier
Hmm
Because in the end you're working with duplicate keys
But then that would mean more coding >:D
Yes, but sometimes more code is better
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
At least use ValueTuples not the old
Tuple<T...>
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...
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.