Closed ycrumeyrolle closed 3 years ago
You have to use an array initializer with braces instead of brackets, in addition to a a new JwtArray
like this:
jwsDescriptor.AddClaim("ClaimName", new JwtObject
{
{ "stuff1", "xyz789" },
{ "stuff2", "abc123" },
{ "subObject" , new JwtObject
{
{ "prop1" , "abc123" },
{ "prop2" , "xyz789" }
}
},
{ "Modules" , new JwtArray
{
"a", "b", 3
}
}
});
Version 2.0 will support array initialization without requiring to instanctiate a new JwtArray (this class will be deprecated)
Good to know about the JwtArray, I will avoid using it.
Not sure if you fully understood, what I was trying to do,
I am trying to create an Array of JWT Objects, a Nested Array. as below is standard JSON notation with [ ].
jwsDescriptor.AddClaim("ClaimName", new JwtObject
{
{ "stuff1", "xyz789" },
{ "stuff2", "abc123" },
{ "subObject" , new JwtObject
{
{ "prop1" , "abc123" },
{ "prop2" , "xyz789" }
}
},
{ "Modules" , [
{
{ "name" , "module1" },
{ "prop1" , "abc123" },
{ "prop2" , "xyz789" }
},
{
{ "name" , "module2" },
{ "prop1" , "abc123" },
{ "prop2" , "xyz789" }
}
]
}
});
You are in C#, you cannot use JSON notation.
I know :( , is there any way to achieve the above currently (Array of JWT Objects, a Nested Array) or is that the enhancement?
This is probably not ideal but...:
jwsDescriptor.AddClaim("ClaimName", new JwtObject
{
{ "stuff1", "xyz789" },
{ "stuff2", "abc123" },
{
"subObject" , new JwtObject
{
{ "prop1" , "abc123" },
{ "prop2" , "xyz789" }
}
},
{
"Modules" , new JwtArray(new List<JwtValue>()) {
new JwtObject{
{ "name" , "module1" },
{ "prop1" , "abc123" },
{ "prop2" , "xyz789" }
},
new JwtObject{
{ "name" , "module2" },
{ "prop1" , "abc123" },
{ "prop2" , "xyz789" }
}
}
}
});
It produce:
"ClaimName": {
"stuff1": "xyz789",
"stuff2": "abc123",
"subObject": {
"prop1": "abc123",
"prop2": "xyz789"
},
"Modules": [
{
"name": "module1",
"prop1": "abc123",
"prop2": "xyz789"
},
{
"name": "module2",
"prop1": "abc123",
"prop2": "xyz789"
}
]
}
Perfect
Fixed in #509, available in package version 2.0.0-beta.1
JsonObject
replaces JwtObject
. Arrays are now supported, JwtArray
is not necessary anymore.
Otherwise, JSON serialization of POCO is now also supported aswell as anonymous types: https://github.com/uruk-project/Jwt/blob/1c98645df6fbd76d5ad3ac4b1075563db519bed4/samples/JwsCreationSample/Program.cs#L62-L84
Slightly different, put potentially related, I am trying to add an Array of JwtObject to a claim
Everything I have tried so far hasn't worked, you don't seam to be able to have a JwtArray of JwtObjects?
The closest I got is below.