/// <summary> /// Deserialize a json string to a Model instance /// </summary> /// <param name="payload">json string</param> /// <returns>Model instance</returns> public static Model DeserializeModel(string payload) { JObject root = JObject.Parse(payload); List <ObjectType> objectTypes = new List <ObjectType>(); Dictionary <ObjectAttributeFields, List <string> > allObjectAttributes = new Dictionary <ObjectAttributeFields, List <string> >(); foreach (JObject rawOt in root["objectTypes"]) { string key = rawOt["key"].ToObject <string>(); string description = rawOt["description"].ToObject <string>(); List <string> objectAttributeKeys = new List <string>(); foreach (JObject rawObjectAttributes in rawOt["objectAttributes"]) { string objKey = rawObjectAttributes["key"].ToObject <string>(); string objDescription = rawObjectAttributes["description"].ToObject <string>(); string objDisplayName = rawObjectAttributes["displayName"].ToObject <string>(); string objHighLevelType = rawObjectAttributes["type"]["highLevelType"].ToObject <string>(); string objContainerType = rawObjectAttributes["type"]["containerType"].ToObject <string>(); objectAttributeKeys.Add(objKey); ObjectAttributeFields objFields = Tuple.Create(objKey, objDisplayName, objDescription, objHighLevelType, objContainerType); if (allObjectAttributes.ContainsKey(objFields)) { allObjectAttributes[objFields].Add(key); } else { allObjectAttributes.Add(objFields, new List <string>() { key }); } } objectTypes.Add(new ObjectType(key, description, objectAttributeKeys)); } List <EventType> eventTypes = new List <EventType>(); Dictionary <TimeseriesFields, List <string> > allTimeseries = new Dictionary <TimeseriesFields, List <string> >(); foreach (JObject rawEt in root["eventTypes"]) { string key = rawEt["key"].ToObject <string>(); string description = rawEt["description"].ToObject <string>(); string origin = rawEt["origin"].ToObject <string>(); List <string> timeseriesKeys = new List <string>(); foreach (JObject rawTimeseries in rawEt["timeseries"]) { string tsKey = rawTimeseries["key"].ToObject <string>(); string tsDescription = rawTimeseries["description"].ToObject <string>(); string tsDisplayName = rawTimeseries["displayName"].ToObject <string>(); string tsHighLevelType = rawTimeseries["type"]["highLevelType"].ToObject <string>(); timeseriesKeys.Add(tsKey); TimeseriesFields tsFields = Tuple.Create(tsKey, tsDisplayName, tsDescription, tsHighLevelType); if (allTimeseries.ContainsKey(tsFields)) { allTimeseries[tsFields].Add(key); } else { allTimeseries.Add(tsFields, new List <string>() { key }); } } eventTypes.Add(new EventType(key, description, origin, timeseriesKeys)); } List <OwnerAttribute> ownerAttributes = new List <OwnerAttribute>(); foreach (JObject rawOwner in root["ownerAttributes"]) { string key = rawOwner["key"].ToObject <string>(); string description = rawOwner["description"].ToObject <string>(); string displayName = rawOwner["displayName"].ToObject <string>(); string highLevelType = rawOwner["type"]["highLevelType"].ToObject <string>(); string containerType = rawOwner["type"]["containerType"].ToObject <string>(); OwnerAttribute owner = new OwnerAttribute(key, displayName, description, highLevelType, containerType); ownerAttributes.Add(owner); } List <Sessionizer> sessionizers = new List <Sessionizer>(); foreach (JObject rawSessionizer in root["sessionizers"]) { string key = rawSessionizer["key"].ToObject <string>(); string description = rawSessionizer["description"].ToObject <string>(); string displayName = rawSessionizer["displayName"].ToObject <string>(); string startEventTypeKey = rawSessionizer["startEventTypeKey"].ToObject <string>(); string endEventTypeKey = rawSessionizer["endEventTypeKey"].ToObject <string>(); Sessionizer sess = new Sessionizer(key, displayName, description, startEventTypeKey, endEventTypeKey); sessionizers.Add(sess); } JToken rawOrphan = root["orphans"]; List <ObjectAttribute> orphanObjects = new List <ObjectAttribute>(); JToken orphanObjectsNode = rawOrphan["objectAttributes"]; if (orphanObjectsNode != null) { foreach (JObject rawObjectAttributes in orphanObjectsNode) { string key = rawObjectAttributes["key"].ToObject <string>(); string description = rawObjectAttributes["description"].ToObject <string>(); string displayName = rawObjectAttributes["displayName"].ToObject <string>(); string highLevelType = rawObjectAttributes["type"]["highLevelType"].ToObject <string>(); string containerType = rawObjectAttributes["type"]["containerType"].ToObject <string>(); ObjectAttribute obj = new ObjectAttribute(key, displayName, description, highLevelType, containerType, new List <string>() { }); orphanObjects.Add(obj); } } List <Timeseries> orphanTimeseries = new List <Timeseries>(); JToken orphanTsNode = rawOrphan["timeseries"]; if (orphanTsNode != null) { foreach (JObject rawTimeseries in orphanTsNode) { string key = rawTimeseries["key"].ToObject <string>(); string description = rawTimeseries["description"].ToObject <string>(); string displayName = rawTimeseries["displayName"].ToObject <string>(); string highLevelType = rawTimeseries["type"]["highLevelType"].ToObject <string>(); Timeseries ts = new Timeseries(key, displayName, description, highLevelType, new List <string>() { }); orphanTimeseries.Add(ts); } } List <ObjectAttribute> objectAttributes = new List <ObjectAttribute>(); foreach (KeyValuePair <ObjectAttributeFields, List <string> > entry in allObjectAttributes) { ObjectAttributeFields objFields = entry.Key; List <string> objTypeKeys = entry.Value; ObjectAttribute obj = new ObjectAttribute(objFields.Item1, objFields.Item2, objFields.Item3, objFields.Item4, objFields.Item5, objTypeKeys); objectAttributes.Add(obj); } List <Timeseries> timeseries = new List <Timeseries>(); foreach (KeyValuePair <TimeseriesFields, List <string> > entry in allTimeseries) { TimeseriesFields tsFields = entry.Key; List <string> eventTypeKeys = entry.Value; Timeseries ts = new Timeseries(tsFields.Item1, tsFields.Item2, tsFields.Item3, tsFields.Item4, eventTypeKeys); timeseries.Add(ts); } return(new Model( eventTypes, objectTypes, timeseries, objectAttributes, ownerAttributes, sessionizers, new Orphans(orphanTimeseries, orphanObjects) )); }
public void TestDeserialize() { string json = @" { ""objectTypes"": [ { ""key"": ""object_type1"", ""description"": ""desc"", ""objectAttributes"": [ { ""key"": ""object_text_attribute"", ""displayName"": ""dp object_text_attribute"", ""description"": ""desc object_text_attribute"", ""type"": { ""highLevelType"": ""TEXT"", ""containerType"": ""none"" } }, { ""key"": ""object_int_attribute"", ""displayName"": ""dp object_int_attribute"", ""description"": ""desc object_int_attribute"", ""type"": { ""highLevelType"": ""INT"", ""containerType"": ""list"" } } ] } ], ""eventTypes"": [ { ""key"": ""event_type1"", ""description"": ""desc"", ""origin"": ""scheduled"", ""timeseries"": [ { ""key"": ""ts_number_attribute"", ""displayName"": ""dp ts_number_attribute"", ""description"": ""desc ts_number_attribute"", ""type"": { ""highLevelType"": ""DOUBLE"" } }, { ""key"": ""ts_text_attribute"", ""displayName"": ""dp ts_text_attribute"", ""description"": ""desc ts_text_attribute"", ""type"": { ""highLevelType"": ""TEXT"" } } ] }, { ""key"": ""event_type2"", ""description"": ""desc"", ""origin"": ""rule"", ""timeseries"": [ { ""key"": ""ts_text_attribute"", ""displayName"": ""dp ts_text_attribute"", ""description"": ""desc ts_text_attribute"", ""type"": { ""highLevelType"": ""TEXT"" } } ] } ], ""ownerAttributes"": [ { ""key"": ""owner_text_attribute"", ""displayName"": ""dp owner_text_attribute"", ""description"": ""desc owner_text_attribute"", ""type"": { ""highLevelType"": ""TEXT"", ""containerType"": ""none"" } } ], ""sessionizers"": [ { ""key"": ""sessionizer"", ""displayName"": ""dp sessionizer"", ""description"": ""desc sessionizer"", ""startEventTypeKey"": ""event_type1"", ""endEventTypeKey"": ""event_type2"" } ], ""orphans"": { ""timeseries"": [ { ""key"": ""orphan_ts"", ""displayName"": ""dp orphan_ts"", ""description"": ""desc orphan_ts"", ""type"": { ""highLevelType"": ""ACCELERATION"" } } ], ""objectAttributes"": [ { ""key"": ""orphan_object"", ""displayName"": ""dp orphan_object"", ""description"": ""desc orphan_object"", ""type"": { ""highLevelType"": ""EMAIL"", ""containerType"": ""none"" } } ] } } "; Model model = ModelDeserializer.DeserializeModel(json); Assert.AreEqual(2, model.EventTypes.Count, "event types count"); Assert.AreEqual(2, model.Timeseries.Count, "timeseries count"); Assert.AreEqual(1, model.ObjectTypes.Count, "object types count"); Assert.AreEqual(2, model.ObjectAttributes.Count, "object attributes count"); Assert.AreEqual(1, model.OwnerAttributes.Count, "owner attributes count"); Assert.AreEqual(1, model.Sessionizers.Count, "sessionizers count"); EventType type1 = model.EventTypes[0]; Assert.AreEqual("event_type1", type1.Key); Assert.AreEqual("desc", type1.Description); Assert.AreEqual("scheduled", type1.Origin); Assert.AreEqual(new List <string>() { "ts_number_attribute", "ts_text_attribute" }, type1.TimeseriesKeys); EventType type2 = model.EventTypes[1]; Assert.AreEqual("event_type2", type2.Key); Assert.AreEqual("desc", type2.Description); Assert.AreEqual("rule", type2.Origin); Assert.AreEqual(new List <string>() { "ts_text_attribute" }, type2.TimeseriesKeys); ObjectType oType = model.ObjectTypes[0]; Assert.AreEqual("object_type1", oType.Key); Assert.AreEqual("desc", oType.Description); Assert.AreEqual(new List <string>() { "object_text_attribute", "object_int_attribute" }, oType.ObjectAttributeKeys); ObjectAttribute object1 = model.ObjectAttributes[0]; Assert.AreEqual("object_text_attribute", object1.Key); Assert.AreEqual("dp object_text_attribute", object1.DisplayName); Assert.AreEqual("desc object_text_attribute", object1.Description); Assert.AreEqual("TEXT", object1.HighLevelType); Assert.AreEqual("none", object1.ContainerType); Assert.AreEqual(new List <string>() { "object_type1" }, object1.ObjectTypeKeys); ObjectAttribute object2 = model.ObjectAttributes[1]; Assert.AreEqual("object_int_attribute", object2.Key); Assert.AreEqual("dp object_int_attribute", object2.DisplayName); Assert.AreEqual("desc object_int_attribute", object2.Description); Assert.AreEqual("INT", object2.HighLevelType); Assert.AreEqual("list", object2.ContainerType); Assert.AreEqual(new List <string>() { "object_type1" }, object2.ObjectTypeKeys); Timeseries ts1 = model.Timeseries[0]; Assert.AreEqual("ts_number_attribute", ts1.Key); Assert.AreEqual("dp ts_number_attribute", ts1.DisplayName); Assert.AreEqual("desc ts_number_attribute", ts1.Description); Assert.AreEqual("DOUBLE", ts1.HighLevelType); Assert.AreEqual(new List <string>() { "event_type1" }, ts1.EventTypeKeys); Timeseries ts2 = model.Timeseries[1]; Assert.AreEqual("ts_text_attribute", ts2.Key); Assert.AreEqual("dp ts_text_attribute", ts2.DisplayName); Assert.AreEqual("desc ts_text_attribute", ts2.Description); Assert.AreEqual("TEXT", ts2.HighLevelType); Assert.AreEqual(new List <string>() { "event_type1", "event_type2" }, ts2.EventTypeKeys); OwnerAttribute owner = model.OwnerAttributes[0]; Assert.AreEqual("owner_text_attribute", owner.Key); Assert.AreEqual("dp owner_text_attribute", owner.DisplayName); Assert.AreEqual("desc owner_text_attribute", owner.Description); Assert.AreEqual("TEXT", owner.HighLevelType); Assert.AreEqual("none", owner.ContainerType); Sessionizer sess = model.Sessionizers[0]; Assert.AreEqual("sessionizer", sess.Key); Assert.AreEqual("dp sessionizer", sess.DisplayName); Assert.AreEqual("desc sessionizer", sess.Description); Assert.AreEqual("event_type1", sess.StartEventTypeKey); Assert.AreEqual("event_type2", sess.EndEventTypeKey); ObjectAttribute orphanObj = model.Orphans.ObjectAttributes[0]; Assert.AreEqual("orphan_object", orphanObj.Key); Assert.AreEqual("dp orphan_object", orphanObj.DisplayName); Assert.AreEqual("desc orphan_object", orphanObj.Description); Assert.AreEqual("EMAIL", orphanObj.HighLevelType); Assert.AreEqual("none", orphanObj.ContainerType); Assert.AreEqual(new List <string>(), orphanObj.ObjectTypeKeys); Timeseries orphanTs = model.Orphans.Timeseries[0]; Assert.AreEqual("orphan_ts", orphanTs.Key); Assert.AreEqual("dp orphan_ts", orphanTs.DisplayName); Assert.AreEqual("desc orphan_ts", orphanTs.Description); Assert.AreEqual("ACCELERATION", orphanTs.HighLevelType); Assert.AreEqual(new List <string>(), orphanTs.EventTypeKeys); }