C
C#2y ago
arch_il

✅ Why can't I deserialize list of tuples?

I Got List<bool, string> inside my code. This is how I wrote it down in Json.
"texts": [
{
"Item1": true,
"Item2": "hello"
},
{
"Item1": false,
"Item2": "hi"
}
]
"texts": [
{
"Item1": true,
"Item2": "hello"
},
{
"Item1": false,
"Item2": "hi"
}
]
But bool is always false and string is always equal to null. Is it wrong format or something?
13 Replies
MODiX
MODiX2y ago
ero
REPL Result: Success
var json = """
[
{
"Item1": false,
"Item2": "foo"
},
{
"Item1": true,
"Item2": "bar"
}
]
""";
System.Text.Json.JsonSerializer.Deserialize<List<(bool, string)>>(json)
var json = """
[
{
"Item1": false,
"Item2": "foo"
},
{
"Item1": true,
"Item2": "bar"
}
]
""";
System.Text.Json.JsonSerializer.Deserialize<List<(bool, string)>>(json)
Result: List<ValueTuple<bool, string>>
[
{
"item1": false,
"item2": null
},
{
"item1": false,
"item2": null
}
]
[
{
"item1": false,
"item2": null
},
{
"item1": false,
"item2": null
}
]
Compile: 564.776ms | Execution: 45.109ms | React with ❌ to remove this embed.
arch_il
arch_ilOP2y ago
same problem with me
ero
ero2y ago
fun
arch_il
arch_ilOP2y ago
wait. I think it should be item1 instead of Item1
arch_il
arch_ilOP2y ago
nope. still same issue
ero
ero2y ago
valuetuples just don't work with serialization i guess
MODiX
MODiX2y ago
ero
REPL Result: Success
List<(bool, string)> tuples = new()
{
(false, "foo"),
(true, "bar")
};

System.Text.Json.JsonSerializer.Serialize(tuples)
List<(bool, string)> tuples = new()
{
(false, "foo"),
(true, "bar")
};

System.Text.Json.JsonSerializer.Serialize(tuples)
Result: string
[{},{}]
[{},{}]
Compile: 537.975ms | Execution: 51.936ms | React with ❌ to remove this embed.
arch_il
arch_ilOP2y ago
i better create a small class with just bool and string no other option here are there some built in things like tuples?
ero
ero2y ago
record R(bool B, string S);
MODiX
MODiX2y ago
ero
REPL Result: Success
List<(bool, string)> tuples = new()
{
(false, "foo"),
(true, "bar")
};

System.Text.Json.JsonSerializer.Serialize(tuples, new JsonSerializerOptions { IncludeFields = true })
List<(bool, string)> tuples = new()
{
(false, "foo"),
(true, "bar")
};

System.Text.Json.JsonSerializer.Serialize(tuples, new JsonSerializerOptions { IncludeFields = true })
Result: string
[{"Item1":false,"Item2":"foo"},{"Item1":true,"Item2":"bar"}]
[{"Item1":false,"Item2":"foo"},{"Item1":true,"Item2":"bar"}]
Compile: 599.487ms | Execution: 60.277ms | React with ❌ to remove this embed.
MODiX
MODiX2y ago
Angius
REPL Result: Success
using System.Text.Json;

record Thing(bool Item1, string Item2);

var json = """
[
{
"Item1": false,
"Item2": "foo"
},
{
"Item1": true,
"Item2": "bar"
}
]
""";
JsonSerializer.Deserialize<List<Thing>>(json)
using System.Text.Json;

record Thing(bool Item1, string Item2);

var json = """
[
{
"Item1": false,
"Item2": "foo"
},
{
"Item1": true,
"Item2": "bar"
}
]
""";
JsonSerializer.Deserialize<List<Thing>>(json)
Result: List<Thing>
[
{
"item1": false,
"item2": "foo"
},
{
"item1": true,
"item2": "bar"
}
]
[
{
"item1": false,
"item2": "foo"
},
{
"item1": true,
"item2": "bar"
}
]
Compile: 680.984ms | Execution: 86.612ms | React with ❌ to remove this embed.
ero
ero2y ago
:/ Item1 and Item2 are fields
arch_il
arch_ilOP2y ago
thanks i would have thought to do that
Want results from more Discord servers?
Add your server