Solved: code below is not causing an infinite loop as I thought. the loop was in the code calling the deserialization. this posted code works just fine
I am trying to serialize and deserialize to xml the following object
public class MessageObjectCollection : List<MessageObject>
{
public string Serialize()
{
return XmlObjectSerializer.SerializeObject(this);
}
public static MessageObjectCollection DeSerialize(string serializedPriceHistory)
{
return XmlObjectSerializer.DeserializeObject<MessageObjectCollection>(serializedPriceHistory);
}
}
The MessageObject class looks like this
public class MessageObject
{
public string Title;
public MessageObjectCollection Responses;
}
So if I have a instance of messageobjectcollection that looks like:
var msgColl = new MessageObjectCollection
{
new MessageObject
{
Title = "Hello",
Responses = new MessageObjectCollection
{
new MessageObject
{
Title = "hi",
Responses = null
}
}
}
}
I can serialize this just fine by calling
var xml = msgColl.Serialize();
However when I try to deserialize this by calling
var msgColl = new MessageObjectCollection().Deserialize(xml);
I get an stack overflow exception in the deserialization method:
public static T DeserializeObject<T>(string xml)
{
T result;
var ser = new XmlSerializer(typeof(T));
var buffer = StringToUTF8ByteArray(xml);
using (var stream = new MemoryStream(buffer, 0, buffer.Length))
{
result = (T) ser.Deserialize(stream);
}
return result;
}
Anyone know what I'm doing wrong?
Im not sure if its relevant to the problem but as the Deserialize method is static shouldn't you be calling...
var msgColl = MessageObjectCollection.Deserialize(xml);
instead of...
var msgColl = new MessageObjectCollection().Deserialize(xml);
??
Related
Given the following c# code:
using Newtonsoft.Json;
using System;
namespace ConsoleApp2
{
internal class Program
{
public class BatteryStatus
{
// The battery level reports percentage of the full battery.The field can take values from 0 to 100% (0x00 – 0x64).
// The value 0xFF indicates a battery low warning.
public byte BatteryLevel { get; set; }
public bool LowBatteryWarning { get; set; }
public DateTime TimestampUtc { get; set; }
public BatteryStatus(byte batteryLevel)
{
TimestampUtc = DateTime.UtcNow;
if (batteryLevel == 0xff)
{
LowBatteryWarning = true;
batteryLevel = 0;
}
BatteryLevel = batteryLevel;
}
}
static void Main(string[] args)
{
BatteryStatus batteryStatus = new BatteryStatus(40);
string json = SaveObjectToJsonString(batteryStatus);
Object obj = ReadObjectFromJsonString(json);
}
static string SaveObjectToJsonString(Object obj)
{
string json = JsonConvert.SerializeObject(obj);
return json;
}
static Object ReadObjectFromJsonString(string json)
{
var obj = JsonConvert.DeserializeObject(json);
return obj;
}
}
}
Without changing the SaveObjectToString or ReadObjectFromString functions (and pretend that you didn't even see the source code for these), how can obj be converted to an object of the BatteryStatus class?
Attempting to cast directly like this:
batteryStatus = (BatteryStatus)obj;
result in the following error:
Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'BatteryStatus'.
You can use JObject.ToObject method.
static void Main(string[] args)
{
BatteryStatus batteryStatus = new BatteryStatus(40);
string json = SaveObjectToJsonString(batteryStatus);
var obj = ReadObjectFromJsonString(json);
JObject jObj = obj is JObject ? (JObject)obj : JObject.FromObject(obj);
var deserializedBatteryStatus = jObj.ToObject<BatteryStatus>();
}
Would something using generics work for your use case?
We could change your ReadObjectFromJsonString and SaveObjectToJsonString to use generics...
static string SaveObjectToJsonString<T>(Object obj) where T: class
{
string json = JsonConvert.SerializeObject(obj);
return json;
}
static T ReadObjectFromJsonString<T>(string json) where T: class
{
var obj = JsonConvert.DeserializeObject<T>(json);
return obj;
}
Then the class can be specified when you call those functions -
BatteryStatus status = new BatteryStatus(40);
string str = SaveObjectToJsonString<BatteryStatus>(status);
BatteryStatus readBack = ReadObjectFromJsonString<BatteryStatus>(str);
try this
BatteryStatus BatteryStatus = ((JObject)obj).ToObject<BatteryStatus>();
This is the solution I ended up with by combining information from the other answers (use of JObject and generics):
Add a generic wrapper function around the original ReadObjectFromJsonString function:
static T ReadObjectFromJsonString<T>(string json)
{
var obj = ReadObjectFromJsonString(json);
if (obj == null)
return default;
JObject jObj = obj is JObject ? (JObject)obj : JObject.FromObject(obj);
return jObj.ToObject<T>();
}
Then the main code will become quite clean, simple and logical:
static void Main(string[] args)
{
BatteryStatus batteryStatus = new BatteryStatus(40);
string json = SaveObjectToJsonString(batteryStatus);
batteryStatus = ReadObjectFromJsonString<BatteryStatus>(json);
}
I want to serialize some json data I get from the web to classes and use the data, so I went to http://json2csharp.com/ and turned the json as below
json: [{"line_descr":"\u03a0\u0395\u0399\u03a1\u0391\u0399\u0391\u03a3 -
\u0392\u039f\u03a5\u039b\u0391","line_descr_eng":"PEIRAIAS - VOYLA"}]
To this class:
public class RootObject
{
public string line_descr { get; set; }
public string line_descr_eng { get; set; }
}
This is my code:
class LineName
{
public async static Task<RootObject> GetLineName(int linecode)
{
var http = new HttpClient();
var response = await http.GetAsync("http://telematics.oasa.gr/api/?act=getLineName&p1=962");
var result = await response.Content.ReadAsStringAsync();
var serializer = new DataContractJsonSerializer(typeof(RootObject));
var ms = new MemoryStream(Encoding.UTF8.GetBytes(result));
var data = (RootObject)serializer.ReadObject(ms);
return data;
}
}
[DataContract]
public class RootObject
{
[DataMember]
public string line_descr { get; set; }
[DataMember]
public string line_descr_eng { get; set; }
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
RootObject myLine = await LineName.GetLineName(92);
ResultTextBlock.Text = myLine.line_descr_eng;
}
So when I try to get the data and display it in my textblock I get the error: line_descr_eng is null.
Can someone point where the fault is ? since the line_descr_eng should be
PEIRAIAS - VOYLA but mine is null and after a lot of searching I cant find where the fault is.
Your json is an array, not an object, and you should deserialize it into an array.
public async static Task<RootObject[]> GetLineName(int linecode)
{
var http = new HttpClient();
var response = await http.GetAsync("http://telematics.oasa.gr/api/?act=getLineName&p1=962");
var result = await response.Content.ReadAsStringAsync();
var serializer = new DataContractJsonSerializer(typeof(RootObject[]));
var ms = new MemoryStream(Encoding.UTF8.GetBytes(result));
var data = (RootObject[])serializer.ReadObject(ms);
return data;
}
//...
var myLines = await LineName.GetLineName(92);
var myLine = myLines.FirstOrDefault();
Also you don't need a memory stream, you can read stream from the http response
var result = await response.Content.ReadAsStreamAsync();
You simple can use the JavaScriptSerializer class instead of DataContractJsonSerializer like this:
Replace:
var serializer = new DataContractJsonSerializer(typeof(RootObject));
var ms = new MemoryStream(Encoding.UTF8.GetBytes(result));
var data = (RootObject)serializer.ReadObject(ms);
with this:
var ser = new JavaScriptSerializer();
var test = ser.Deserialize<List<RootObject>>(json);
If you cannot find JavaScriptSerializer, then you have to do the simple following steps:
Right click References and do Add Reference, then from Assemblies->Framework select System.Web.Extensions.
Now you should be able to add the following to your class file:
using System.Web.Script.Serialization;
Cited from: https://stackoverflow.com/a/15391388/5056173
I tried with following code but getting error for input parameter as string.
protected override object DeserializeCore(Type type, byte[] value)
{
using (var ms = new MemoryStream(value))
using (var sr = new StreamReader(ms, Encoding.UTF8))
{
var result = Newtonsoft.Json.JsonConvert.DeserializeObject(sr, type);
return result;
}
}
and I passed it as sr.ToString() getting error :
Unexpected character encountered while parsing value: S. Path '', line 0, position 0.
Would this not be simpler?
protected override object DeserializeCore(Type type, byte[] value) {
var str = System.Text.Encoding.UTF8.GetString(value);
return JsonConvert.DeserializeObject(str, type);
}
(I can't figure out why you are using the streams. Is it related to some issue with the encoding?)
try this -
public class JsonObject
{
public object Value { get; set; }
public string Type { get; set; }
}
var s = "{'Value':{'something':'test'},'Type':'JsonData'}";
var o = DeserializeCore(typeof(JsonObject), Encoding.UTF8.GetBytes(s.ToCharArray()));
should work fine.
I have an interface with exposes a property called Pages:
public interface INameSet
{
IQueryable<string> Names { get; }
}
I have this class which implements the interface and must also be parsed from a JSON object:
[DataContract(Name = "surveyPageSet")]
public class SurveyPage : INameSet
{
[DataMember(Name = "names")]
public List<string> SurveyNames { get; set; }
public IQueryable<string> Names
{
get
{
//Returns SurveyNames after some filtration logic
}
}
}
My problem is that when I pass in this object:
{
"names": ["testname"]
}
The JSON interpreter is trying to deserialize it to match the Names property instead of the SurveyNames property. I know this happens because when removing the implementation of the interface and changing SurveyNames to Names it populates the property fine. Is there any way to get it to serialize to the correct property or do I need to create a translator class that will generate the proper concretion of the INameSet interface?
EDIT: This is with the built-in serializer. If there is a solution with Newtonsoft/JSON.NET that would be fine with me.
JavaScriptSerializer doesn't allow for remapping of names out of the box, so don't use it.
Instead, use Json.NET or DataContractJsonSerializer. In fact, both should already work given the data contract attributes you have applied.
For instance, using Json.NET, if I do:
var page1 = JsonConvert.DeserializeObject<SurveyPage>(json);
Debug.Assert(page1.SurveyNames != null && page1.SurveyNames.SequenceEqual(new string [] { "testname" }));
Then there is no assert. Similarly there is no assert if I do:
var page2 = DataContractJsonSerializerHelper.GetObject<SurveyPage>(json);
Debug.Assert(page2.SurveyNames != null && page2.SurveyNames.SequenceEqual(new string[] { "testname" }));
using the helper class:
public static class DataContractJsonSerializerHelper
{
private static MemoryStream GenerateStreamFromString(string value)
{
return new MemoryStream(Encoding.Unicode.GetBytes(value ?? ""));
}
public static string GetJson<T>(T obj, DataContractJsonSerializer serializer)
{
using (var memory = new MemoryStream())
{
serializer.WriteObject(memory, obj);
memory.Seek(0, SeekOrigin.Begin);
using (var reader = new StreamReader(memory))
{
return reader.ReadToEnd();
}
}
}
public static string GetJson<T>(T obj) where T : class
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
return GetJson(obj, serializer);
}
public static T GetObject<T>(string json) where T : class
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
return GetObject<T>(json, serializer);
}
public static T GetObject<T>(string json, DataContractJsonSerializer serializer)
{
T obj = default(T);
using (var stream = GenerateStreamFromString(json))
{
obj = (T)serializer.ReadObject(stream);
}
return obj;
}
}
Update
If you really want to continue to use JavaScriptConverter, you can write your own JavaScriptConverter and deserialize each field manually. But it's a bother and I wouldn't recommend it.
I'm trying to convert the result I get from my web service as a string and convert it to an object.
This is the string I'm getting from my service:
<StatusDocumentItem><DataUrl/><LastUpdated>2013-01-31T15:28:13.2847259Z</LastUpdated><Message>The processing of this task has started</Message><State>1</State><StateName>Started</StateName></StatusDocumentItem>
So I have a class for this as:
[XmlRoot]
public class StatusDocumentItem
{
[XmlElement]
public string DataUrl;
[XmlElement]
public string LastUpdated;
[XmlElement]
public string Message;
[XmlElement]
public int State;
[XmlElement]
public string StateName;
}
And this is how I'm trying to get that string as an object of type StatusDocumentItem with XMLDeserializer (NB. operationXML contains the string):
string operationXML = webRequest.getJSON(args[1], args[2], pollURL);
var serializer = new XmlSerializer(typeof(StatusDocumentItem));
StatusDocumentItem result;
using (TextReader reader = new StringReader(operationXML))
{
result = (StatusDocumentItem)serializer.Deserialize(reader);
}
Console.WriteLine(result.Message);
But my result object is always empty. What am I doing wrong?
Update. The value I get from my operationXML is like this and has an unnecessary xmlns attribute that is blocking my deserialization. Without that attribute, everything is working fine. Here is how it looks like:
"<StatusDocumentItem xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><DataUrl/><LastUpdated>2013-02-01T12:35:29.9517061Z</LastUpdated><Message>Job put in queue</Message><State>0</State><StateName>Waiting to be processed</StateName></StatusDocumentItem>"
Try this:
string xml = "<StatusDocumentItem xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><DataUrl/><LastUpdated>2013-02-01T12:35:29.9517061Z</LastUpdated><Message>Job put in queue</Message><State>0</State><StateName>Waiting to be processed</StateName></StatusDocumentItem>";
var serializer = new XmlSerializer(typeof(StatusDocumentItem));
StatusDocumentItem result;
using (TextReader reader = new StringReader(xml))
{
result = (StatusDocumentItem)serializer.Deserialize(reader);
}
Console.WriteLine(result.Message);
Console.ReadKey();
Does it show "Job put in queue"?
This generic extension works well for me....
public static class XmlHelper
{
public static T FromXml<T>(this string value)
{
using TextReader reader = new StringReader(value);
return (T) new XmlSerializer(typeof(T)).Deserialize(reader);
}
}