Reputation: 21
I'm working on a WEB API that returns a JSON file. I want my JSON to like like this :
{
"PensionDistribution": [{
"rows": [{
"uniqueName": "Age"
}]
},
{
"columns": [{
"uniqueName": "Year"
},
{
"uniqueName": "Type"
}]
},
{
"measures": [{
"uniqueName": "AgentID",
"aggregation": "count"
}]
},
{
"fields": {
"Age": {
"type": "number",
"caption": "Age"
},
"AgentComputedCompleteName": {
"type": "string",
"caption": "Nom complet encodé de l'agent"
},
"AgentID": {
"type": "number",
"caption": "ID Agent"
},
"MatriculeAgent": {
"type": "string",
"caption": "Matricule agent"
},
"Type": {
"type": "string",
"caption": "Type"
},
"TypeKey": {
"type": "string",
"caption": "Type clé"
},
"Year": {
"type": "string",
"caption": "Année"
}
}
}]
}
So I created the classes that will convert my database data into my JSON :
public class PivotConfigData
{
public class Report
{
public string Name { get; set; }
public List<Row> Rows { get; set; }
public List<Column> Columns { get; set; }
public List<Measure> Measures { get; set; }
public List<Field> Fields { get; set; }
public Report()
{
Name = "";
Rows = new List<Row>();
Columns = new List<Column>();
Measures = new List<Measure>();
Fields = new List<Field>();
}
}
public class Row
{
public string UniqueName { get; set; }
}
public class Column
{
public string UniqueName { get; set; }
}
public class Measure
{
public string UniqueName { get; set; }
public string Aggregation { get; set; }
}
public class Field
{
public string Name { get; set; }
public FieldAttributes Attributes { get; set; }
}
public class FieldAttributes
{
public string Type { get; set; }
public string Caption { get; set; }
}
}
By using this I get a JSON file like this :
{
"Name": "PensionDistribution",
"Rows": [{
"UniqueName": "Age"
}],
"Columns": [{
"UniqueName": "Year"
},
{
"UniqueName": "Type"
}],
"Measures": [{
"UniqueName": "AgentID",
"Aggregation": "count"
}],
"Fields": [{
"Name": "Age",
"Attributes": {
"Type": "number",
"Caption": "Age"
}
},
{
"Name": "AgentComputedCompleteName",
"Attributes": {
"Type": "string",
"Caption": "Nom complet encodé de l'agent"
}
},
{
"Name": "AgentID",
"Attributes": {
"Type": "number",
"Caption": "ID Agent"
}
},
{
"Name": "MatriculeAgent",
"Attributes": {
"Type": "string",
"Caption": "Matricule agent"
}
},
{
"Name": "Type",
"Attributes": {
"Type": "string",
"Caption": "Type"
}
},
{
"Name": "TypeKey",
"Attributes": {
"Type": "string",
"Caption": "Type clé"
}
},
{
"Name": "Year",
"Attributes": {
"Type": "string",
"Caption": "Année"
}
}]
}
As you can see, it added the "Name": before "PensionDistribution" but that's not a big deal. It's an issue for the "Fields" collection, as it adds the "Name" and "Attributes", how can I delete or hide those property names ? I tried to add [JsonProperty("")] before my property but it replaces "Name": by "": which is not what I wanted.
Can anybody help me please ?
Upvotes: 2
Views: 4745
Reputation: 35544
Instead of using the Attributes from JSON.NET (that makes your code dependant on it) you can use the normal serialization attributes of the .NET Framework (DataMemberAttribute
, DataContractAttribute
, etc.).
To ignore/remove properties from JSON you can decorate them with the IgnoreDataMemberAttribute
.
[DataContract]
public class Field
{
[IgnoreDataMember]
public string Name { get; set; }
[DataMember]
public FieldAttributes Attributes { get; set; }
}
Upvotes: 1
Reputation: 5764
Use on unwanted properties attribute JsonIgnore.
For change property name use [JsonProperty(PropertyName = "NewName")]
.
Upvotes: 1