0

It's part of AuthData class:

[Serializable]
public class AuthData :IAuthData
{
   
    [JsonProperty("USER_ROLES"), DataMember(Name = "USER_ROLES")]
    public String roles { get; protected set; }

    [JsonProperty("USER_ATTRIBUTES"), DataMember(Name = "USER_ATTRIBUTES")]
    public IUserAttributes userAttributes { get; protected set; }

It's UserAttributes class:

[Serializable]
public class UserAttributes : IUserAttributes
{

    private static long serialVersionUID = 1L;

    private static Gson gson = new Gson();

    //[JsonProperty("")]
    private List<Dictionary<string, List<JValue>>> tuples = new List<Dictionary<string, List<JValue>>>();

Serialized object:

    "USER_ROLES": " sysadmin", "USER_ATTRIBUTES": {
    "tuples": [{
        "Department": [""],
        "Colors": ["blue", "green", "red"],
        "City": ["RamatGan"]
    }]
}...

I need the serialized object to be:

    "USER_ROLES": " sysadmin", "USER_ATTRIBUTES": [{
    "Department": [""],
    "Colors": ["blue", "green", "red"],
    "City": ["RamatGan"]
}]
}...

How can I do it?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
R_B
  • 41
  • 5
  • Please format your question properly – DavidG Jun 21 '23 at 09:11
  • can you please explain what is not formatted well? – R_B Jun 21 '23 at 09:16
  • 1
    You have `enter code here` still in the post, you have code not part of the code block, your JSON is not complete, etc. Please spent at least a few moments to look ar your post, it is painfully obvious. – DavidG Jun 21 '23 at 09:22
  • json is not completed because it's VERY long, no value to put it as is. The purpose is just to show the "USER_ATTRIBUTES" part. Regard to [Serializable] lines that are not formatted as code - I don't succeed to fix it. it seems as a part of code in the edition. – R_B Jun 21 '23 at 09:38
  • Thanks @DavidG, now I hope somebody to help :) – R_B Jun 21 '23 at 10:06
  • Does this answer your question? [Deserialize json with known and unknown fields](https://stackoverflow.com/questions/15253875/deserialize-json-with-known-and-unknown-fields) – Charlieface Jun 21 '23 at 10:23
  • Use the `[JsonExtensionData]` attribute on `tuples` – Charlieface Jun 21 '23 at 10:23
  • @Charlieface I think it is not supported for .Net framework 4.5 – R_B Jun 21 '23 at 18:14
  • Not sure what you mean, Json.Net v13 supports even .NET Framework 2.0 – Charlieface Jun 21 '23 at 18:39

1 Answers1

0

Solution: Define a custom converter and return only the required property.

[JsonConverter(typeof(UserAttributesConverter))] public class

UserAttributes : IUserAttributes {

    private static long serialVersionUID = 1L;

    private static Gson gson = new Gson();

    private List<Dictionary<string, List<JValue>>> tuples = new List<Dictionary<string, List<JValue>>>();

And the UserAttributesConverter class:

public class UserAttributesConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        throw new NotImplementedException();
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        writer.WriteRawValue(JsonConvert.SerializeObject(((UserAttributes)value).getTuples()));
    }
}
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
R_B
  • 41
  • 5