/* * Experiment with Unit support for deserialization */ private static void UnitDeserializationSupportExperiments() { Unit u = new Unit(); var contentPool = new KC_ContentPool("test", true); var utility = new KC_Utility(1.0); var prologAddList = new KC_PrologFactAddList(new string[] { "one", "two", "three" }, false); var idRequest = new KC_IDSelectionRequest("foo"); u.AddComponent(contentPool); u.AddComponent(utility); u.AddComponent(prologAddList); u.AddComponent(idRequest); string output = u.SerializeToJson(); Console.WriteLine(output); Unit deserializedUnit = Unit.DeserializeFromJson(output); Console.WriteLine(deserializedUnit); }
/* * Experimenting with approaches for serializing and deserializing Units and KnowledgeComponents. */ private static void TestJson() { Unit u = new Unit(); var contentPool = new KC_ContentPool("test", true); var utility = new KC_Utility(1.0); var prologAddList = new KC_PrologFactAddList(new string[] { "one", "two", "three" }, false); var idRequest = new KC_IDSelectionRequest("foo"); u.AddComponent(contentPool); u.AddComponent(utility); u.AddComponent(prologAddList); u.AddComponent(idRequest); /* * This code serializes and deserializes individual KnoweldgeComponents, demonstrating figuring out the type * based on looking at the properties in JObject. */ string contentPoolOutput = JsonConvert.SerializeObject(contentPool, Formatting.Indented); Console.WriteLine(contentPoolOutput); JObject joContentPool = (JObject)JsonConvert.DeserializeObject(contentPoolOutput); //foreach (JToken token in jo.Children()) //{ // Console.WriteLine(token); //} Console.WriteLine(ConvertJObjectToKC(joContentPool)); string utilityOutput = JsonConvert.SerializeObject(utility, Formatting.Indented); Console.WriteLine(utilityOutput); JObject joUtility = (JObject)JsonConvert.DeserializeObject(utilityOutput); Console.WriteLine(ConvertJObjectToKC(joUtility)); string prologAddListOutput = JsonConvert.SerializeObject(prologAddList, Formatting.Indented); Console.WriteLine(prologAddListOutput); JObject joPrologAddList = (JObject)JsonConvert.DeserializeObject(prologAddListOutput); Console.WriteLine(ConvertJObjectToKC(joPrologAddList)); string idRequestOutput = JsonConvert.SerializeObject(idRequest, Formatting.Indented); Console.WriteLine(idRequestOutput); JObject joIdRequest = (JObject)JsonConvert.DeserializeObject(idRequestOutput); Console.WriteLine(ConvertJObjectToKC(joIdRequest)); /* * this code serializes and deserializes Units. */ string output = JsonConvert.SerializeObject(u.GetComponents(), Formatting.Indented); // new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All } ); Console.WriteLine(output); var joList = JsonConvert.DeserializeObject <IList <JObject> >(output); Console.WriteLine("Writing list of knowledge components as JObjects"); foreach (JObject jo in joList) { Console.WriteLine(jo); } /* * Creating Unit by iterating list of JObjects and calling ConvertJObjectToKC on each, then adding KnowledgeComponet to * unit. */ Unit deserializedUnit = new Unit(); foreach (JObject jo in joList) { KnowledgeComponent kc = ConvertJObjectToKC(jo); deserializedUnit.AddComponent(kc); } Console.WriteLine(deserializedUnit); }