how to convert C# code to Kotlin consist of Json library

Viewed 1304

I'm kotlin newbie and have bad English sorry.

I want to convert to below code to Kotlin.

but I can't find a matching code with [JsonExtensiondata] in kotlin.

public class ProofAttribute
{
    [JsonProperty("raw")]
    public string Raw { get; set; }

    /// <summary>
    /// ignore structural mapping of other properties
    /// </summary>
    [JsonExtensionData]
    public IDictionary<string, JToken> Rest { get; set; }
} 
1 Answers

You can use the Jackson library for serialization/deserialization. If you choose to do so use the @JsonAnyGetter annotation which is described in the docs here:https://github.com/FasterXML/jackson-annotations/wiki/Jackson-Annotations

@JsonAnyGetter: annotation used to define a getter as "any getter", which returns a java.util.Map, contents of which will be serialized as additional properties for JSON Object, along with regular properties that the Object may have.

Check an example:

class Student {
   private Map<String, String> properties;
   public Student(){
      properties = new HashMap<>();
   }
   @JsonAnyGetter
   public Map<String, String> getProperties(){
      return properties;
   }
   public void add(String property, String value){
      properties.put(property, value);
   }
}

The HashMap is the equivalent of the dictionary with similar retrieval times and complexities.

Kotlin definition:

@get:JsonAnyGetter
val details: Map<String, JsonNode>
Related