I have a class, that has an object:
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34283")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=true)]
public partial class Scores : baseModel
{
private System.Xml.XmlAttribute[] anyAttrField;
/// <remarks/>
[System.Xml.Serialization.XmlAnyAttributeAttribute()]
public System.Xml.XmlAttribute[] AnyAttr
{
get
{
return this.anyAttrField;
}
set
{
this.anyAttrField = value;
}
}
}
From the parent class (snippet of it):
public parial class LandingPage : baseModel
{
private string projectNameField;
private Scores scoresField;
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string projectName
{
get { return this.projectNameField; }
set { this.projectNameField = value; }
}
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public Scores scores
{
get { return this.scoresField; }
set { this.scoresField = value }
}
}
The JSON string I'm trying to work with:
{
"projectName":"PROJECTTEST",
"scores":{
"browLocker":100,
"heavyAd":0,
"walletRedirection":0
}
}
NewtonSoft.JsonConvert ignores the scores child fields...
How can I easily convert this to work?
Given that your Scores
type has no properties of its own to serialize, you can create a custom JsonConverter
for it that converts the XmlAttribute []
array to a Dictionary<string, string>
then serializes that dictionary in place of the Scores
object:
public class XmlAttributeArrayConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(XmlAttribute[]);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
return null;
var dict = serializer.Deserialize<Dictionary<string, string>>(reader);
var doc = new XmlDocument();
return dict.Select(p => { var a = doc.CreateAttribute(p.Key); a.Value = p.Value; return a; }).ToArray();
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
// TODO: determine how to represent XmlAttribute values with non-empty NamespaceURI - or throw an exception.
var attributes = (IEnumerable<XmlAttribute>)value;
var dict = attributes.ToDictionary(a => a.Name, a => a.Value);
serializer.Serialize(writer, dict);
}
}
class ScoresConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(Scores);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
return null;
var attributes = (XmlAttribute[])new XmlAttributeArrayConverter().ReadJson(reader, typeof(XmlAttribute[]), null, serializer);
return new Scores { AnyAttr = attributes };
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var scores = (Scores)value;
if (scores.AnyAttr == null)
writer.WriteNull();
else
{
new XmlAttributeArrayConverter().WriteJson(writer, scores.AnyAttr, serializer);
}
}
}
(Note I extracted some of the logic into a separate XmlAttributeArrayConverter
in case it might be of use elsewhere.)
Then apply it to Scores
as follows:
[JsonConverter(typeof(ScoresConverter))]
public partial class Scores
{
}
Sample fiddle .
Note if one of the XML attributes happens to be in a namespace (most of the time none will be ) then the namespace is not serialized. If your attributes might have namespaces you will need to decide how to convert the attribute's full name to a JSON property name. {namespace}localname
would be one possibility, that is what is used by XName.ToString()
.
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.