Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/12/09 12:19:32 (15 years ago)
Author:
epitzer
Message:

More compact XML Formatting (no empty names, referencable primitives, compact arrays & lists). (#506)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/New Persistence Exploration/Persistence/Persistence/PrimitiveSerializers.cs

    r1315 r1330  
    11using System;
     2using System.Collections;
     3using System.Collections.Generic;
    24using System.Text;
    35using System.Globalization;
     
    2022      StringBuilder sb = new StringBuilder();
    2123      foreach (string s in ((string)o).Split(
    22         new string[] { "<![CDATA[", "]]>" },
     24        new[] { "<![CDATA[", "]]>" },
    2325        StringSplitOptions.RemoveEmptyEntries)) {
    2426        sb.Append(s);
     
    2729    }
    2830  }
     31
    2932  public class Int2XMLSerializer : IPrimitiveSerializer {
    3033    public Type Type { get { return typeof(int); } }
     
    3639    }
    3740  }
     41
    3842  public class Double2XmlSerializer : IPrimitiveSerializer {   
    3943    public Type Type { get { return typeof(double); } }       
     
    4549    }
    4650  }
     51
    4752  public class Boolean2XmlSerializer : IPrimitiveSerializer {   
    4853    public Type Type { get { return typeof(bool); } }   
     
    5459    }
    5560  }
     61
    5662  public class DateTime2XmlSerializer : IPrimitiveSerializer {   
    5763    public Type Type { get { return typeof(DateTime); } }     
     
    6369    }
    6470  }
     71
     72  public abstract class NumberArray2XmlSerializer : IPrimitiveSerializer {
     73    public abstract Type Type { get; }   
     74    protected virtual string Separator { get { return ";"; } }
     75    protected abstract string formatValue(object o);
     76    protected abstract object parseValue(string o);
     77    public object Serialize(object obj) {     
     78      Array a = (Array)obj;
     79      StringBuilder sb = new StringBuilder();
     80      sb.Append(a.Rank);     
     81      for (int i = 0; i < a.Rank; i++) {
     82        sb.Append(Separator);
     83        sb.Append(a.GetLength(i));
     84      }
     85      foreach (object o in a) {
     86        sb.Append(Separator);
     87        sb.Append(formatValue(o));
     88      }
     89      return sb.ToString();
     90    }
     91    public object DeSerialize(object o) {
     92      IEnumerator values =
     93        ((string) o)
     94        .Split(new[] {Separator},
     95        StringSplitOptions.RemoveEmptyEntries).GetEnumerator();
     96      values.MoveNext();
     97      int rank = int.Parse((string)values.Current);
     98      int[] lengths = new int[rank];
     99      for ( int i = 0; i<rank; i++ ) {
     100        values.MoveNext();
     101        lengths[i] = int.Parse((string)values.Current);
     102      }
     103      Array a = Array.CreateInstance(this.Type.GetElementType(), lengths);
     104      int[] positions = new int[rank];
     105      while ( values.MoveNext() ) {
     106        a.SetValue(parseValue((string)values.Current), positions);
     107        positions[0] += 1;
     108        for ( int i = 0; i<rank-1; i++) {
     109          if (positions[i] >= lengths[i]) {
     110            positions[i] = 0;
     111            positions[i + 1] += 1;
     112          } else {
     113            break;
     114          }
     115        }
     116      }
     117      return a;
     118    }
     119  }
     120  public class IntArray2XmlSerializer : NumberArray2XmlSerializer {
     121    public override Type Type { get { return typeof(int[]); } }
     122    protected override string formatValue(object o) { return o.ToString(); }
     123    protected override object parseValue(string o) { return int.Parse(o); }
     124  }
     125  public class Int2DArray2XmlSerializer : IntArray2XmlSerializer {
     126    public override Type Type { get { return typeof (int[,]); } }
     127  }
     128  public class Int3DArray2XmlSerializer : IntArray2XmlSerializer {
     129    public override Type Type { get { return typeof(int[,,]); } }
     130  }
     131  public class DoubleArray2XmlSerializer : NumberArray2XmlSerializer {
     132    public override Type Type { get { return typeof(double[]); } }
     133    protected override string formatValue(object o) { return ((double) o).ToString("r"); }
     134    protected override object parseValue(string o) { return double.Parse(o); }   
     135  }
     136  public class Double2DArray2XmlSerializer : DoubleArray2XmlSerializer {
     137    public override Type Type { get { return typeof(double[,]); } }   
     138  }
     139  public class Double3DArray2XmlSerializer : DoubleArray2XmlSerializer {
     140    public override Type Type { get { return typeof(double[,,]); } }
     141  }
     142
     143  public abstract class NumberEnumeration2XmlSerializer : IPrimitiveSerializer {
     144    public abstract Type Type { get; }
     145    protected virtual string Separator { get { return ";"; } }
     146    protected abstract void Add(IEnumerable enumeration, object o);
     147    protected abstract object Instantiate();
     148    protected abstract string formatValue(object o);
     149    protected abstract object parseValue(string o);   
     150    public object Serialize(object o) {
     151      StringBuilder sb = new StringBuilder();
     152      foreach (var value in (IEnumerable) o) {       
     153        sb.Append(formatValue(value));
     154        sb.Append(Separator);
     155      }
     156      return sb.ToString();
     157    } 
     158    public object DeSerialize(object o) {
     159      IEnumerable enumeration = (IEnumerable) Instantiate();       
     160      string[] values = ((string) o).Split(new[] {Separator}, StringSplitOptions.RemoveEmptyEntries);
     161      foreach ( var value in values ) {
     162        Add(enumeration, parseValue(value));
     163      }
     164      return enumeration;
     165    }
     166  }
     167  public class IntList2XmlSerializer : NumberEnumeration2XmlSerializer {
     168    public override Type Type { get { return typeof(List<int>); } }
     169    protected override void Add(IEnumerable enumeration, object o) { ((List<int>)enumeration).Add((int)o); }
     170    protected override object Instantiate() { return new List<int>(); }
     171    protected override string formatValue(object o) { return o.ToString(); }
     172    protected override object parseValue(string o) { return int.Parse(o); }
     173  } 
    65174}
Note: See TracChangeset for help on using the changeset viewer.