简体   繁体   中英

c# json serialize with attributes

I like to write a .net connector to Eureka register service using rest api. It asks for a json format as below and would like to generate that json from configuration class. Could not find out how to serialize some properties like attributes which looks like $, and @ at the beginnnig in the json like "securePort": {"$": "8443", "@enabled": "true"},

the json needed for eureka:

{
    "instance": {
        "hostName": "WKS-SOF-L011",
        "app": "com.automationrhapsody.eureka.app",
        "vipAddress": "com.automationrhapsody.eureka.app",
        "secureVipAddress": "com.automationrhapsody.eureka.app"
        "ipAddr": "10.0.0.10",
        "status": "STARTING",
        "port": {"$": "8080", "@enabled": "true"},
        "securePort": {"$": "8443", "@enabled": "true"},
        "healthCheckUrl": "http://WKS-SOF-L011:8080/healthcheck",
        "statusPageUrl": "http://WKS-SOF-L011:8080/status",
        "homePageUrl": "http://WKS-SOF-L011:8080",
        "dataCenterInfo": {
            "@class": "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo", 
            "name": "MyOwn"
        },
    }
}

my class thats expected generate the json below after serialization(i use newtonsoft.json):

public class Port
{
    public string PortName { get; set; }
    [JsonProperty("enabled")]
    public bool Enabled { get; set; }
}

public class DataCenterInfo
{
    [JsonProperty("class")]
    public string Class { get; set; }
    [JsonProperty("name")]
    public string Name { get; set; }
}

public class EurekaRegisterParams
{
    [JsonProperty("instanceId")]
    public string InstanceId { get; set; }
    [JsonProperty("hostName")]
    public string HostName { get; set; }
    [JsonProperty("app")]
    public string App { get; set; }
    [JsonProperty("ipAddr")]
    public string IpAddr { get; set; }
    [JsonProperty("status")]
    public string Status { get; set; }
    [JsonProperty("port")]
    public Port Port { get; set; }
    [JsonProperty("securePort")]
    public Port SecurePort { get; set; }
    [JsonProperty("countryId")]
    public string CountryId { get; set; }
    [JsonProperty("dataCenterInfo")]
    public DataCenterInfo DataCenterInfo { get; set; }
    [JsonProperty("homePageUrl")]
    public string HomePageUrl { get; set; }
    [JsonProperty("statusPageUrl")]
    public string StatusPageUrl { get; set; }
    [JsonProperty("healthCheckUrl")]
    public string HealthCheckUrl { get; set; }
}

You are very close to the solution. You could use the JsonProperty attribute to do your job as you have already done with the rest of the properties.

public class Port
{
    [JsonProperty("$")]
    public string PortName { get; set; }
    [JsonProperty("@enabled")]
    public bool Enabled { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM