public void TestSerialize() { var color = new CodedColor(1, 2, 3, 4) { ColorCode = "100", ColorName = "Black", SymbolChar = 'A' }; var sb = new StringBuilder(); using (var writer = XmlWriter.Create(sb)) { color.WriteToXml(writer); } using (var reader = XmlReader.Create(new StringReader(sb.ToString()))) { var reloadedColor = ColorSerializer.ReadFromXml(reader); Assert.AreEqual(1, reloadedColor.A); Assert.AreEqual(2, reloadedColor.R); Assert.AreEqual(3, reloadedColor.G); Assert.AreEqual(4, reloadedColor.B); Assert.AreEqual("100", reloadedColor.ColorCode); Assert.AreEqual("Black", reloadedColor.ColorName); Assert.AreEqual('A', reloadedColor.SymbolChar); } }
public override void StrongSerialize(Stream stream, Light value) { LightType type = value.type; Write(stream, (int)type); switch (type) { case LightType.Point: Write(stream, value.range); break; case LightType.Directional: Write(stream, value.cookieSize); Write(stream, value.shadowStrength); Write(stream, value.shadowBias); break; case LightType.Spot: Write(stream, value.spotAngle); Write(stream, value.range); break; } ColorSerializer.Write(stream, value.color); Write(stream, value.intensity); if (type != LightType.Area) { AssetReferenceSerializer.Write(stream, value.cookie); AssetReferenceSerializer.Write(stream, value.flare); Write(stream, value.cullingMask); Write(stream, (int)value.renderMode); } }
public override void StrongDeserialize(Stream stream, ref Light instance) { var type = (LightType)stream.ReadInt(); switch (type) { case LightType.Point: instance.range = stream.ReadFloat(); break; case LightType.Directional: instance.cookieSize = stream.ReadFloat(); instance.shadowStrength = stream.ReadFloat(); instance.shadowBias = stream.ReadFloat(); break; case LightType.Spot: instance.spotAngle = stream.ReadFloat(); instance.range = stream.ReadFloat(); break; } instance.color = ColorSerializer.Read(stream); instance.intensity = stream.ReadFloat(); if (type != LightType.Area) { instance.cookie = AssetReferenceSerializer.Read(stream) as Texture; instance.flare = AssetReferenceSerializer.Read(stream) as Flare; instance.cullingMask = stream.ReadInt(); instance.renderMode = (LightRenderMode)stream.ReadInt(); } }
public void SerializeNamedColor() { Color color = Color.DodgerBlue; string encodedColor = ColorSerializer.SerializeColor(color); Assert.AreEqual("NamedColor:DodgerBlue", encodedColor); Color color2 = ColorSerializer.DeserializeColor(encodedColor); Assert.AreEqual(color, color2); }
public void SerializeArgbColor() { Color color = Color.FromArgb(255, 128, 64, 32); string encodedColor = ColorSerializer.SerializeColor(color); Assert.AreEqual("ARGBColor:255:128:64:32", encodedColor); Color color2 = ColorSerializer.DeserializeColor(encodedColor); Assert.AreEqual(color, color2); }
/// <summary> /// Writes the property. /// </summary> /// <param name="name">The property name.</param> /// <param name="type">The property type.</param> /// <param name="value">The property value.</param> private void WriteProperty(string name, Type type, object value) { this.writer.WriteStartElement(PropertyElement); this.writer.WriteAttributeString(NameAttribute, name); this.writer.WriteAttributeString(TypeAttribute, type.AssemblyQualifiedName); if (value == null) { this.writer.WriteAttributeString(IsNullAttribute, TrueValue); } else { this.writer.WriteAttributeString(IsNullAttribute, FalseValue); if (value is Font) { FontSerailizer.WriteType(value, this.writer); } else if (value is Point) { PointSerializer.WriteType(value, this.writer); } else if (value is Size) { SizeSerializer.WriteType(value, this.writer); } else if (value is Color) { ColorSerializer.WriteType(value, this.writer); } else if (value is RecentFiles) { RecentFiles.WriteType(value, this.writer); } else { this.writer.WriteAttributeString(SeralizeAsAttribute, SeralizeAs.Default.ToString()); this.writer.WriteValue(value); } } this.writer.WriteEndElement(); // Property }
/// <summary> /// Reads the property value. /// </summary> /// <returns>The property value.</returns> private object ReadPropertyValue() { SeralizeAs serializeAs = (SeralizeAs)Enum.Parse(typeof(SeralizeAs), this.reader.GetAttribute(SeralizeAsAttribute)); if (serializeAs == SeralizeAs.Font) { return(FontSerailizer.ReadType(this.reader)); } else if (serializeAs == SeralizeAs.Point) { return(PointSerializer.ReadType(this.reader)); } else if (serializeAs == SeralizeAs.Size) { return(SizeSerializer.ReadType(this.reader)); } else if (serializeAs == SeralizeAs.Color) { return(ColorSerializer.ReadType(this.reader)); } else if (serializeAs == SeralizeAs.RecentFiles) { return(RecentFiles.ReadType(this.reader)); } else { // SeralizeAs.Default Type type = Type.GetType(this.reader.GetAttribute(TypeAttribute)); if (type.IsEnum) { return(Enum.Parse(type, this.reader.ReadString())); } else if (type == typeof(string)) { return(this.reader.ReadString()); } else { return(Convert.ChangeType(this.reader.ReadString(), type, CultureInfo.CurrentCulture)); } } }
/// <summary> /// Serialize VectorLayer /// </summary> /// <param name="vectorLayer">VectorLayer to serialize</param> /// <param name="vectorLayerElement">Root element to serialize</param> public static void Serialize(EidssUserDbLayer vectorLayer, XmlElement vectorLayerElement) { XmlElement xmlElement; LayerSerializer.SerializeLayerFields(vectorLayer, vectorLayerElement); #region << Serialization >> //TODO[enikulin]: need refactoring. only one property (LayerDBguid) and type of layer is changed from VectorLayer //LayerDbGuid xmlElement = vectorLayerElement.OwnerDocument.CreateElement("LayerDbGuid"); xmlElement.InnerText = vectorLayer.LayerDbGuid.ToString(); vectorLayerElement.AppendChild(xmlElement); //LabelLayerGuid xmlElement = vectorLayerElement.OwnerDocument.CreateElement("LabelLayerGuid"); xmlElement.InnerText = vectorLayer.LabelLayerGuid.ToString(); vectorLayerElement.AppendChild(xmlElement); //ClippingEnabled xmlElement = vectorLayerElement.OwnerDocument.CreateElement("ClippingEnabled"); xmlElement.InnerText = vectorLayer.ClippingEnabled.ToString(); vectorLayerElement.AppendChild(xmlElement); //SmoothingMode xmlElement = vectorLayerElement.OwnerDocument.CreateElement("SmoothingMode"); xmlElement.InnerText = vectorLayer.SmoothingMode.ToString("D"); vectorLayerElement.AppendChild(xmlElement); //SRID xmlElement = vectorLayerElement.OwnerDocument.CreateElement("SRID"); xmlElement.InnerText = vectorLayer.SRID.ToString("F0"); vectorLayerElement.AppendChild(xmlElement); //Style try { StyleSerializer.SerializeAsNode(vectorLayer.Style, vectorLayerElement); } catch (Exception ex) { throw new VectorLayerSerializationException("Style can't be serialized: " + ex.Message, ex); } //Theme try { ThemeSerializer.SerializeAsNode(vectorLayer.Theme, vectorLayerElement); } catch (Exception ex) { throw new VectorLayerSerializationException("Theme can't be serialized: " + ex.Message, ex); } //Marker size xmlElement = vectorLayerElement.OwnerDocument.CreateElement("MarkerSize"); xmlElement.InnerText = vectorLayer.MarkerSize.ToString(); vectorLayerElement.AppendChild(xmlElement); //Marker color try { ColorSerializer.SerializeAsNode(vectorLayer.MarkerColor, vectorLayerElement, "MarkerColor"); } catch (Exception ex) { throw new VectorLayerSerializationException("'MarkerColor' can't be serialized: " + ex.Message, ex); } //Polygon Fill Style xmlElement = vectorLayerElement.OwnerDocument.CreateElement("PolygonFillStyle"); xmlElement.InnerText = vectorLayer.PolygonFillStyle; vectorLayerElement.AppendChild(xmlElement); //Polygon Fill Color try { ColorSerializer.SerializeAsNode(vectorLayer.PolygonFillColor, vectorLayerElement, "PolygonFillColor"); } catch (Exception ex) { throw new VectorLayerSerializationException("'PolygonFillColor' can't be serialized: " + ex.Message, ex); } #endregion }
/// <summary> /// Deserialize VectorLayer /// </summary> /// <param name="vectorLayerElement">Root element to deserialize</param> /// <returns>VectorLayer</returns> public static EidssUserDbLayer Deserialize(XmlElement vectorLayerElement) { EidssUserDbLayer resultVectorLayer = null; XmlNode tempNode; LayerSerializer.LayerInfo lyrInfo; bool tempBool; int tempInt; #region <<Deserialize LayerFields >> try { lyrInfo = LayerSerializer.DeserializeLayerFields(vectorLayerElement); } catch (Exception ex) { throw new VectorLayerDeserializationException("VectorLayer can't be deserialized: " + ex.Message, ex); } //LayerDbGuid tempNode = vectorLayerElement.SelectSingleNode("LayerDbGuid"); if (tempNode == null) { throw new VectorLayerDeserializationException("VectorLayer can't be deserialized: 'LayerElement' don't have element 'LayerDbGuid'!"); } var layerDbGuid = new Guid(tempNode.InnerText); //LabelLayer Guid tempNode = vectorLayerElement.SelectSingleNode("LabelLayerGuid"); if (tempNode == null) { throw new VectorLayerDeserializationException("VectorLayer can't be deserialized: 'LayerElement' don't have element 'LabelLayerGuid'!"); } var labelLayerGuid = new Guid(tempNode.InnerText); //create layer switch (vectorLayerElement.Attributes["Type"].Value) { case "eidss.gis.Layers.EidssUserDbLayer": resultVectorLayer = new EidssUserDbLayer(layerDbGuid, lyrInfo.LayerName, lyrInfo.Guid, labelLayerGuid); break; case "eidss.gis.Layers.EidssUserBufZoneLayer": resultVectorLayer = new EidssUserBufZoneLayer(layerDbGuid, lyrInfo.LayerName, lyrInfo.Guid, labelLayerGuid); break; } resultVectorLayer.ControledByUser = lyrInfo.ControledByUser; resultVectorLayer.Enabled = lyrInfo.Enabled; resultVectorLayer.MaxVisible = lyrInfo.MaxVisible; resultVectorLayer.MinVisible = lyrInfo.MinVisible; //resultVectorLayer.SRID = lyrInfo.SRID; - тут бля косяк в провайдере! нет проверки! resultVectorLayer.VisibleInTOC = lyrInfo.VisibleInTOC; resultVectorLayer.Transparency = lyrInfo.Transparency; #endregion #region << Deserialize >> //ClippingEnabled tempNode = vectorLayerElement.SelectSingleNode("ClippingEnabled"); if (tempNode == null) { throw new VectorLayerDeserializationException("'VectorLayerElement' don't have element 'ClippingEnabled'!"); } if (!bool.TryParse(tempNode.InnerText, out tempBool)) { throw new VectorLayerDeserializationException("Can't parse node 'ClippingEnabled'!"); } resultVectorLayer.ClippingEnabled = tempBool; //SmoothingMode tempNode = vectorLayerElement.SelectSingleNode("SmoothingMode"); if (tempNode == null) { throw new VectorLayerDeserializationException("'VectorLayerElement' don't have element 'SmoothingMode'!"); } if (!Int32.TryParse(tempNode.InnerText, out tempInt) || !Enum.IsDefined(typeof(SmoothingMode), tempInt)) { throw new VectorLayerDeserializationException("Can't parse node 'SmoothingMode'!"); } resultVectorLayer.SmoothingMode = (SmoothingMode)tempInt; //SRID tempNode = vectorLayerElement.SelectSingleNode("SRID"); if (tempNode == null) { throw new VectorLayerDeserializationException("'VectorLayerElement' don't have element 'SRID'!"); } if (!Int32.TryParse(tempNode.InnerText, out tempInt)) { throw new VectorLayerDeserializationException("Can't parse node 'SRID'!"); } resultVectorLayer.SRID = tempInt; //Style try { resultVectorLayer.Style = (VectorStyle)StyleSerializer.DeserializeFromNode(vectorLayerElement); } catch (Exception ex) { throw new VectorLayerSerializationException("Style can't be deserialized: " + ex.Message, ex); } //Theme try { resultVectorLayer.Theme = ThemeSerializer.DeserializeFromNode(vectorLayerElement); } catch (Exception ex) { throw new VectorLayerSerializationException("Theme can't be deserialized: " + ex.Message, ex); } //Marker size tempNode = vectorLayerElement.SelectSingleNode("MarkerSize"); if (tempNode == null) { throw new VectorLayerDeserializationException("'VectorLayerElement' don't have element 'MarkerSize'!"); } if (!Int32.TryParse(tempNode.InnerText, out tempInt)) { throw new VectorLayerDeserializationException("Can't parse node 'MarkerSize'!"); } resultVectorLayer.MarkerSize = tempInt; //Marker color try { resultVectorLayer.MarkerColor = ColorSerializer.DeserializeFromNode(vectorLayerElement, "MarkerColor"); } catch (Exception ex) { throw new VectorLayerSerializationException("MarkerColor can't be deserialized: " + ex.Message, ex); } //Polygon Fill Style tempNode = vectorLayerElement.SelectSingleNode("PolygonFillStyle"); if (tempNode == null) { throw new VectorLayerDeserializationException("'VectorLayerElement' don't have element 'PolygonFillStyle'!"); } resultVectorLayer.PolygonFillStyle = tempNode.InnerText; //Polygon Fill Color try { resultVectorLayer.PolygonFillColor = ColorSerializer.DeserializeFromNode(vectorLayerElement, "PolygonFillColor"); } catch (Exception ex) { throw new VectorLayerSerializationException("PolygonFillColor can't be deserialized: " + ex.Message, ex); } #endregion return(resultVectorLayer); }
// Read a single value from JSON public static object ReadJSONValue(Jboy.JsonReader reader, FieldInfo field) { var fieldType = field.FieldType; if (fieldType == typeof(int)) { return((int)(reader.ReadNumber())); } else if (fieldType == typeof(uint)) { return((uint)(reader.ReadNumber())); } else if (fieldType == typeof(long)) { return((long)(reader.ReadNumber())); } else if (fieldType == typeof(byte)) { return((byte)(reader.ReadNumber())); } else if (fieldType == typeof(double)) { return(reader.ReadNumber()); } else if (fieldType == typeof(float)) { return((float)reader.ReadNumber()); } else if (fieldType == typeof(KeyCode)) { return((KeyCode)(reader.ReadNumber())); } else if (fieldType == typeof(string)) { string stringObject; if (reader.TryReadString(out stringObject)) { return(stringObject); } else { reader.ReadNull(); return(null); } } else if (fieldType == typeof(int[])) { return(Jboy.Json.ReadObject <int[]>(reader)); } else if (fieldType == typeof(Color)) { return(ColorSerializer.JsonDeserializer(reader)); } else if (fieldType == typeof(ServerType)) { return(ServerTypeSerializer.JsonDeserializer(reader)); } else if (fieldType == typeof(PlayerQueueStats)) { return(GenericSerializer.ReadJSONClassInstance <PlayerQueueStats>(reader)); } else if (fieldType == typeof(PlayerQueueStats[])) { reader.ReadArrayStart(); PlayerQueueStats[] valArray = new PlayerQueueStats[QueueSettings.queueCount]; for (int i = 0; i < QueueSettings.queueCount; i++) { valArray[i] = GenericSerializer.ReadJSONClassInstance <PlayerQueueStats>(reader); } reader.ReadArrayEnd(); return(valArray); } else if (fieldType == typeof(InputControl[])) { reader.ReadArrayStart(); List <InputControl> valList = new List <InputControl>(); while (true) { try { valList.Add(GenericSerializer.ReadJSONClassInstance <InputControl>(reader)); } catch { break; } } reader.ReadArrayEnd(); return(valList.ToArray()); } else if (fieldType == typeof(Artifact)) { return(Jboy.Json.ReadObject <Artifact>(reader)); } else if (fieldType == typeof(ArtifactSlot)) { return(Jboy.Json.ReadObject <ArtifactSlot>(reader)); } else if (fieldType == typeof(ArtifactTree)) { return(Jboy.Json.ReadObject <ArtifactTree>(reader)); } else if (fieldType == typeof(ArtifactInventory)) { return(Jboy.Json.ReadObject <ArtifactInventory>(reader)); } else if (fieldType == typeof(List <ItemSlot>)) { return(Jboy.Json.ReadObject <List <ItemSlot> >(reader)); } else if (fieldType == typeof(TimeStamp)) { return(Jboy.Json.ReadObject <TimeStamp>(reader)); } else if (fieldType == typeof(SkillBuild)) { return(Jboy.Json.ReadObject <SkillBuild>(reader)); //} else if(fieldType == typeof(WeaponBuild)) { // return Jboy.Json.ReadObject<WeaponBuild>(reader); //} else if(fieldType == typeof(AttunementBuild)) { // return Jboy.Json.ReadObject<AttunementBuild>(reader); } else if (fieldType == typeof(WeaponBuild[])) { return(Jboy.Json.ReadObject <WeaponBuild[]>(reader)); } else if (fieldType == typeof(AttunementBuild[])) { return(Jboy.Json.ReadObject <AttunementBuild[]>(reader)); } else if (fieldType == typeof(Guild)) { return(GenericSerializer.ReadJSONClassInstance <Guild>(reader)); } else if (fieldType == typeof(GuildMember)) { return(GenericSerializer.ReadJSONClassInstance <GuildMember>(reader)); } else if (fieldType == typeof(GuildMember[])) { return(Jboy.Json.ReadObject <GuildMember[]>(reader)); } else if (fieldType == typeof(List <string>)) { return(Jboy.Json.ReadObject <List <string> >(reader)); } else if (fieldType == typeof(List <Friend>)) { return(Jboy.Json.ReadObject <List <Friend> >(reader)); } else if (fieldType == typeof(List <FriendsGroup>)) { return(Jboy.Json.ReadObject <List <FriendsGroup> >(reader)); } else if (fieldType == typeof(Texture2D)) { return(Texture2DSerializer.JsonDeserializer(reader)); } else { LogManager.General.LogError("Unknown field type for GenericSerializer.ReadJSONValue: " + fieldType); return((int)(reader.ReadNumber())); } }