question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add Support for Jagged Arrays

See original GitHub issue

Hello,

there seems to be an issue serializing 3D-array type double.

I set up a simplified example. Console Application / Install Extension with Install-Package ExtendedXmlSerializer -Version 2.1.1

Code Structure

  1. main
  2. public class (target for serialization)
  3. static helper class to handle se-/deserialization by file

Logic: Create simple object > Initialize 1D,2D and 3D Array > Serialize 2 file > read from file.

Error Thrown at Read ExtendedXmlSerializer.Core.Sprache.ParseException: ‘An attempt was made to parse the identity ‘https://extendedxmlserializer.github.io/system:Double’, but no type could be located with that name.’

Code main

class Program
    {
        static void Main(string[] args)
        {
            //filepath for load/save
            string filepath = @"C:\Temp\Person.xml";

            //create tmpobj
            Person tmpP = new Person{Name = "Test Person"};
            tmpP.Initialize();

            //write 2 file
            Helper_Serial.WriteToXmlFileNG<Person>(filepath, tmpP);

            //read 2 file
            Person loadPerson = Helper_Serial.ReadFromXmlFileNG<Person>(filepath);

        }
    }

person class

[Serializable]
    public class Person
    {
        //Props
        public string Name { get; set; }

        public double[] complex1D { get; set; }
        public double[][] complex2D { get; set; }
        public double[][][] complex3D { get; set; }

        //Method 2 initialize complexArr.
        public void Initialize()
        {
            int _one = 1, two = 2, three = 3;
            complex1D = new double[_one];
            complex2D = new double[_one][];
            complex3D = new double[_one][][];

            for (int i = 0; i < _one; i++)
            {
                complex1D[i] = i;
                complex2D[i] = new double[two];
                complex3D[i] = new double[two][];

                for (int j = 0; j < two; j++)
                {
                    complex2D[i][j] = j;
                    complex3D[i][j] = new double[three];
                    for (int k = 0; k < three; k++)
                        complex3D[i][j][k] = k;
                }

            }
        }
    }

helper serial class

    /// <summary>
    /// Helper Class for Serial. 
    /// </summary>
    public static class Helper_Serial
    {
        //Write
        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
        public static T ReadFromXmlFileNG<T>(string filepath) where T : new()
        {
            StreamReader reader = null;
            try
            {
                var serializer = new ConfigurationContainer().Create();
                reader = new StreamReader(filepath);
                return serializer.Deserialize<T>(reader);
            }
            catch (Exception) { throw; }
            finally { if (reader != null) { reader.Close(); } }
        }
    }

Person.xml after writeoperation

<?xml version="1.0" encoding="utf-8"?>
<Person xmlns="clr-namespace:XML_3D_Test;assembly=XML_3D_Test">
  <Name>Test Person</Name>
  <complex1D>
    <double xmlns="https://extendedxmlserializer.github.io/system">0</double>
  </complex1D>
  <complex2D>
    <Array xmlns:exs="https://extendedxmlserializer.github.io/v2" exs:item="double" xmlns="https://extendedxmlserializer.github.io/system">
      <double>0</double>
      <double>1</double>
    </Array>
  </complex2D>
  <complex3D>
    <Array xmlns:exs="https://extendedxmlserializer.github.io/v2" exs:item="Double[]" xmlns="https://extendedxmlserializer.github.io/system">
      <Array exs:item="double">
        <double>0</double>
        <double>1</double>
        <double>2</double>
      </Array>
      <Array exs:item="double">
        <double>0</double>
        <double>1</double>
        <double>2</double>
      </Array>
    </Array>
  </complex3D>
</Person>

Any help or advise appreciated. Thank you for that great product though.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:22 (14 by maintainers)

github_iconTop GitHub Comments

1reaction
Mike-E-angelocommented, Feb 27, 2018

Sure… I guess I am being a little sloppy with open issues. Guess we can take care of that now. 😆

1reaction
Mike-E-angelocommented, Feb 27, 2018

Woohoo! That’s awesome. TBH I am amazed it even works, haha. Figured there must be a reason why it wasn’t supported in the classic XmlSerializer. I got to learn a little bit about arrays to boot. 😉

For version 3 I am thinking of enabling formatting support for arrays that have elements with a registered converter, so that it will emit them as one string instead of many elements. for example, the array above would be something like:

<ComplexND exs:type="sys:double^6" exs:map="1,2,3,1,2,3">0,12,24,6,18,30,2,14,26,8,20,32,4,16,28,10,22,34,1,13,25,7,19,31,3,15,27,9,21,33,5,17,29,11,23,35</ComplexND>

We actually already do something similar to this for our byte[] arrays, but not others. Should be easy enough to extend. I would introduce it to v2 but it’s pretty much in quality release mode while we work on v3 (total rewrite).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jagged Arrays - C# Programming Guide
A jagged array in C# is an array whose elements are arrays of different sizes. Learn how to declare, initialize, and access jagged...
Read more >
Jagged Array in Java
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, ...
Read more >
Jagged Array In Java - Tutorial With Examples
This Tutorial Explains a Special Version of Multidimensional Array called Jagged Array. Learn to Create, Initialize & Use Jagged Arrays in ...
Read more >
C# Jagged Arrays - Explained in 5 Minutes - YouTube
Since its origin, C# has added features to support new workloads and ... A jagged array is an array whose elements are arrays,...
Read more >
How to work with jagged arrays in C# | InfoWorld
You can have jagged arrays in any computer language that provides support for arrays. A jagged array (also known as a ragged array)...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found