问题
I am trying to write a converter for a class using JSON.net
When I serialize the object of type JsonObject
, I get the following output
{"DataObject":{"id":"1","name":"data name"}}
How can I move the DataObject
one level up to get the following output:
{"id":"1","name":"data name"}
You can find the relevant code below.
My class has the following format:
public class JsonObject
{
public JsonObject(IDataObject dataObject)
{
this.DataObject= dataObject;
}
[JsonConverter(typeof(JsonDataObjectConverter))]
public IDataObject DataObject;
}
The DataObject
has some properties:
public class MyDataObject : IDataObject
{
[JsonProperty(PropertyName = "id", Required = Required.Always)]
public string Id { get; set; }
[JsonProperty(PropertyName = "name", Required = Required.Always)]
public string Name { get; set; }
}
I referred this page from the documentation and wrote the following converter:
public class JsonDataObjectConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return typeof(IDataObject).GetTypeInfo().IsAssignableFrom(objectType.GetTypeInfo());
}
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)
{
if (value is MyDataObject dataObject)
{
writer.WriteStartObject();
writer.WritePropertyName("id");
writer.WriteValue(dataObject.Id);
writer.WritePropertyName("name");
writer.WriteValue(dataObject.Name);
writer.WriteEndObject();
}
}
}
Any help is appreciated. Thank you.
回答1:
I was able to get the desired output by moving the converter to the class instead of the property and ignoring the property using [JsonIgnore]
. The property needs to be ignored since the converter for the class will generate the JSON for the property as shown below.
So the JsonObject
class will be:
[JsonConverter(typeof(JsonObjectConverter))]
public class JsonObject
{
public JsonObject(IDataObject dataObject)
{
this.DataObject= dataObject;
}
[JsonIgnore]
public IDataObject DataObject;
}
Then create the converter like this:
public class JsonObjectConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return typeof(IDataObject).GetTypeInfo().IsAssignableFrom(objectType.GetTypeInfo());
}
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)
{
JsonObject jsonObject = (JsonObject)value;
if (jsonObject.DataObject.GetType() == typeof(MyDataObject))
{
MyDataObject dataObject = (MyDataObject) jsonObject.DataObject;
writer.WriteStartObject();
writer.WritePropertyName("id");
writer.WriteValue(dataObject.Id);
writer.WritePropertyName("name");
writer.WriteValue(dataObject.Name);
writer.WriteEndObject();
}
}
}
This gives the desired output of:
{"id":"1","name":"data name"}
来源:https://stackoverflow.com/questions/47136461/how-to-move-json-objects-depth-one-level-up-in-json-net-using-c