I have some XML in the following format:
<ObjectData>
<ModelName>MODEL_123</ModelName>
<ObjectName>OBJECT_A</ObjectName>
<Values>
<KeyValuePair>
<Key>NAME</Key>
<Value>PAUL</Value>
</KeyValuePair>
...
</Values>
</ObjectData>
...
I want to deserialize this into the following class:
[XmlRoot(Namespace = "")]
public class ObjectData
{
[XmlElement(Namespace = "")]
public string ModelName { get; set; }
[XmlElement(Namespace = "")]
public string ObjectName { get; set; }
[XmlArray]
public List<KeyValuePair<string, string>> Values { get; set; }
}
When I use this code, the KeyValuePairs are not deserialized and the Values property is empty.
List<ObjectData> data = new List<ObjectData>();
XmlSerializer serializer = new XmlSerializer(typeof(ObjectData));
using (XmlReader reader = XmlReader.Create(new StringReader(inputXML)))
{
reader.MoveToContent();
ObjectData temp = (ObjectData)serializer.Deserialize(reader);
data.Add(temp);
}
Is the KeyValuePair class not serializable in the way I use it? Or is there a problem in my ObjectData class?
Try specifying the element names in your attributes:
[XmlArray("Values")]
[XmlArrayItem("KeyValuePair")]
public List<KeyValuePair<string, string>> Values { get; set; }
Combining D Stanley's answer with this post, I was able to come up with the right structure:
[XmlRoot(Namespace = "")]
public class ObjectData
{
[XmlElement(Namespace = "")]
public string ModelName { get; set; }
[XmlElement(Namespace = "")]
public string ObjectName { get; set; }
[XmlArray("Values")]
[XmlArrayItem("KeyValuePair")]
public List<KeyValuePair<string, string>> Values { get; set; }
}
[Serializable]
public class KeyValuePair<K, V> {
public K Key { get; set; }
public V Value { get; set; }
public KeyValuePair() { }
public KeyValuePair(K key, V value)
{
this.Key = key;
this.Value = value;
}
}
There is no setter for Key or Value in the KeyValuePair struct. You'll have to change the data type. You can decorate the property so you can name the new type whatever you want.
....
[XmlArray("Values")]
[XmlArrayItem("KeyValuePair")] //not needed if MyItem is named KeyValuePair
public List<MyItem> Values { get; set; }
}
public class MyItem
{
public string Key { get; set; }
public string Value { get; set; }
}
Related
So I'm making a small JSON Parser.
This is the JSON I want to parse:
{"158023":{"prices":{"xbox":{"LCPrice":"225,000","LCPrice2":"232,000","LCPrice3":"235,000","LCPrice4":"235,000","LCPrice5":"239,000","updated":"15 secs ago","MinPrice":"27,000","MaxPrice":"500,000","PRP":"41"},"ps":{"LCPrice":"228,000","LCPrice2":"231,000","LCPrice3":"232,000","LCPrice4":"233,000","LCPrice5":"235,000","updated":"9 mins ago","MinPrice":"30,000","MaxPrice":"550,000","PRP":"38"},"pc":{"LCPrice":"305,000","LCPrice2":"305,000","LCPrice3":"315,000","LCPrice4":"333,000","LCPrice5":"347,000","updated":"1 hour ago","MinPrice":"37,000","MaxPrice":"700,000","PRP":"40"}}}}
And I have the following class, to represent the Json Object.
public partial class Prices
{
[JsonProperty("158023")]
public Token TokenNumber { get; set; }
}
public partial class Token
{
[JsonProperty("prices")]
public PricesClass Prices { get; set; }
}
public partial class PricesClass
{
[JsonProperty("xbox")]
public Pc Xbox { get; set; }
[JsonProperty("ps")]
public Pc Ps { get; set; }
[JsonProperty("pc")]
public Pc Pc { get; set; }
}
public partial class Pc
{
[JsonProperty("LCPrice")]
public string LcPrice { get; set; }
[JsonProperty("LCPrice2")]
public string LcPrice2 { get; set; }
[JsonProperty("LCPrice3")]
public string LcPrice3 { get; set; }
[JsonProperty("LCPrice4")]
public string LcPrice4 { get; set; }
[JsonProperty("LCPrice5")]
public string LcPrice5 { get; set; }
[JsonProperty("updated")]
public string Updated { get; set; }
[JsonProperty("MinPrice")]
public string MinPrice { get; set; }
[JsonProperty("MaxPrice")]
public string MaxPrice { get; set; }
[JsonProperty("PRP")]
public string Prp { get; set; }
}
public partial class Prices
{
public static Prices FromJson(string json) => JsonConvert.DeserializeObject<Prices>(json, PriceConverter.Settings);
}
internal static class PriceConverter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
Converters = {
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
},
};
}
I'm easily able to parse the JSON, by doing:
Prices prices = Prices.FromJson(myJson);
The problem is when I want to use a different number than 158023.
For example, 158025.
The JsonProperty on the Prices class is already set to "158023", and I have no clue how to rename it.
TLDR:
I have a JSON Object, which I want to rename the JsonProperty text, before deserializing.
Since you don't know the key, use a Dictionary<string, Token> instead of the property TokenNumber in class Prices.
public partial class Prices
{
// Remove this property
// [JsonProperty("158023")]
// public Token TokenNumber { get; set; }
}
public partial class Prices
{
public static Dictionary<string, Token> FromJson(string json) => JsonConvert.DeserializeObject<Dictionary<string, Token>>(json, PriceConverter.Settings);
}
Now the result will be a dictionary where the key is the token value as an string and the value is a Token object.
You could make use of the JsonExtensionData and OnDeserialized attributes:
public class Wrapper
{
public string Id { get; set; }
public Item Item { get; set; }
[JsonExtensionData]
private IDictionary<string, JToken> _additionalData;
[OnDeserialized]
private void OnDeserialized(StreamingContext context)
{
// Get the first key. If you have more than one, you may need
// to customize this for your use case
var id = _additionalData.Keys.FirstOrDefault();
if (id != null)
{
// Assign to our Id property
this.Id = id;
// Create a reader for the subobject
var itemReader = _additionalData[id].CreateReader();
var serializer = new JsonSerializer();
// Deserialize the subobject into our Item property
this.Item = serializer.Deserialize<Item>(itemReader);
itemReader.Close();
}
}
}
public class Item
{
public string Name { get; set; }
}
You can try it here. Alternatively you could write a JsonConverter to achieve the same thing, or do what Amir suggested.
I want to Deserialize and get values of 2 attributes with different ID.
<Attributes><AddressAttribute ID="18"><AddressAttributeValue><Value>Sala 305</Value></AddressAttributeValue></AddressAttribute><AddressAttribute ID="17"><AddressAttributeValue><Value>3434</Value></AddressAttributeValue></AddressAttribute></Attributes>
I treid this C# code but it only returns the 1st attribute.
please help
[XmlRoot(ElementName = "AddressAttributeValue")]
public class AddressAttributeValue
{
[XmlElement(ElementName = "Value")]
public string Value { get; set; }
}
[XmlRoot(ElementName = "AddressAttribute")]
public class AddressAttribute
{
[XmlElement(ElementName = "AddressAttributeValue")]
public AddressAttributeValue AddressAttributeValue { get; set; }
[XmlAttribute(AttributeName = "ID")]
public string ID { get; set; }
}
[XmlRoot(ElementName = "Attributes")]
public class Attributes
{
[XmlElement(ElementName = "AddressAttribute")]
public AddressAttribute AddressAttribute { get; set; }
}
var xmlData= customer.BillingAddress.CustomAttributes;
XmlSerializer serializer = new XmlSerializer(typeof(Attributes));
Attributes data;
using (TextReader reader = new StringReader(xmlData))
{
data = (Attributes)serializer.Deserialize(reader);
}
Should I change classes of Deserialize logic???
Sometimes using Linq instead of xml serialization can be simpler
var list = XDocument.Parse(xmlstring).Descendants("AddressAttribute")
.Select(x => new
{
Id = (int)x.Attribute("ID"),
Value = (string)x.Element("AddressAttributeValue").Element("Value")
})
.ToList();
[XmlRoot(ElementName = "Attributes")]
public class Attributes
{
[XmlElement(ElementName = "AddressAttribute")]
public AddressAttribute AddressAttribute { get; set; }
}
Change it to:
[XmlRoot(ElementName = "Attributes")]
public class Attributes
{
[XmlElement(ElementName = "AddressAttribute")]
public AddressAttribute[] AddressAttribute { get; set; }
}
As you need to have collection of AddressAttribute you need to declare as an array.
This is probably something really simple and I looked everywhere and tried everything I could come up with. So I apologize if this is a simple search and I was just looking for the wrong thing. I'm also new to data contracts and somewhat JSON so this probably isn't really that complex.
I am creating an API to ingest JSON and store it in our database.
The JSON will look something like this:
{
"appname" : "MyApp",
"key" : "Test123",
"data" :[
{ "data1" : "10551296", "data2" : "TrainingIns", "data3" : "Completed"}
,
{ "connectorType" : "webserver-to-appserver", "sourceUri" : "data4", "destinationUri" : "data5", "rails" : "N", "data6" : "null" }
,
{ "groupId" : "group1", "failbackAction" : "null", "normal" : "null", "failoverAction" : "null", "failbackAction" : "null", "failoverAction" : "null", "artifactId" : "mywebserver", "normalState" : "null" }
,
{ "instanceId" : "10551296abc" }]
,
"updateId" : "MyID",
"updateTS" : "30-AUG-16 05.56.24.000000000 AM" ,
"creationUser" : "APICall"
}
Where the 'data' field will be an array with a variable amount of JSON objects. The issue I am having stems from either not getting data in the 'data' object or having it be completely undefined.
[DataContract]
public class Update_DB
{
[DataMember(Name = "appname", IsRequired = true)]
public string appname { get; set; }
[DataMember]
public string key { get; set; }
[DataMember(Name="data",IsRequired = true)]
public List<JsonValue> data { get; set; }
[DataMember]
public string updateId { get; set; }
[DataMember]
public string updateTS { get; set; }
[DataMember]
public string creationUser { get; set; }
}
I've gathered I might need a collection of some sort? I've tried everything I could find but I don't know how I should define the data member for 'data'.
The above contract gives me empty arrays when I do this:
string x = JsonConvert.SerializeObject(collection.data);
I can get every other field I just need to turn the 'data' field into a string.
Hopefully that is enough info. Thanks in advance for any help!
Under normal circumstances, you could define your data property as a List<Dictionary<string, string>>, like so:
[DataMember(Name = "data", IsRequired = true)]
public List<Dictionary<string, string>> data { get; set; }
Then you would be able to serialize and deserialize it successfully with Json.NET. Unfortunately, one of your data objects has duplicated keys:
{
"groupId":"group1",
"failbackAction":"null",
"normal":"null",
"failoverAction":"null",
"failbackAction":"null",
"failoverAction":"null",
"artifactId":"mywebserver",
"normalState":"null"
},
Using duplicated keys is not recommended by the JSON standard, which states:
When the names within an object are not unique, the behavior of software that receives such an object is unpredictable.
In addition, c# dictionaries of course do not support duplicated keys, and data contract serialization does not duplicated property names.
However, it is possible to read a JSON object with duplicated keys using Json.NET's JsonReader and create a custom JsonConverter to handle duplicated keys.
First, define the following class to replace JsonValue. JsonValue is a silverlight-specific class whose use has been deprecated in overall .Net:
[JsonConverter(typeof(JsonValueListConverter))]
public sealed class JsonValueList
{
public JsonValueList()
{
this.Values = new List<KeyValuePair<string, string>>();
}
public List<KeyValuePair<string, string>> Values { get; private set; }
}
class JsonValueListConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return typeof(JsonValueList).IsAssignableFrom(objectType);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
return null;
var jsonValue = (existingValue as JsonValueList ?? new JsonValueList());
if (reader.TokenType != JsonToken.StartObject)
throw new JsonSerializationException("Invalid reader.TokenType " + reader.TokenType);
while (reader.Read())
{
switch (reader.TokenType)
{
case JsonToken.Comment:
break;
case JsonToken.PropertyName:
{
var key = reader.Value.ToString();
if (!reader.Read())
throw new JsonSerializationException(string.Format("Missing value at path: {0}", reader.Path));
var value = serializer.Deserialize<string>(reader);
jsonValue.Values.Add(new KeyValuePair<string, string>(key, value));
}
break;
case JsonToken.EndObject:
return jsonValue;
default:
throw new JsonSerializationException(string.Format("Unknown token {0} at path: {1} ", reader.TokenType, reader.Path));
}
}
throw new JsonSerializationException(string.Format("Unclosed object at path: {0}", reader.Path));
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var jsonValue = (JsonValueList)value;
writer.WriteStartObject();
foreach (var pair in jsonValue.Values)
{
writer.WritePropertyName(pair.Key);
writer.WriteValue(pair.Value);
}
writer.WriteEndObject();
}
}
Notice the use of [JsonConverter(typeof(JsonValueListConverter))]. This specifies the use of a custom converter when serializing and deserializing JsonValueList.
Next, define your Update_DB class as follows:
[DataContract]
public class Update_DB
{
[DataMember(Name = "appname", IsRequired = true)]
public string appname { get; set; }
[DataMember]
public string key { get; set; }
[DataMember(Name = "data", IsRequired = true)]
public List<JsonValueList> data { get; set; }
[DataMember]
public string updateId { get; set; }
[DataMember]
public string updateTS { get; set; }
[DataMember]
public string creationUser { get; set; }
}
Now you will be able to serialize and deserialize your JSON successfully. Sample fiddle.
Update
If you do not have duplicated keys, you can define your class as follows:
[DataContract]
public class Update_DB
{
[DataMember(Name = "appname", IsRequired = true)]
public string appname { get; set; }
[DataMember]
public string key { get; set; }
[DataMember(Name = "data", IsRequired = true)]
public List<Dictionary<string, string>> data { get; set; }
[DataMember]
public string updateId { get; set; }
[DataMember]
public string updateTS { get; set; }
[DataMember]
public string creationUser { get; set; }
}
And then the following:
var collection = new Update_DB
{
data = new List<Dictionary<string, string>>
{
new Dictionary<string, string>
{
{"data1", "10551296"},
{"data2", "TrainingIns"},
{"data3", "Completed"},
},
new Dictionary<string, string>
{
{"connectorType", "webserver-to-appserver"},
{"sourceUri", "data4"},
{"destinationUri", "data5"},
},
},
};
string x = JsonConvert.SerializeObject(collection.data, Formatting.Indented);
Console.WriteLine(x);
Produces the output:
[
{
"data1": "10551296",
"data2": "TrainingIns",
"data3": "Completed"
},
{
"connectorType": "webserver-to-appserver",
"sourceUri": "data4",
"destinationUri": "data5"
}
]
Sample fiddle.
Another option is to use the dynamic keyword. You could use a list of this type for data (per below).
[DataContract]
public class Update_DB
{
[DataMember(Name = "appname", IsRequired = true)]
public string appname { get; set; }
[DataMember]
public string key { get; set; }
[DataMember(Name = "data", IsRequired = true)]
public List<dynamic> data { get; set; }
[DataMember]
public string updateId { get; set; }
[DataMember]
public string updateTS { get; set; }
[DataMember]
public string creationUser { get; set; }
}
From there, you could use the object by deserializing with JSON.Net, and access into the dynamic data object (assuming you know something about the shape of this dynamic object). Something like below will work based on the input string from the original post.
Update_DB dataObj = JsonConvert.DeserializeObject<Update_DB>(objectJSON);
string test = dataObj.data[1].connectorType; //evaluates to "webserver-to-appserver"
Use Json2CSharp.com to make sure you have everything correct:
public class Datum
{
public string data1 { get; set; }
public string data2 { get; set; }
public string data3 { get; set; }
public string connectorType { get; set; }
public string sourceUri { get; set; }
public string destinationUri { get; set; }
public string rails { get; set; }
public string data6 { get; set; }
public string groupId { get; set; }
public string failbackAction { get; set; }
public string normal { get; set; }
public string failoverAction { get; set; }
public string artifactId { get; set; }
public string normalState { get; set; }
public string instanceId { get; set; }
}
public class RootObject
{
public string appname { get; set; }
public string key { get; set; }
public List<Datum> data { get; set; }
public string updateId { get; set; }
public string updateTS { get; set; }
public string creationUser { get; set; }
}
In my case I've a XMLTAG <phrase> that can contains one or more XMLTAG <q> but they can appear in phraseTag value in a random order.
Here it is an example of xmlcode:
<phrase level="1">
Where
<q>are</q>
<subject>you</subject>
<q>going?</q>
</phrase>
For deserialization I'm using this class:
[XmlRoot("phrase")]
public class Phrase
{
public Phrase()
{
this.Quoted = new List<string>();
}
[XmlAttribute("level")]
public int Level { get; set; }
[XmlElement("subject")]
public string Subject { get; set; }
[XmlText]
public string Value { get; set; }
[XmlElement("q")]
List<string> Quoted { get; set; }
}
My extension method is:
public static T Deserialize<T>(this XElement xElement)
{
try
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
return (T)xmlSerializer.Deserialize(xElement.CreateReader());
}
catch (Exception)
{
throw;
}
}
When I deserialize XML document all class members are successfully serialized:
Phrase.Level = 1
Phrase.Subyect = "you"
Phrase.Value = "Where"
How can I deserialize <q> tags?
I've tried to use XmlArrayAttibute and XmlArrayItemAttibute but I have not a member for this list (eg. QTags).
You need XmlElement attribute :)
[XmlRoot("phrase")]
public class Phrase
{
[XmlElement("q")]
public List<string> q { get; set; }
[XmlAttribute("level")]
public int Level { get; set; }
[XmlElement("subject")]
public string Subject { get; set; }
[XmlText]
public string Value { get; set; }
}
I'm trying to serialize a class derived from List<> using DataContract. The problem is that properties of my class won't get serialized.
My classes:
[CollectionDataContract] /*[Serializable]*/ /*[DataContract]*/
public class DownloadRuleCollection : List<DownloadRule> {
[DataMember(EmitDefaultValue = false)]
public string SomeProperty { get; set; }
//this is, in fact, more complex, but this is enough for the example
}
[DataContract]
public class DownloadRule {
[DataMember(EmitDefaultValue = false)]
public string Name { get; set; }
/*
more properties
...
*/
}
Test:
static void Main(string[] args) {
//fill test collection with some data...
var col = new DownloadRuleCollection { SomeProperty = "someText" };
var rule = new DownloadRule { Name = "test01" };
col.Add(rule);
rule = new DownloadRule { Name = "test02" };
col.Add(rule);
rule = new DownloadRule { Name = "test03" };
col.Add(rule);
//serialize
Console.WriteLine("serializing");
Serialize(col, "serializationTest.xml");
Console.WriteLine("serialized");
Console.ReadLine();
}
result:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfDownloadRule xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/ConsoleApplication1">
<DownloadRule>
<Name>test01</Name>
</DownloadRule>
<DownloadRule>
<Name>test02</Name>
</DownloadRule>
<DownloadRule>
<Name>test03</Name>
</DownloadRule>
</ArrayOfDownloadRule>
As you can see, the List's items are serialized (and deserialized) properly, but the List itself does not get serialized. I have tried to use different attributes:
[Serializable], no change;
[DataContract], throws exception during serialization (collections cant use this attribute)
btw I am serializing also private fields, so I cannot use XmlSerializer (or other classes that can't serialize private fields).
Use an IList instead. That should serialize properly.
[CollectionDataContract] /*[Serializable]*/ /*[DataContract]*/
public class DownloadRuleCollection : IList<DownloadRule> {
Here is an example i use and works perfectly:
[DataContract(Namespace="http://schemas.datacontract.org/2004/07/InboundIntegration.HL7Messaging")]
public class Message {
public Message() {
InsuranceList = new List<Insurance>();
MessageId = GuidComb.NewGuid();
}
[IgnoreDataMember]
public Guid MessageId { get; private set; }
#region "Data"
[DataMember]
public string MessageTypeIndicator { get; set; }
[DataMember]
public MessageConfiguration MessageConfiguration { get; set; }
[DataMember]
public Patient Patient { get; set; }
[DataMember]
public Encounter Encounter { get; set; }
[DataMember]
public IList<Insurance> InsuranceList { get; set; }
#endregion
Then insurance class looks like this:
[DataContract(Namespace = "http://schemas.datacontract.org/2004/07/InboundIntegration.HL7Messaging")]
public class Insurance {
[DataMember]
public string ExternalPayerId { get; set; }
[DataMember]
public string PayerName { get; set; }
[DataMember]
public string GroupNumber { get; set; }
[DataMember]
public string MemberIdOfPatient { get; set; }
[DataMember]
public string PatientRelationshipToInsuredIndicator { get; set; }
[DataMember]
public string CoordinationOfBenefitsPrecedenceIndicator { get; set; }
Ok, so Climber104's solution would work, but I would need to re-implement all of the List's methods, which makes me feel that I am reinventing the wheel.
JaredPar from Jarek Waliszko's thread suggests to use a wrapper class.
The easiest is to use it just for the sake of serialization process, so I used an protected innner wrapper class. This allows me to achieve my goals with just a few lines of code needed.
public class DownloadRuleCollection : List<DownloadRule> {
public string SomeProperty { get; set; }
public void Serialize(string fileName) {
Serializer.Serialize(
new DownloadRuleCollection_SerializationWrapper {
Collection = this,
SomeProperty = SomeProperty
}, fileName);
}
public static DownloadRuleCollection Deserialize(string fileName) {
var wrapper = Serializer.Deserialize<DownloadRuleCollection_SerializationWrapper>(fileName);
var result = wrapper.Collection;
result.SomeProperty = wrapper.SomeProperty;
return result;
}
[DataContract(Name = "DownloadRuleCollection")]
private class DownloadRuleCollection_SerializationWrapper {
[DataMember(EmitDefaultValue = false, Name = "SomeProperty", Order = 0)]
public string SomeProperty { get; set; }
[DataMember(EmitDefaultValue = false, Name = "DownloadRules", Order = 1)]
public DownloadRuleCollection Collection;
}
}
[DataContract]
public class DownloadRule {
[DataMember(EmitDefaultValue = false)]
public string Name { get; set; }
}
public static class Serializer {
public static void Serialize<T>(T obj, string fileName) {
using(XmlWriter writer = XmlWriter.Create(fileName, new XmlWriterSettings { Indent = true }))
new DataContractSerializer(typeof(T)).WriteObject(writer, obj);
}
public static T Deserialize<T>(Stream stream) {
return (T)new DataContractSerializer(typeof(T)).ReadObject(stream);
}
public static T Deserialize<T>(string fileName) {
using(FileStream fs = File.OpenRead(fileName)) {
return Deserialize<T>(fs);
}
}
}