Free cookie consent management tool by TermsFeed Policy Generator

Changeset 1363


Ignore:
Timestamp:
03/19/09 14:19:58 (16 years ago)
Author:
epitzer
Message:

Convenience method for XML serialization and de-serizliation. (#506)

Location:
branches/New Persistence Exploration/Persistence
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/New Persistence Exploration/Persistence/Persistence/Default/Xml/XmlGenerator.cs

    r1362 r1363  
    44using HeuristicLab.Persistence.Interfaces;
    55using HeuristicLab.Persistence.Core;
     6using System.IO;
    67
    78namespace HeuristicLab.Persistence.Default.Xml {
     
    3637    private enum NodeType { Start, End, Inline } ;
    3738
    38     private static string FormatNode(string name, Dictionary<string, object> attributes, NodeType type) {
     39    private static string FormatNode(string name,
     40        Dictionary<string, object> attributes,
     41        NodeType type) {
     42      return FormatNode(name, attributes, type, " ");
     43    }
     44
     45    private static string FormatNode(string name,
     46        Dictionary<string, object> attributes,
     47        NodeType type, string space) {
    3948      StringBuilder sb = new StringBuilder();
    4049      sb.Append('<');
     
    4453      foreach (var attribute in attributes) {
    4554        if (attribute.Value != null && !string.IsNullOrEmpty(attribute.Value.ToString())) {
    46           sb.Append(' ');
     55          sb.Append(space);
    4756          sb.Append(attribute.Key);
    4857          sb.Append("=\"");
     
    112121      yield return "<" + XmlStrings.TYPECACHE + ">";
    113122      foreach (var mapping in typeCache)
    114         yield return FormatNode(XmlStrings.TYPE, mapping.GetDict(), NodeType.Inline);                               
     123        yield return "  " + FormatNode(
     124          XmlStrings.TYPE,
     125          mapping.GetDict(),
     126          NodeType.Inline,
     127          "\n    ");
    115128      yield return "</" + XmlStrings.TYPECACHE + ">";
    116129    }
     130
     131    public static void Serialize(object o, string basename) {     
     132      Serialize(o, basename, ConfigurationService.Instance.GetDefaultConfig(XmlFormat.Instance));
     133    }
     134
     135    public static void Serialize(object o, string basename, Configuration configuration) {
     136    Serializer s = new Serializer(o, configuration);
     137      XmlGenerator xmlGenerator = new XmlGenerator();
     138      StreamWriter writer = new StreamWriter(basename + ".xml");
     139      foreach (ISerializationToken token in s) {
     140        string line = xmlGenerator.Format(token);
     141        writer.Write(line);
     142        Console.Out.Write(line);
     143      }
     144      writer.Close();
     145      writer = new StreamWriter(basename + "-types.xml");
     146      foreach (string line in xmlGenerator.Format(s.TypeCache)) {
     147        writer.WriteLine(line);
     148        Console.Out.WriteLine(line);
     149      }
     150      writer.Close();     
     151    }
     152
    117153  }
    118154}
  • branches/New Persistence Exploration/Persistence/Persistence/Default/Xml/XmlParser.cs

    r1362 r1363  
    104104      return typeCache;
    105105    }
     106
     107    public static object DeSerialize(string basename) {
     108      DeSerializer deSerializer = new DeSerializer(
     109        ParseTypeCache(new StreamReader(basename + "-types.xml")));
     110      XmlParser parser = new XmlParser(new StreamReader("test.xml"));
     111      return deSerializer.DeSerialize(parser);     
     112    }
    106113  } 
    107114}
  • branches/New Persistence Exploration/Persistence/Persistence/HeuristicLab.Persistence.csproj

    r1361 r1363  
    99    <OutputType>Library</OutputType>
    1010    <AppDesignerFolder>Properties</AppDesignerFolder>
    11     <RootNamespace>Persistence</RootNamespace>
    12     <AssemblyName>Persistence</AssemblyName>
     11    <RootNamespace>HeuristicLab.Persistence</RootNamespace>
     12    <AssemblyName>HeuristicLab.Persistence</AssemblyName>
    1313    <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
    1414    <FileAlignment>512</FileAlignment>
  • branches/New Persistence Exploration/Persistence/Test/NewSerializationTest.cs

    r1362 r1363  
    154154      r.dict.Add("two", 2);
    155155      r.dict.Add("three", 3);
    156       Serializer s = new Serializer(r, ConfigurationService.Instance.GetDefaultConfig(XmlFormat.Instance));       
    157       XmlGenerator xmlGenerator = new XmlGenerator();
    158       StreamWriter writer = new StreamWriter("test.xml");
    159       foreach (ISerializationToken token in s) {
    160         string line = xmlGenerator.Format(token);
    161         writer.Write(line);
    162         Console.Out.Write(line);
    163       }
    164       writer.Close();
    165       writer = new StreamWriter("test-types.xml");
    166       foreach (string line in xmlGenerator.Format(s.TypeCache)) {
    167         writer.WriteLine(line);
    168         Console.Out.WriteLine(line);
    169       }
    170       writer.Close();
    171      
    172       XmlParser parser = new XmlParser(new StreamReader("test.xml"));
    173       DeSerializer deSerializer = new DeSerializer(
    174         XmlParser.ParseTypeCache(new StreamReader("test-types.xml")));       
    175       object o = deSerializer.DeSerialize(parser);     
     156      XmlGenerator.Serialize(r, "test");           
     157      object o = XmlParser.DeSerialize("test");
    176158      Console.Out.WriteLine(Util.AutoFormat(o, true));     
    177159    }
Note: See TracChangeset for help on using the changeset viewer.