Add Support for Multidimensional Arrays
See original GitHub issueHi, I found an issue and tried to simplifiy it.
Lets say you have a BaseClass storing a property as type object. Then you have a class deriving from the baseclass which handles the object as a specific type like a string or double[].
The Serialization suceeds but the Deserialization result is different.
- string-> passes
- double[] fails.
Message: Test method UnitTestXML.ExtendenXMLTestLocal.TestObjectSerial threw exception: System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.
The Example used. TestMethod creates 2 objects:
- Adult (handling property ‘Complex’ as double[])
- Children (handling property ‘Complex’ as string)
Both derive from abstract class Person. Then both are se/ and deserialized. Error occurs while trying to read the Adult.
TestMethod
[TestMethod]
public void TestObjectSerial()
{
string filepathChild = @"C:\Temp\Child.xml";
string filepathAdult = @"C:\Temp\Adult.xml";
//int adultC = 1;
double[] adultC = new double[2] { 1.0, 2.0 };
string childC = "COMPLEXCHILDRENDATA";
Child orgC = new Child
{
Name = "Tom",
Complex = childC,
};
Adult orgA = new Adult
{
Name = "Andy",
Complex = adultC
};
Helper_Serial.WriteToXmlFileNG<Child>(filepathChild, orgC);
Helper_Serial.WriteToXmlFileNG<Adult>(filepathAdult, orgA);
Child loadC = Helper_Serial.ReadFromXmlFileNG<Child>(filepathChild);
Adult loadA = Helper_Serial.ReadFromXmlFileNG<Adult>(filepathAdult);
}
Parent/Child class.
public abstract class Person
{
public string Name { get; set; }
public abstract object Complex { get; set; }
}
public class Child : Person
{
string _complex;
public override object Complex
{
get { return _complex; }
set { _complex = (string)value; }
}
}
public class Adult : Person
{
//int IDs;
double[] IDs;
public override object Complex
{
get { return IDs; }
//set { IDs = (int)value; }
set { IDs = (double[])value; }
}
}
Outcome XML Adult
<?xml version="1.0" encoding="utf-8"?>
<Adult
xmlns:exs="https://extendedxmlserializer.github.io/v2"
xmlns:sys="https://extendedxmlserializer.github.io/system"
xmlns="clr-namespace:UnitTestXML.Helper;assembly=UnitTestXML">
<Name>Andy</Name>
<Complex exs:type="sys:double^1">
<sys:double>1</sys:double>
<sys:double>2</sys:double>
</Complex>
</Adult>
Oh Helper Methods:
using ExtendedXmlSerializer.Configuration;
using ExtendedXmlSerializer.ExtensionModel.Xml;
using System;
using System.IO;
using System.Xml.Serialization;
namespace UnitTestXML.Helper
{
public class DeSerialize
{
public static class Helper_Serial
{
//Write Extended
public static bool WriteToXmlFileNG<T>(string filepath, T ObjectToWrite, bool append = false) where T : new()
{
var serializer = new ConfigurationContainer().UseOptimizedNamespaces().Create();
var xml = serializer.Serialize(ObjectToWrite);
File.WriteAllText(filepath, serializer.Serialize(ObjectToWrite));
return true;
}
//Read Extended
public static T ReadFromXmlFileNG<T>(string filepath) where T : new()
{
StreamReader reader = null;
try
{
var serializer = new ConfigurationContainer().UseOptimizedNamespaces().Create();
reader = new StreamReader(filepath);
return serializer.Deserialize<T>(reader);
}
catch (Exception) { throw; }
finally { if (reader != null) { reader.Close(); } }
}
//Write Original XML
public static bool WriteToXmlFile<T>(string filePath, T ObjectToWrite, bool append = false) where T : new()
{
TextWriter writer = null;
var serializer = new XmlSerializer(typeof(T));
writer = new StreamWriter(filePath, append);
serializer.Serialize(writer, ObjectToWrite);
if (writer != null)
writer.Close();
return true;
}
//Read Original
public static T ReadFromXmlFile<T>(string filePath) where T : new()
{
TextReader reader = null;
try
{
var serializer = new XmlSerializer(typeof(T));
reader = new StreamReader(filePath);
return (T)serializer.Deserialize(reader);
}
finally
{
if (reader != null)
reader.Close();
}
}
}
}
}
Issue Analytics
- State:
- Created 6 years ago
- Comments:10 (6 by maintainers)
Top Results From Across the Web
Adding multidimensional arrays of arrays in Java
Declare and initialize new 2-dimensional array "arrayC" to hold your results. Use nested for loops to loop through both arrays and add the ......
Read more >Add support for multidimensional arrays in the Godot ...
The current work-around for this is either to wrap an array object inside a class and use that class in another array or...
Read more >Multidimensional Arrays - MATLAB & Simulink
A multidimensional array in MATLAB® is an array with more than two dimensions. In a matrix, the two dimensions are represented by rows...
Read more >MultiDimensional Arrays In Java (2d and 3d Arrays In Java)
This Tutorial on Multidimensional Arrays in Java Discusses how to Initialize, Access and Print 2d and 3d Arrays in Java with Syntax &...
Read more >Multidimensional Arrays in Java
Overview of Java ... How to Download and Install Java for 64 bit machine? ... How to Download and Install Eclipse on Windows?...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hey @Mike-EEE I have forked and tested the code. The tests passes! You do a fantastic job. Even Newtonsoft json fails handling this.
❤️
Shall I close this issue?