Mycroft.Messages.Msg.MsgQuery.Serialize C# (CSharp) Method

Serialize() public method

public Serialize ( ) : string
return string
        public override string Serialize()
        {
            var dct = new Dictionary<string, object>();
            dct["id"] = Id;
            dct["capability"] = Capability;
            dct["action"] = Action;
            dct["data"] = Data;
            dct["instanceId"] = InstanceId;
            dct["priority"] = Priority;
            dct["fromInstanceId"] = FromInstanceId;
            var obj = new DynamicJsonObject(dct);
            var writer = new StringWriter();
            Json.Write(obj, writer);
            return writer.ToString();
        }

Usage Example

Beispiel #1
0
        public void TestMsgQuerySerialization()
        {
            var msgQuery = new MsgQuery();
            msgQuery.Id = "uuid";
            msgQuery.Capability = "weather";
            msgQuery.Action = "get_temperature";
            var instanceIds = new List<string>();
            instanceIds.Add("xxxx");
            instanceIds.Add("xx2");
            msgQuery.InstanceId = instanceIds;
            var data = new Dictionary<string, object>();
            data["scale"] = "fahrenheit";
            var inner = new Dictionary<string, object>();
            inner["k"] = "v";
            data["other"] = inner;
            msgQuery.Data = data;

            string json = msgQuery.Serialize();
            Debug.WriteLine(json);

            Assert.IsFalse(json.IndexOf("\"data\":null") >= 0, "Sould not contain null for data");
            Assert.IsFalse(json.IndexOf("KeyValuePairOf") >= 0, "Should not have strange toString");
            Assert.IsFalse(json.IndexOf("\"data\":[]") >= 0, "Should not have empty array for data");
        }