Beispiel #1
0
        public override string Serialize(Node node, Type typeAttr)
        {
            XpcaProxy proxy = new XpcaProxy(node);
            StringWriter sw = new StringWriter();

            using(JsonWriter jsonWriter = new JsonTextWriter(sw))
            {
                jsonWriter.Formatting = Newtonsoft.Json.Formatting.None;
                jsonWriter.WriteStartObject();

                foreach (KeyValuePair<string, PropertyInfo> property in proxy.GetPropertiesFor(typeAttr)) {
                    object value = proxy[property.Key];

                    if (value != null) {
                        jsonWriter.WritePropertyName(property.Key);
                        if(value is IEnumerable<object>) {
                            jsonWriter.WriteStartArray();

                            foreach (object obj in (value as IEnumerable<object>)) {
                                JsonWriteValue(jsonWriter, obj);
                            }

                            jsonWriter.WriteEndArray();
                        }
                        else {
                            JsonWriteValue(jsonWriter, value);
                        }
                    }
                }
                jsonWriter.WriteEndObject();
            }

            return sw.ToString();
        }
Beispiel #2
0
        public override void Deserialize(Node node, string data)
        {
            TextReader tr = new StringReader(data);
            Dictionary<string, object> properties = new Dictionary<string, object>();

            using(JsonTextReader jsonReader = new JsonTextReader(tr))
            {
                while (jsonReader.Read()){
                    if (jsonReader.TokenType == JsonToken.PropertyName) {
                        string name = jsonReader.Value.ToString();
                        if (jsonReader.Read()) {
                            switch (jsonReader.TokenType) {
                            case JsonToken.String:
                            case JsonToken.Boolean:
                            case JsonToken.Date:
                            case JsonToken.Integer:
                                properties.Add(name, jsonReader.Value);
                                break;
                            case JsonToken.StartArray:
                                List<object> objs = new List<object>();
                                while(jsonReader.Read() && jsonReader.TokenType != JsonToken.EndArray) {
                                    objs.Add(jsonReader.Value);
                                }
                                properties.Add(name, objs.ToArray());
                                break;
                            }

                        }

                    }
                }
            }

            UpdateNode (node, properties);
        }
Beispiel #3
0
 public void SetUp()
 {
     parentNode1 = new Node("parentNode1");
     parentNode2 = new Node("parentNode2");
     node = new Node("node");
     node.Parent = parentNode1;
 }
Beispiel #4
0
        public void Build(string fullName, NameValueCollection parms)
        {
            Node node;
            string[] splitUrl = fullName.Split('/');
            string name = splitUrl[splitUrl.Length -1];
            string parentPath = String.Join("/", splitUrl, 0, splitUrl.Length - 1);

            if (parms["type"] == null) {
                node = new Node();
            }
            else {
                    Type type = srv.Types.Find(delegate (Type t) {
                        return t.Name == parms["type"];
                    });
                if (type != null) {
                    node = Activator.CreateInstance(type, new object[] {}) as Node;
                }
                else {
                    throw new XpcaTypeError(parms["type"]);
                }
            }

            node.Name = name;
            srv[parentPath] = node;
            hasConfigChanges = true;

            Update(fullName, parms);
        }
Beispiel #5
0
        public override void Deserialize(Node node, string data)
        {
            TextReader tr = new StringReader(data);
            Dictionary<string, object> properties = new Dictionary<string, object>();

            using(XmlReader xmlReader = new XmlTextReader(tr))
            {
                xmlReader.ReadStartElement("root");
                do {
                    if (xmlReader.NodeType == XmlNodeType.Element) {
                        string name = xmlReader.Name;
                        if (xmlReader.Read()) {
                            switch (xmlReader.NodeType) {
                            case XmlNodeType.Text:
                                properties.Add(name, xmlReader.ReadContentAs(xmlReader.ValueType, null));
                                break;
                            case XmlNodeType.Element:
                                List<object> objs = new List<object>();
                                while(xmlReader.Read() && xmlReader.NodeType != XmlNodeType.EndElement) {
                                    if (xmlReader.Name == "item")
                                        objs.Add(xmlReader.ReadElementContentAs(xmlReader.ValueType, null));
                                }
                                properties.Add(name, objs.ToArray());
                                break;
                            }

                        }

                    }
                } while(xmlReader.Read());
            }

            UpdateNode (node, properties);
        }
Beispiel #6
0
        public override string Serialize(Node node, Type typeAttr)
        {
            XpcaProxy proxy = new XpcaProxy(node);
            StringWriter sw = new StringWriter();

            using(XmlWriter xmlWriter = new XmlTextWriter(sw))
            {
                xmlWriter.WriteProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\"");
                xmlWriter.WriteStartElement("root");
                foreach (KeyValuePair<string, PropertyInfo> property in proxy.GetPropertiesFor(typeAttr)) {

                    object value = proxy[property.Key];
                    if (value != null) {
                        xmlWriter.WriteStartElement(property.Key);
                        if(value is IEnumerable<object>) {
                            foreach (object obj in (value as IEnumerable<object>)) {
                                xmlWriter.WriteStartElement("item");
                                XmlWriteValue(xmlWriter, obj);
                                xmlWriter.WriteEndElement();
                            }

                        }
                        else {
                            XmlWriteValue(xmlWriter, value);
                        }
                        xmlWriter.WriteEndElement();
                    }
                }
                xmlWriter.WriteEndElement();
            }

            return sw.ToString();
        }
Beispiel #7
0
        public void Catch(Node node)
        {
            this.node = node;

            NodeAttribute nodeAttr =  Array.Find(node.GetType().GetCustomAttributes(false), delegate (object attr) {
                return attr is NodeAttribute;
            }) as NodeAttribute;

            if (nodeAttr != null) {
                typeName = node.GetType().Name;
            }
            else {
                throw new XpcaTypeError(node.GetType().Name);
            }

            // Getting XPCA properties from type

            List<PropertyInfo> props = new List<PropertyInfo>();

            GetProperties(node.GetType(), props);
            props.RemoveAll(delegate(PropertyInfo p){
                return GetAttribute(p) == null;
            });

            properties = new Dictionary<string, PropertyInfo>();
            foreach (PropertyInfo property in props) {
                properties.Add(
                    property.Name.Substring(0,1).ToLower() + property.Name.Substring(1),
                    property
                );
            }
        }
        public void SetUp()
        {
            root = new Root();
            root["/"] = new Node("node_1");
            root["/node_1"] = new Node("node_2");

            serializer = new JsonSerializer(typeof(Node));
        }
Beispiel #9
0
 protected void UpdateNode(Node node, Dictionary<string, object> properties)
 {
     proxy = new XpcaProxy(node);
     //Set properties
     foreach (KeyValuePair<string, object> property in properties) {
         object value = property.Value;
         if (value.ToString().IndexOf("xpca:/") > -1) {
             value = proxy.Node.Root[value.ToString().Replace("xpca:/", "")];
         }
         proxy[property.Key] = value;
     }
 }
Beispiel #10
0
 private void OnConfig(Node node)
 {
     queueToSave.Enqueue(node);
     saveTimer.Stop();
     saveTimer.Start();
 }
Beispiel #11
0
 private void OnConfig(Node node)
 {
     if (ConfigChange != null) {
         ConfigChange(node);
         hasConfigChanges = false;
     }
 }
Beispiel #12
0
 public abstract string Serialize(Node node, Type typeAttr);
Beispiel #13
0
 public string Serialize(Node node)
 {
     return Serialize(node, typeof(PropertyAttribute));
 }
Beispiel #14
0
 public abstract void Deserialize(Node node, string data);
Beispiel #15
0
 public void Add(string path, Node node)
 {
     node.Parent = Get(path);
 }
Beispiel #16
0
        private void Save(Node node, JsonTextWriter jsonWriter)
        {
            jsonWriter.Formatting = Formatting.Indented;
            jsonWriter.WritePropertyName(node.FullName);
            jsonWriter.WriteStartObject();
            XpcaProxy proxy = new XpcaProxy(node);

            foreach (KeyValuePair<string, PropertyInfo> property in proxy.GetPropertiesFor(typeof(ConfigAttribute))) {

                object value = proxy[property.Key];

                if (value != null ) {
                    jsonWriter.WritePropertyName(property.Key);
                    if(value is Node) {
                        jsonWriter.WriteValue("xpca:/" + ((Node)value).FullName);
                    }
                    else {
                        jsonWriter.WriteValue(value.ToString());
                    }
                }
            }

            jsonWriter.WriteEndObject();

            foreach(Node ch in node.Children) {
                Save(ch, jsonWriter);
            }
        }
Beispiel #17
0
 public XpcaProxy(Node node)
 {
     Catch(node);
 }