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_BuildJsonForAnonymous_SimpleObject() { float Floaty = 3.14f; var d = new { What = 3, Noice = true, Floaty }; Json json = JsonHelper.BuildJsonForObject(d); Assert.IsNotNull(json); Assert.IsFalse(json.HasErrors, "Json parse errors:\n" + String.Join("\n\t", json.Errors)); Assert.IsNotNull(json.Data); Assert.IsTrue(json.Data.IsDocument); Assert.AreEqual(3, ((JsonDocument)json.Data).Count); // What JsonValue what = ((JsonDocument)json.Data).ValueFor("What"); Assert.IsNotNull(what); Assert.AreEqual("3", what.StringValue); // Noice JsonValue noice = ((JsonDocument)json.Data).ValueFor("Noice"); Assert.IsNotNull(noice); Assert.AreEqual(true.ToString(), noice.StringValue); JsonValue floaty = ((JsonDocument)json.Data).ValueFor("Floaty"); Assert.IsNotNull(floaty); Assert.AreEqual(Floaty.ToString(), floaty.StringValue); json.AssertSourceIsValid(); }
public void AJson_JsonHelper_BuildJsonForObject_ComplexObject() { Json json = JsonHelper.BuildJsonForObject(ComplexGuy.MakeOne()); Assert.IsNotNull(json); Assert.IsFalse(json.HasErrors, "Json parse errors:\n" + String.Join("\n\t", json.Errors)); json.AssertSourceIsValid(); }
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(); }
public void AJson_DictionaryArrayFailure() { Dictionary <string, int>[] da = new Dictionary <string, int> [2]; da[0] = new Dictionary <string, int> { { "Test0", 10 } }; Json json = JsonHelper.BuildJsonForObject(da); Assert.IsFalse(json.HasErrors, json.BuildJsonErrorReport()); }
public void AJson_JsonHelper_BuildJsonForObject_ObjectWithItems() { ItemsGuy d = new ItemsGuy("Bob"); d.Items.AddRange(new[] { 1, 2, 3, 4 }); Json json = JsonHelper.BuildJsonForObject(d); Assert.IsNotNull(json); Assert.IsFalse(json.HasErrors, "Json parse errors:\n" + String.Join("\n\t", json.Errors)); json.AssertSourceIsValid(); }
public void AJson_JsonHelper_BuildJsonForDictionary() { var d = new Dictionary <string, string>(); d.Add("Test", "Value"); Json json = JsonHelper.BuildJsonForObject(d); Assert.IsNotNull(json); Assert.IsFalse(json.HasErrors, "Json parse errors:\n" + String.Join("\n\t", json.Errors)); Assert.IsNotNull(json.Data); Assert.IsTrue(json.Data.IsArray); Assert.AreEqual(1, ((JsonArray)json.Data).Count); }
public void AJson_JsonHelper_BuildJsonForObject_SimpleObject() { SimpleGuy d = new SimpleGuy() { What = 3, Noice = 5 }; Json json = JsonHelper.BuildJsonForObject(d); Assert.IsNotNull(json); Assert.IsFalse(json.HasErrors, "Json parse errors:\n" + String.Join("\n\t", json.Errors)); json.AssertSourceIsValid(); }
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_CreatingJson_UsingNullValues() { var test = new TestingClass(); test.NumberValue = 2; test.ObjectValue = null; Json json = JsonHelper.BuildJsonForObject(test, m_testSettings); var doc = json.Data as JsonDocument; Assert.IsNotNull(doc); Assert.AreEqual(1, doc.Count); Assert.AreEqual("NumberValue", doc.KeyAt(0)); Assert.IsTrue(doc.ValueAt(0).IsValue); Assert.AreEqual("2", doc.ValueAt(0).StringValue); }
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_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_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"]); }
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 Write() { try { /* * using (Stream s = File.OpenWrite(m_cachePath)) * { * StreamWriter writer = new StreamWriter(s); * g_xmlSerializer.Serialize(writer, this); * } */ Json json = JsonHelper.BuildJsonForObject(this); if (!json.HasErrors) { File.WriteAllText(m_cachePath, json.Data.StringValue); } } catch (Exception exc) { Logger.LogError("There was an error writing PerforceCmdManager's checkout later cache to disk.", exc); } }
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); }
public Json SerializeToJson(bool includeBaseline = true, params int[] overrideLayersToInclude) { var output = new StratabaseDataModel(this, includeBaseline, overrideLayersToInclude); return(JsonHelper.BuildJsonForObject(output, StratabaseDataModel.kJsonSettings)); }