/** * <summary>Deserialize an ObjectTag back into an object.</summary> * <param name="tag">The object tag.</param> * <typeparam name="T">The Class to deserialize into.</typeparam> * <returns>The deserialized object.</returns> */ public static T Deserialize <T>(ObjectTag tag) { if (!tag.HasTag("ODS_TAG")) { throw new Exception("Cannot deserialze tag: This tag was not serialzed!"); } string classname = ((StringTag)tag.GetTag("ODS_TAG")).GetValue(); Assembly asm = Assembly.GetEntryAssembly(); Type mainType = asm.GetType(classname); BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; object obj = Activator.CreateInstance(mainType); foreach (FieldInfo field in mainType.GetFields(flags)) { if (field.GetCustomAttribute(typeof(ODSSerializeable)) == null) { continue; } ITag fTag = tag.GetTag(field.Name); field.SetValue(obj, unwrap(fTag)); } return((T)obj); }
/** * <summary>Unwrap a tag and turn it back into its original value. This method will not work for Lists or Dictionaries.</summary> * <param name="tag">The tag to unwrap.</param> * <typeparam name="T">The c# datatype represented by the tag.</typeparam> * <returns>The value of the tag.</returns> */ public static T UnWrap <T>(Tag <T> tag) { if (tag.GetType() == typeof(ObjectTag)) { ObjectTag objTag = (ObjectTag)tag; StringTag clazzName = (StringTag)objTag.GetTag("ODS_TAG"); if (clazzName == null) { throw new Exception("Cannot unwrap object: TagObject is not a serialized object"); } return((T)Deserialize <object>(objTag)); } return(tag.GetValue()); }
/** * <summary>This method is to unwrap tags manualy. Due to the limitations of C# I cannot use the UnWrap methods and generics cannot have wildcards.</summary> */ private static object unwrap(ITag tag) { if (tag.GetType() == typeof(ObjectTag)) { ObjectTag objTag = (ObjectTag)tag; StringTag clazzName = (StringTag)objTag.GetTag("ODS_TAG"); if (clazzName == null) { throw new Exception("Cannot unwrap object: TagObject is not a serialized object"); } return(Deserialize <object>(objTag)); } if (tag.GetType() == typeof(ByteTag)) { return(((ByteTag)tag).GetValue()); } if (tag.GetType() == typeof(CharTag)) { return(((CharTag)tag).GetValue()); } if (tag.GetType() == typeof(DoubleTag)) { return(((DoubleTag)tag).GetValue()); } if (tag.GetType() == typeof(FloatTag)) { return(((FloatTag)tag).GetValue()); } if (tag.GetType() == typeof(IntTag)) { return(((IntTag)tag).GetValue()); } if (tag.GetType() == typeof(LongTag)) { return(((LongTag)tag).GetValue()); } if (tag.GetType() == typeof(ShortTag)) { return(((ShortTag)tag).GetValue()); } if (tag.GetType() == typeof(StringTag)) { return(((StringTag)tag).GetValue()); } return(null); }
static void Main(string[] args) { ObjectDataStructure ods = new ObjectDataStructure(new FileInfo(Directory.GetCurrentDirectory() + @"\test3.ods"), new GZIPCompression()); // Register a custom tag. ODSUtil.RegisterCustomTag(new CustomTag("", "")); Console.WriteLine(Directory.GetCurrentDirectory() + "\\test3.ods"); List <ITag> tags = new List <ITag>(); tags.Add(new StringTag("ExampleKey", "This is an example string!")); tags.Add(new IntTag("ExampleInt", 754)); ObjectTag car = new ObjectTag("Car"); car.AddTag(new StringTag("type", "Jeep")); car.AddTag(new IntTag("gas", 30)); List <IntTag> coordsList = new List <IntTag>() { new IntTag("", 10), new IntTag("", 5), new IntTag("", 10) }; car.AddTag(new ListTag <IntTag>("coords", coordsList)); ObjectTag owner = new ObjectTag("Owner"); owner.AddTag(new StringTag("firstName", "Jeff")); owner.AddTag(new StringTag("lastName", "Bob")); owner.AddTag(new IntTag("Age", 30)); car.AddTag(owner); tags.Add(car); tags.Add(new CustomTag("Test", "Test")); CompressedObjectTag compressedObjectTag = new CompressedObjectTag("TestCompressedObject"); compressedObjectTag.AddTag(ODSUtil.Wrap("TestObject", "This is a test!")); compressedObjectTag.AddTag(ODSUtil.Wrap("Number", 15)); compressedObjectTag.AddTag(ODSUtil.Wrap("Decimal", 34.5)); tags.Add(compressedObjectTag); ods.Save(tags); ods.Append(new StringTag("Test", "test")); ods.GetAll(); // Loading Example StringTag tag = (StringTag)ods.Get("ExampleKey"); Console.WriteLine("The value of the ExampleKey is: " + tag.GetValue()); ObjectTag myCar = (ObjectTag)ods.Get("Car"); StringTag myCarType = (StringTag)myCar.GetTag("type"); Console.WriteLine("The car is a " + myCarType.GetValue()); Console.WriteLine("First Name:"); StringTag ownerFirstName = (StringTag)ods.Get("Car.Owner.firstName"); Console.WriteLine("Last Name:"); StringTag ownerLastName = (StringTag)ods.Get("Car.Owner.lastName"); Console.WriteLine("The owner of the car is " + ODSUtil.UnWrap(ownerFirstName) + " " + ODSUtil.UnWrap(ownerLastName)); Console.WriteLine(ods.Find("Car.Owner.firstName")); Console.WriteLine(((CustomTag)ods.Get("Test")).GetValue()); ods.Set("Car.Owner.firstName", new StringTag("firstName", "Example")); ods.ReplaceData("Car.Owner.Age", new IntTag("Age", 3)); Console.WriteLine(ODSUtil.UnWrap((IntTag)ods.Get("Car.Owner.Age"))); CompressedObjectTag compressedObject = (CompressedObjectTag)ods.Get("TestCompressedObject"); IntTag numberTag = (IntTag)compressedObject.GetTag("Number"); Console.WriteLine("Test Compression Number: " + numberTag.GetValue()); ods.SaveToFile(new FileInfo(@"new_file.ods"), new GZIPCompression()); //ods.Set("Car.Owner.Age", new IntTag("Age", 3)); }