public void AJson_JsonBuilding_CreatingArraysNotList() { var twa_input = new ThingWithArray() { Names = new[] { "Bob", "Joe", "McFartpantz" } }; Json json = JsonHelper.BuildJsonForObject(twa_input); Assert.IsNotNull(json); Assert.IsFalse(json.HasErrors, "Json parse errors:\n" + String.Join("\n\t", json.Errors)); var twa_output = JsonHelper.BuildObjectForJson <ThingWithArray>(json); Assert.IsNotNull(twa_output); Assert.IsInstanceOfType(twa_output, typeof(ThingWithArray)); Assert.IsNotNull(twa_output.Names); Assert.AreEqual(3, twa_output.Names.Length); Assert.AreEqual("Bob", twa_output.Names[0]); Assert.AreEqual("Joe", twa_output.Names[1]); Assert.AreEqual("McFartpantz", twa_output.Names[2]); }
public void AJson_JsonHelper_BuildObjectForJson_ComplexObject() { ComplexGuy theDudeGoinIn = ComplexGuy.MakeOne(); Json json = JsonHelper.BuildJsonForObject(theDudeGoinIn); ComplexGuy theDude = JsonHelper.BuildObjectForJson <ComplexGuy>(json); Assert.IsTrue(theDudeGoinIn.Equals(theDude)); json.AssertSourceIsValid(); }
/// <summary> /// Constructs a <see cref="Stratabase"/> from the json previously serialized by a stratabase /// </summary> public static Stratabase DeserializeFromJson(Json json) { var data = JsonHelper.BuildObjectForJson <StratabaseDataModel>(json); if (data == null) { return(null); } return(new Stratabase(data)); }
public void AJson_GeneralParsing_JsonValue_EnumValuesCanBeRead() { Json json = JsonHelper.ParseText("{ \"Item\": Sweet }"); Assert.IsNotNull(json); Assert.IsFalse(json.HasErrors, "Json parse errors:\n" + String.Join("\n\t", json.Errors)); Assert.IsTrue(json.Data.IsDocument); Test t = JsonHelper.BuildObjectForJson <Test>(json); Assert.IsNotNull(t); Assert.AreEqual(eTest.Sweet, t.Item); }
public void AJson_JsonBuilding_CanBuildObjectPropertyOfSimpleTypes() { double pi = 3.14159; RuntimeTypeEvaluatorObject obj = new RuntimeTypeEvaluatorObject(); obj.Obj = pi; Json json = JsonHelper.BuildJsonForObject(obj); Assert.IsNotNull(json); Assert.IsFalse(json.HasErrors, json.BuildJsonErrorReport()); var result = JsonHelper.BuildObjectForJson <RuntimeTypeEvaluatorObject>(json); Assert.IsTrue(result.Obj is double, "Result is not a double"); Assert.AreEqual(pi, (double)result.Obj); }
public void AJson_JsonBuilding_DeserializeJsonToObject_WorksWithSimpleGuy() { SimpleGuy guy = new SimpleGuy(); guy.What = 2; guy.Noice = 3; Json json = JsonHelper.BuildJsonForObject(guy); Assert.IsNotNull(json); Assert.IsFalse(json.HasErrors, "Json parse errors:\n" + String.Join("\n\t", json.Errors)); string simpleGuyJson = json.Data.StringValue; SimpleGuy serializedGuy = JsonHelper.BuildObjectForJson <SimpleGuy>(json); Assert.IsTrue(guy.Equals(serializedGuy)); }
public void AJson_JsonBuilding_HandleAttributeProperly_JsonWithJsonPropertyAsSelfAttribute() { var thing = new ThingThatHoldsElevator { SuperAwesomeInt = new OnlyCareAboutValue <int>(3) }; Json json = JsonHelper.BuildJsonForObject(thing); Assert.IsNotNull(json); Assert.IsFalse(json.HasErrors, "Json parse errors:\n" + String.Join("\n\t", json.Errors)); Assert.IsFalse(json.Data.StringValue.Contains("Value"), "We should be elevating out of value, instead it looks like we're showing the whole object."); var serializedThing = JsonHelper.BuildObjectForJson <ThingThatHoldsElevator>(json); Assert.IsInstanceOfType(serializedThing.SuperAwesomeInt, typeof(OnlyCareAboutValue <int>)); Assert.AreEqual(3, serializedThing.SuperAwesomeInt.Value); }
public void AJson_JsonHelper_IOBuiltInItems() { var testDictionary = new Dictionary <int, bool> { { 3, true }, { 4, false } }; var testDateTime = DateTime.Now; var testTimeSpan = TimeSpan.FromMilliseconds(2200.4); var testGuid = Guid.NewGuid(); var bunchaGuids = new[] { Guid.NewGuid(), Guid.NewGuid() }; var test = new ThingWithSpecialStuff(); test.DictionaryItem = testDictionary; test.DateTimeItem = testDateTime; test.TimeSpanItem = testTimeSpan; test.GuidItem = testGuid; test.BunchaGuids = bunchaGuids; Json json = JsonHelper.BuildJsonForObject(test); Assert.IsFalse(json.HasErrors, json.BuildJsonErrorReport()); var found = JsonHelper.BuildObjectForJson <ThingWithSpecialStuff>(json); Assert.IsNotNull(found); // Dictionary Assert.AreEqual(2, found.DictionaryItem.Count); Assert.IsTrue(found.DictionaryItem.ContainsKey(3)); Assert.IsTrue(found.DictionaryItem.ContainsKey(4)); Assert.AreEqual(true, found.DictionaryItem[3]); Assert.AreEqual(false, found.DictionaryItem[4]); // Buncha Guids Assert.AreEqual(2, found.BunchaGuids.Length); Assert.AreEqual(bunchaGuids[0], found.BunchaGuids[0]); Assert.AreEqual(bunchaGuids[1], found.BunchaGuids[1]); // Reading is a bit funky compared to normal creation, they have internal things // that show as different even though they look *EXACTLY* the same Assert.AreEqual(testDateTime.ToString(), found.DateTimeItem.ToString()); Assert.AreEqual(testTimeSpan, found.TimeSpanItem); Assert.AreEqual(testGuid, found.GuidItem); }
public void AJson_JsonHelper_BuildJsonForDictionary_ProperKVPTypingWithKeyValueTypeIdToWriteSetting() { var dictionary = new Dictionary <string, int> { { "Test", 10 } }; Json json = JsonHelper.BuildJsonForObject(dictionary); Assert.IsFalse(json.HasErrors, json.BuildJsonErrorReport()); // Verify we've made a useless read for the dictionary values, but the shape is correct Dictionary <object, object> generatedDictionary = JsonHelper.BuildObjectForJson <Dictionary <object, object> >(json); Assert.AreEqual(1, generatedDictionary.Count); Assert.AreEqual(1, generatedDictionary.Keys.Count); Assert.AreEqual(1, generatedDictionary.Values.Count); object generatedObjKey = generatedDictionary.Keys.First(); Assert.IsNotNull(generatedObjKey); object generatedObjValue = generatedDictionary[generatedObjKey]; Assert.IsNotNull(generatedObjValue); Assert.AreEqual(typeof(Object), generatedObjValue.GetType()); // === Try 2: With kvp option set === json = JsonHelper.BuildJsonForObject(dictionary, new JsonBuilder.Settings { KeyValuePairKeyTypeIdToWrite = eTypeIdInfo.FullyQualifiedSystemType, KeyValuePairValueTypeIdToWrite = eTypeIdInfo.FullyQualifiedSystemType }); generatedDictionary = JsonHelper.BuildObjectForJson <Dictionary <object, object> >(json); Assert.AreEqual(1, generatedDictionary.Count); Assert.AreEqual(1, generatedDictionary.Keys.Count); Assert.AreEqual(1, generatedDictionary.Values.Count); string generatedStrKey = generatedDictionary.Keys.First() as string; Assert.IsNotNull(generatedStrKey); Assert.AreEqual("Test", generatedStrKey); generatedObjValue = generatedDictionary[generatedStrKey]; Assert.IsNotNull(generatedObjValue); Assert.AreEqual(typeof(int), generatedObjValue.GetType()); Assert.AreEqual(10, (int)generatedObjValue); }
public void AJson_JsonBuilding_CanBuildWithNullSimpleProperty() { Json json = JsonHelper.BuildJsonForObject( new NullTester <string> { NumberProp = 8, NullProp = null } ); Assert.IsNotNull(json); Assert.IsFalse(json.HasErrors, "Json parse errors:\n" + String.Join("\n\t", json.Errors)); Assert.IsTrue(json.Data.IsDocument, "Json should have parsed an array"); NullTester <string> created = JsonHelper.BuildObjectForJson <NullTester <string> >(json); Assert.IsNotNull(created); Assert.AreEqual(8, created.NumberProp); Assert.IsNull(created.NullProp); }
public void AJson_JsonHelper_ParseJsonForDictionary() { var d = new Dictionary <string, string>(); d.Add("Test1", "V1"); d.Add("Test2", "V2"); d.Add("Test3", "V3"); Json json = JsonHelper.BuildJsonForObject(d); Assert.IsNotNull(json); Assert.IsFalse(json.HasErrors, "Json parse errors:\n" + String.Join("\n\t", json.Errors)); var parsed = JsonHelper.BuildObjectForJson <Dictionary <string, string> >(json); Assert.IsNotNull(parsed); Assert.AreEqual(d.Count, parsed.Count); Assert.AreEqual(d["Test1"], parsed["Test1"]); Assert.AreEqual(d["Test2"], parsed["Test2"]); Assert.AreEqual(d["Test3"], parsed["Test3"]); }
/* * private static readonly XmlSerializer g_xmlSerializer = new XmlSerializer(typeof(WorkspaceToChekcoutCache)); */ public static WorkspaceToChekcoutCache Read(string cacheLocation, string workspaceName) { string filePath = Path.Combine(cacheLocation, workspaceName + ".p4cache"); WorkspaceToChekcoutCache cacheFile = null; try { if (File.Exists(filePath)) { cacheFile = JsonHelper.BuildObjectForJson <WorkspaceToChekcoutCache>(JsonHelper.ParseFile(filePath)); /* * using (Stream s = File.OpenRead(filePath)) * { * StreamReader reader = new StreamReader(s); * * cacheFile = g_xmlSerializer.Deserialize(reader) as WorkspaceToChekcoutCache; * } */ } } catch (Exception exc) { Logger.LogError("Failed to load p4cache info", exc); } if (cacheFile == null) { return(new WorkspaceToChekcoutCache() { m_cachePath = filePath }); } else { cacheFile.m_cachePath = filePath; return(cacheFile); } }
public void AJson_JsonHelper_IOBuiltInItems_TempFocused() { var testGuids = new[] { Guid.NewGuid() }; var test = new ThingWithId { Ids = testGuids }; Json json = JsonHelper.BuildJsonForObject(test); Assert.IsFalse(json.HasErrors, json.BuildJsonErrorReport()); string jsonText = json.Data.StringValue; json = JsonHelper.ParseText(jsonText); Assert.IsFalse(json.HasErrors, json.BuildJsonErrorReport()); var found = JsonHelper.BuildObjectForJson <ThingWithId>(json); Assert.IsNotNull(found?.Ids); Assert.AreEqual(testGuids.Length, found.Ids.Length); Assert.AreEqual(testGuids[0], found.Ids[0]); }
public void AJson_JsonBuilding_DeserializeJsonToObject_PicksDerivedTypesProperly_UseTypeId() { var thing = new OtherThingWithDerived() { Guy = new OtherDerivedGuy { Other = 2, Special = "dood" } }; Json json = JsonHelper.BuildJsonForObject(thing, new JsonBuilder.Settings() { TypeIdToWrite = eTypeIdInfo.TypeIdAttributed | eTypeIdInfo.FullyQualifiedSystemType }); Assert.IsNotNull(json); Assert.IsFalse(json.HasErrors, "Json parse errors:\n" + String.Join("\n\t", json.Errors)); Assert.IsTrue(json.Data.StringValue.Contains("other-derived-guy (this is the type id)")); var serializedThing = JsonHelper.BuildObjectForJson <OtherThingWithDerived>(json); Assert.IsInstanceOfType(serializedThing.Guy, typeof(OtherDerivedGuy)); Assert.AreEqual("dood", (serializedThing.Guy as OtherDerivedGuy)?.Special); }
public void AJson_JsonBuilding_DeserializeJsonToObject_PicksDerivedTypesProperly() { ThingWithDerived thing = new ThingWithDerived() { Guy = new DerivedGuy(462) }; thing.Guy.What = 5; thing.Guy.Noice = 106; Json json = JsonHelper.BuildJsonForObject(thing, new JsonBuilder.Settings() { TypeIdToWrite = eTypeIdInfo.TypeIdAttributed | eTypeIdInfo.FullyQualifiedSystemType }); Assert.IsNotNull(json); Assert.IsFalse(json.HasErrors, "Json parse errors:\n" + String.Join("\n\t", json.Errors)); ThingWithDerived serializedThing = JsonHelper.BuildObjectForJson <ThingWithDerived>(json); Assert.IsInstanceOfType(serializedThing.Guy, typeof(DerivedGuy)); Assert.AreEqual(462, (serializedThing.Guy as DerivedGuy)?.TheDerivedSpecificProperty); }