public void DeserializeShortTest() { Serializer s = new Serializer(typeof(short)); short value = 32; short result = (short)s.Deserialize(value.ToString()); Assert.AreEqual(value, result, "short not deserialized correctly"); result = (short)s.Deserialize(" " + value.ToString() + " "); Assert.AreEqual(value, result, "short not deserialized correctly with whitespace"); value = -44; result = (short)s.Deserialize(" " + value.ToString() + " "); Assert.AreEqual(value, result, "Negative short not deserialized correctly with whitespace"); }
public void DeserializeLongTest() { Serializer s = new Serializer(typeof(long)); long value = 32; long result = (long)s.Deserialize(value.ToString()); Assert.AreEqual(value, result, "long not deserialized correctly"); result = (long)s.Deserialize(" " + value.ToString() + " "); Assert.AreEqual(value, result, "long not deserialized correctly with whitespace"); value = -44; result = (long)s.Deserialize(" " + value.ToString() + " "); Assert.AreEqual(value, result, "Negative long not deserialized correctly with whitespace"); }
public void DeepCircularReferenceError() { Serializer s = new Serializer(typeof(MockReferenceObject)); s.Config.ReferenceWritingType = SerializationContext.ReferenceOption.ErrorCircularReferences; string result = s.Serialize(deep); MockReferenceObject actual = (MockReferenceObject)s.Deserialize(result); }
public static STR_ArbInput GetInput(String inputJson) { Serializer serializer = new Serializer(typeof(STR_ArbInput)); STR_ArbInput input = (STR_ArbInput)serializer.Deserialize(inputJson); return input; }
static STR_Test_PB_HTSInput GetInput(String inputJson) { Serializer serializer = new Serializer(typeof(STR_Test_PB_HTSInput)); STR_Test_PB_HTSInput input = (STR_Test_PB_HTSInput)serializer.Deserialize(inputJson); return input; }
public void ClassAttributeTest() { Serializer serializer = new Serializer(typeof(MyImmutablePoint)); MyImmutablePoint expectedPt = new MyImmutablePoint(12, -10); string result = serializer.Serialize(expectedPt); MyImmutablePoint actualPt = (MyImmutablePoint) serializer.Deserialize(result); Assert.AreEqual(expectedPt, actualPt, "MyImmutablePoint class not serialized correctly"); }
public void CircularReferenceIgnore() { Serializer s = new Serializer(typeof(MockReferenceObject)); s.Config.ReferenceWritingType = SerializationContext.ReferenceOption.IgnoreCircularReferences; string result = s.Serialize(simple); MockReferenceObject actual = (MockReferenceObject)s.Deserialize(result); Assert.IsNull(actual.Reference.Reference); }
public void TestCollectionHandler() { Serializer s = new Serializer(typeof(MockCollection), "TestCollectionHandlers"); MockCollection coll = new MockCollection("test"); string result = s.Serialize(coll); MockCollection actual = (MockCollection) s.Deserialize(result); Assert.AreEqual(coll.Value(), actual.Value(), "MockCollectionHandler not configured correctly"); }
public void ConvertGuidTest() { Serializer s = new Serializer(typeof(Guid)); Guid g = Guid.NewGuid(); string result = s.Serialize(g); Guid actual = (Guid)s.Deserialize(result); Assert.AreEqual(g, actual, "Guid test failed"); }
public void SimpleConstructorNoInitTest() { MyPointConstructor pt = new MyPointConstructor(3, 9); Serializer s = new Serializer(pt.GetType()); string result = s.Serialize(pt); MyPointConstructor actual = (MyPointConstructor)s.Deserialize(result); Assert.AreEqual(pt, actual, "Simple Constructor with no initializer failed"); }
public void SerializeLongTest(long expected) { Serializer s = new Serializer(typeof(long)); s.Config.SetJsonStrictOptions(); string result = s.Serialize(expected); Assert.AreEqual(expected.ToString(CultureInfo.InvariantCulture), result.Trim(), "long did not serialize correctly"); long actual = (long)s.Deserialize(result); Assert.AreEqual(expected, actual, "long did not deserialize correctly"); }
public void TestIntField() { MockFields src = new MockFields(); src.IntValue = 23; Serializer s = new Serializer(typeof(MockFields)); string result = s.Serialize(src); MockFields dest = (MockFields) s.Deserialize(result); Assert.AreEqual(23, dest.IntValue); }
public void OnReadOnlyObject_ObjectIsSerialized() { ObjectParent parent = new ObjectParent(); parent.Item.Value = "TestValue"; Serializer s = new Serializer(typeof(ObjectParent)); string result = s.Serialize(parent); ObjectParent actual = (ObjectParent)s.Deserialize(result); Assert.AreEqual("TestValue", actual.Item.Value, "Readonly object value not set properly"); }
public void Deserialize_NewConstructorStyle_WorksSameAsCast() { Serializer s = new Serializer(typeof(CtorMock)); s.Config.TypeAliases.Add(typeof(CtorMock), "CtorMock"); s.Config.TypeAliases.Add(typeof(CtorMock2), "CtorMock2"); string text = "new CtorMock2(1, 'test')"; CtorMock x = (CtorMock)s.Deserialize(text); Assert.IsInstanceOfType(typeof(CtorMock2), x, "Wrong Type"); }
public void WhenHasProtectedField_ItsNotSerialized() { MockFields src = new MockFields(); src.SetProtected(true); Serializer s = new Serializer(typeof(MockFields)); string result = s.Serialize(src); MockFields dest = (MockFields)s.Deserialize(result); Assert.IsFalse(dest.GetProtected()); }
public override void UpdateUI(String strategyName, String header, String jsonData) { if (header.CompareTo(StrategyCommand.WAS_DIFF_DATA) == 0) { Serializer ser = new Serializer(typeof(Was_Diff_DataForUI)); Was_Diff_DataForUI data = (Was_Diff_DataForUI)ser.Deserialize(jsonData); DrawDiffData(data); } }
public void RMDJsonTest() { RawMarketData rmd = new RawMarketData("testCode", Detail.ProductType.KospiFuture); RawMarketDataJson rmdJson = new RawMarketDataJson(rmd); Serializer serializer = new Serializer(typeof(RawMarketDataJson)); string jsonText = serializer.Serialize(rmdJson); RawMarketDataJson rmdJson2 = (RawMarketDataJson)serializer.Deserialize(jsonText); }
public void StringArrayListTest() { Serializer s = new Serializer(typeof(ArrayList)); ArrayList strings = new ArrayList(); strings.Add("one"); strings.Add("two"); strings.Add("3"); string result = s.Serialize(strings); ArrayList actual = (ArrayList) s.Deserialize(result); CollectionAssert.AreEqual(strings, actual); }
private object DeSerialization(Type type, String path) { if (File.Exists(Application.StartupPath + path/*@"/soft.vault"*/)) { var s = new Serializer(/*typeof(TypeSoftList)*/type); TextReader r = new StreamReader(Application.StartupPath + path/*@"/soft.vault"*/); var data = s.Deserialize(r); r.Close(); return data; } return null; }
public void TestResolveConcreteTypeToInterfaceProperty() { Serializer serializer = new Serializer(typeof(Message)); string json = @"{ type:'Type1', value: {A:1, B: 2} }"; serializer.Context.ParsingStages.Add(new CustomTypeResolver()); Message result = (Message)serializer.Deserialize(json); IType value = result.value; Assert.IsNotNull(value, "value not deserialized"); Assert.IsInstanceOfType(typeof(Type1), value, "Incorrect type on value"); Assert.AreEqual(1, ((Type1)value).A, "obj1.A"); Assert.AreEqual(2, ((Type1)value).B, "obj1.B"); }
public void WhenHasPublicObjectField_ObjectIsSerialized() { MockFields src = new MockFields(); SimpleObject so = new SimpleObject(); so.IntValue = 23; src.SimpleObj = so; Serializer s = new Serializer(typeof(MockFields)); string result = s.Serialize(src); MockFields dest = (MockFields)s.Deserialize(result); Assert.AreEqual(23, dest.SimpleObj.IntValue); }
public void OnReadOnlyCollection_CollectionIsSerialized() { CollParent parent = new CollParent(); parent.Items.Add(new CollItem("2")); parent.Items.Add(new CollItem("Two")); Serializer s = new Serializer(typeof(CollParent)); string result = s.Serialize(parent); CollParent actual = (CollParent)s.Deserialize(result); Assert.AreEqual(2, actual.Items.Count, "Wrong item count on readonly collection"); Assert.AreEqual("2", actual.Items[0].Value, "Wrong item in first position"); Assert.AreEqual("Two", actual.Items[1].Value, "Wrong item in first position"); }
public void DeserializeFloatTest() { Serializer s = new Serializer(typeof(float)); float value = 32.44f; float result = (float)s.Deserialize(value.ToString()); Assert.AreEqual(value, result, "float not deserialized correctly"); result = (float)s.Deserialize(" " + value.ToString() + " "); Assert.AreEqual(value, result, "float not deserialized correctly with whitespace"); value = 0.0f; result = (float)s.Deserialize(value.ToString()); Assert.AreEqual(value, result, "float zero not deserialized correctly"); value = -44.56f; result = (float)s.Deserialize(" " + value.ToString() + " "); Assert.AreEqual(value, result, "Negative float not deserialized correctly with whitespace"); value = float.MaxValue; result = (float)s.Deserialize(value.ToString("R")); Assert.AreEqual(value, result, "Error deserializing float max value"); }
public void CallbackTest() { MockCallbackObject expected = new MockCallbackObject(); expected.Name = "callback"; Serializer s = new Serializer(expected.GetType()); string result = s.Serialize(expected); Assert.AreEqual(1, expected.BeforeSerializeCount, "BeforeSerialize incorrect count"); Assert.AreEqual(1, expected.AfterSerializeCount, "AfterSerialize incorrect count"); MockCallbackObject actual = (MockCallbackObject)s.Deserialize(result); Assert.AreEqual(1, actual.AfterDeserializeCount, "AfterDeserialize incorrect count"); }
public static STR_OOR Build(String inputJson, String strategyName) { STR_OOR oor = new STR_OOR(strategyName); Serializer serializer = new Serializer(typeof(STR_OORInput)); STR_OORInput input = serializer.Deserialize(inputJson) as STR_OORInput; Build_ElwPart(oor, input); // validation oor.ValidateInits_6(); return oor; }
public void TestISODateTime() { DateTimeExpressionHandler handler = new DateTimeExpressionHandler("O"); Serializer s = new Serializer(typeof(DateTime)); s.Config.ExpressionHandlers.InsertBefore(typeof(DateTimeExpressionHandler), handler); s.Config.SetJsonStrictOptions(); s.Config.IsCompact = true; DateTime source = new DateTime(2008, 10, 9, 13, 23, 45); string result = s.Serialize(source); Assert.AreEqual(string.Format("\"{0:O}\"", source), result, "DateTime not serialized correctly using ISO DateTime Format"); DateTime deserialized = (DateTime)s.Deserialize(result); Assert.AreEqual(source, deserialized, "DateTime did not deserialize properly using ISO DateTime Format"); }
public void WhenCustomMetaDataIsReturn_ItsSerializedCorrectly() { Serializer s = new Serializer(typeof(CustomClass)); s.Config.TypeHandlerFactory = new CustomTypeDataRepository(typeof(CustomClassTypeHandler), s.Config); CustomClass cust = new CustomClass(); cust.SetID(23); cust.SetName("Frank"); cust.Value = 999; string result = s.Serialize(cust); CustomClass dest = (CustomClass)s.Deserialize(result); Assert.AreEqual(23, dest.GetID()); Assert.AreEqual("Frank", dest.GetName()); Assert.AreEqual(999, dest.Value); }
public void IntListTest() { Serializer s = new Serializer(typeof(List<int>)); List<int> ints = new List<int>(); ints.Add(0); ints.Add(int.MinValue); ints.Add(int.MaxValue); ints.Add(-23); ints.Add(456); ints.Add(int.MaxValue - 1); ints.Add(int.MinValue + 1); string result = s.Serialize(ints); List<int> actual = (List<int>)s.Deserialize(result); CollectionAssert.AreEqual(ints, actual); }
static Card ConvertRowToCard(DataRow rowToConvert) { String markup = (String)rowToConvert["markup"]; Serializer cardSerializer = new Serializer(typeof(Card)); Card card = (Card)cardSerializer.Deserialize(markup); card.ID = (Int32)rowToConvert["id"]; card.Guid = (String)rowToConvert["guid"]; card.Version = (Int32)rowToConvert["version"]; if (card.Creds == null) { card.Creds = new Credits(); } card.Creds.Designer = (String)rowToConvert["editor"]; return card; }
public void IgnorePropertyTest() { MyLine line = new MyLine(); line.Start = new MyImmutablePoint(1, 5); line.End = new MyImmutablePoint(2, 12); Serializer s = new Serializer(typeof(MyLine), "TestIgnoreProperties"); string result = s.Serialize(line); MyLine actual = (MyLine)s.Deserialize(result); Assert.IsNull(actual.Start, "Line start should be ignored"); Assert.IsNull(actual.End, "Line end should be ignored"); // converters should not be called on ignored properties Assert.AreEqual(0, MyLinePointConverter.ConvertFromCount, "Property ConvertFrom not called correct amount of times"); Assert.AreEqual(0, MyLinePointConverter.ConvertToCount, "Property ConvertTo not called correct amount of times"); }