假设我们要将类Request
序列化为json并从json反序列化。
class Request {
public string SessionId { get; set; }
...
public string InnerJson { get; set; }
}
作为json,它看起来应该是
{
"SessionId": 1,
...
"InnerJson": {
"some": "json object",
"whatever": 666
}
}
InnerJson
是一些json文档(任意类型)。
在string
中InnerJson
使用Request
是否合适?
有没有什么好方法可以设计Request
类?
答案 0 :(得分:0)
如果你想要一个强类型模型,我建议一个工厂。为了示范:
public abstract class AbstractOptions { }
public class Options1 : AbstractOptions { public int Whatever { get; set; } }
public class Options2 : AbstractOptions { public string Some { get; set; } }
public class Options3 : AbstractOptions {
[JsonProperty("when")] public DateTime When { get; set; }
[JsonProperty("inner")] public InnerComplexObject Inner { get; set; }
}
public class Request {
[JsonProperty("session-id")] public string SessionId { get; set; }
[JsonProperty("options")] public AbstractOptions Options { get; set; }
}
public class InnerComplexObject { }
然后使用它:
var req1 = new Request() { SessionId = "s1", Options = new Options1 { Whatever = 123 } };
var req2 = new Request() { SessionId = "s2", Options = new Options2 { Some = "some" } };
var req3 = new JToken.Request() { SessionId = "s3", Options = new Options3 { When = DateTime.UtcNow, Inner = new InnerComplexObject() } };
否则,为了保持灵活性,请InnerJson
string
和use dynamic queries保持pff <- read.table(header=TRUE, text='REGION Q99
Tunis NidaaTounes
Tunis Nepasvoter
Tunis Nahdha
Tunis Nepasvoter
Tunis Nahdha
Tunis Nahdha
Tunis NidaaTounes
Tunis Jabha
Tunis NidaaTounes
Tunis Autres
Tunis Nahdha
Tunis Nahdha
Tunis Autres
Tunis Jabha
Tunis Nepasvoter
Tunis Nepasvoter
Tunis CPR
Tunis Nahdha
Tunis Nepasvoter
Tunis Nepasvoter
Ariana Nahdha
Ariana Nepasvoter
Ariana NidaaTounes
Ariana CPR
Ariana NidaaTounes
Ariana NidaaTounes
Ariana NidaaTounes
Ariana CPR
Ariana Nahdha', stringsAsFactors=FALSE)
g <- ggplot(pff, aes(x = REGION, y =(..count..)/sum(..count..), fill=Q99))
g <- g + geom_bar(position="stack") +
labs(title="Vote par région") +
labs(x=" ", y=" ")+
labs(fill="Parti Politique")+
coord_flip()+
# scale_fill_manual(values=cbPalette)+
scale_y_continuous(labels = percent)+
theme_bw()+
theme(panel.border = element_rect(colour = "white")) +
geom_text(aes( label = scales::percent((..count..)/sum(..count..)),
y=(..count..)/sum(..count..)), stat= "count", size=3)
。